|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpSchool\PHP8Appreciate\Exercise; |
| 4 | + |
| 5 | +use Faker\Generator as FakerGenerator; |
| 6 | +use GuzzleHttp\Psr7\Request; |
| 7 | +use PhpParser\Node\Expr\Ternary; |
| 8 | +use PhpParser\Node\Expr\Throw_; |
| 9 | +use PhpParser\Node\Stmt; |
| 10 | +use PhpParser\Node\Stmt\If_; |
| 11 | +use PhpParser\NodeFinder; |
| 12 | +use PhpParser\Parser; |
| 13 | +use PhpSchool\PhpWorkshop\Exercise\AbstractExercise; |
| 14 | +use PhpSchool\PhpWorkshop\Exercise\CgiExercise; |
| 15 | +use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface; |
| 16 | +use PhpSchool\PhpWorkshop\Exercise\ExerciseType; |
| 17 | +use PhpSchool\PhpWorkshop\Exercise\ProvidesInitialCode; |
| 18 | +use PhpSchool\PhpWorkshop\Exercise\SubmissionPatchable; |
| 19 | +use PhpSchool\PhpWorkshop\ExerciseCheck\SelfCheck; |
| 20 | +use PhpSchool\PhpWorkshop\Input\Input; |
| 21 | +use PhpSchool\PhpWorkshop\Patch; |
| 22 | +use PhpSchool\PhpWorkshop\Result\Failure; |
| 23 | +use PhpSchool\PhpWorkshop\Result\ResultInterface; |
| 24 | +use PhpSchool\PhpWorkshop\Result\Success; |
| 25 | +use PhpSchool\PhpWorkshop\Solution\SingleFileSolution; |
| 26 | +use PhpSchool\PhpWorkshop\Solution\SolutionInterface; |
| 27 | + |
| 28 | +class ThrowAnExpression extends AbstractExercise implements |
| 29 | + ExerciseInterface, |
| 30 | + CgiExercise, |
| 31 | + SelfCheck, |
| 32 | + ProvidesInitialCode, |
| 33 | + SubmissionPatchable |
| 34 | +{ |
| 35 | + public function __construct(private Parser $parser, private FakerGenerator $faker) |
| 36 | + { |
| 37 | + } |
| 38 | + |
| 39 | + public function getName(): string |
| 40 | + { |
| 41 | + return 'Throw an Expression'; |
| 42 | + } |
| 43 | + |
| 44 | + public function getDescription(): string |
| 45 | + { |
| 46 | + return 'PHP 8\'s throw expression'; |
| 47 | + } |
| 48 | + |
| 49 | + public function getType(): ExerciseType |
| 50 | + { |
| 51 | + return ExerciseType::CGI(); |
| 52 | + } |
| 53 | + |
| 54 | + public function getRequests(): array |
| 55 | + { |
| 56 | + return [ |
| 57 | + (new Request('GET', 'https://top-secret.com/forbidden')), |
| 58 | + (new Request('GET', 'https://top-secret.com/blog')) |
| 59 | + ]; |
| 60 | + } |
| 61 | + |
| 62 | + public function check(Input $input): ResultInterface |
| 63 | + { |
| 64 | + /** @var array<Stmt> $statements */ |
| 65 | + $statements = $this->parser->parse((string) file_get_contents($input->getRequiredArgument('program'))); |
| 66 | + |
| 67 | + /** @var If_|null $if */ |
| 68 | + $if = (new NodeFinder())->findFirstInstanceOf($statements, If_::class); |
| 69 | + |
| 70 | + if ($if) { |
| 71 | + return Failure::fromNameAndReason($this->getName(), 'If statement found'); |
| 72 | + } |
| 73 | + |
| 74 | + /** @var Ternary|null $ternary */ |
| 75 | + $ternary = (new NodeFinder())->findFirstInstanceOf($statements, Ternary::class); |
| 76 | + |
| 77 | + if ($ternary === null) { |
| 78 | + return Failure::fromNameAndReason($this->getName(), 'No ternary statement found'); |
| 79 | + } |
| 80 | + |
| 81 | + if (!$ternary->if instanceof Throw_ && !$ternary->else instanceof Throw_) { |
| 82 | + return Failure::fromNameAndReason($this->getName(), 'Ternary does not make use of throw expression'); |
| 83 | + } |
| 84 | + |
| 85 | + return new Success($this->getName()); |
| 86 | + } |
| 87 | + |
| 88 | + public function getInitialCode(): SolutionInterface |
| 89 | + { |
| 90 | + return SingleFileSolution::fromFile( |
| 91 | + __DIR__ . '/../../exercises/throw-an-expression/initial/throw-an-expression.php' |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + public function getPatch(): Patch |
| 96 | + { |
| 97 | + return (new Patch()) |
| 98 | + ->withTransformer(new Patch\WrapInTryCatch(\InvalidArgumentException::class)); |
| 99 | + } |
| 100 | +} |
0 commit comments