|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpSchool\PHP8Appreciate\Exercise; |
| 6 | + |
| 7 | +use Faker\Generator; |
| 8 | +use PhpParser\Node\Stmt; |
| 9 | +use PhpParser\Node\Stmt\TryCatch; |
| 10 | +use PhpParser\NodeFinder; |
| 11 | +use PhpParser\Parser; |
| 12 | +use PhpSchool\PhpWorkshop\CodeInsertion; |
| 13 | +use PhpSchool\PhpWorkshop\Exercise\AbstractExercise; |
| 14 | +use PhpSchool\PhpWorkshop\Exercise\CliExercise; |
| 15 | +use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface; |
| 16 | +use PhpSchool\PhpWorkshop\Exercise\ExerciseType; |
| 17 | +use PhpSchool\PhpWorkshop\Exercise\SubmissionPatchable; |
| 18 | +use PhpSchool\PhpWorkshop\ExerciseCheck\SelfCheck; |
| 19 | +use PhpSchool\PhpWorkshop\Input\Input; |
| 20 | +use PhpSchool\PhpWorkshop\Patch; |
| 21 | +use PhpSchool\PhpWorkshop\Result\Failure; |
| 22 | +use PhpSchool\PhpWorkshop\Result\ResultInterface; |
| 23 | +use PhpSchool\PhpWorkshop\Result\Success; |
| 24 | + |
| 25 | +class CautionWithCatches extends AbstractExercise implements |
| 26 | + ExerciseInterface, |
| 27 | + CliExercise, |
| 28 | + SelfCheck, |
| 29 | + SubmissionPatchable |
| 30 | +{ |
| 31 | + private string $password = ''; |
| 32 | + |
| 33 | + public function __construct(private Parser $parser, private Generator $faker) |
| 34 | + { |
| 35 | + } |
| 36 | + |
| 37 | + public function getName(): string |
| 38 | + { |
| 39 | + return 'Caution with Catches'; |
| 40 | + } |
| 41 | + |
| 42 | + public function getDescription(): string |
| 43 | + { |
| 44 | + return 'PHP 8\'s Non-capturing Catches'; |
| 45 | + } |
| 46 | + |
| 47 | + public function getType(): ExerciseType |
| 48 | + { |
| 49 | + return ExerciseType::CLI(); |
| 50 | + } |
| 51 | + |
| 52 | + public function getArgs(): array |
| 53 | + { |
| 54 | + $this->password = $this->faker->password(); |
| 55 | + return [[$this->password]]; |
| 56 | + } |
| 57 | + |
| 58 | + public function check(Input $input): ResultInterface |
| 59 | + { |
| 60 | + /** @var array<Stmt> $statements */ |
| 61 | + $statements = $this->parser->parse((string) file_get_contents($input->getRequiredArgument('program'))); |
| 62 | + |
| 63 | + /** @var TryCatch|null $tryCatch */ |
| 64 | + $tryCatch = (new NodeFinder())->findFirstInstanceOf($statements, TryCatch::class); |
| 65 | + |
| 66 | + if (null === $tryCatch) { |
| 67 | + return Failure::fromNameAndReason($this->getName(), 'No try/catch statement was found'); |
| 68 | + } |
| 69 | + |
| 70 | + if (count($tryCatch->catches) > 0 && $tryCatch->catches[0]->var !== null) { |
| 71 | + return Failure::fromNameAndReason($this->getName(), 'Exception variable was captured'); |
| 72 | + } |
| 73 | + |
| 74 | + return new Success($this->getName()); |
| 75 | + } |
| 76 | + |
| 77 | + public function getPatch(): Patch |
| 78 | + { |
| 79 | + $code = <<<CODE |
| 80 | + class InvalidPasswordException extends \RuntimeException {} |
| 81 | + function verify_password(string \$password) { |
| 82 | + throw new InvalidPasswordException(sprintf('The password "%s" is invalid', \$password)); |
| 83 | + } |
| 84 | + CODE; |
| 85 | + |
| 86 | + $passwordVerifyInsertion = new CodeInsertion(CodeInsertion::TYPE_BEFORE, $code); |
| 87 | + |
| 88 | + return (new Patch())->withInsertion($passwordVerifyInsertion); |
| 89 | + } |
| 90 | +} |
0 commit comments