|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpSchool\PHP8Appreciate\Exercise; |
| 4 | + |
| 5 | +use DivisionByZeroError; |
| 6 | +use Faker\Generator as FakerGenerator; |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr\BinaryOp\Div; |
| 9 | +use PhpParser\Node\Name; |
| 10 | +use PhpParser\Node\Stmt; |
| 11 | +use PhpParser\Node\Stmt\Catch_; |
| 12 | +use PhpParser\NodeFinder; |
| 13 | +use PhpParser\Parser; |
| 14 | +use PhpSchool\PhpWorkshop\Check\FunctionRequirementsCheck; |
| 15 | +use PhpSchool\PhpWorkshop\Exercise\AbstractExercise; |
| 16 | +use PhpSchool\PhpWorkshop\Exercise\CliExercise; |
| 17 | +use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface; |
| 18 | +use PhpSchool\PhpWorkshop\Exercise\ExerciseType; |
| 19 | +use PhpSchool\PhpWorkshop\ExerciseCheck\FunctionRequirementsExerciseCheck; |
| 20 | +use PhpSchool\PhpWorkshop\ExerciseCheck\SelfCheck; |
| 21 | +use PhpSchool\PhpWorkshop\ExerciseDispatcher; |
| 22 | +use PhpSchool\PhpWorkshop\Input\Input; |
| 23 | +use PhpSchool\PhpWorkshop\Result\Failure; |
| 24 | +use PhpSchool\PhpWorkshop\Result\ResultInterface; |
| 25 | +use PhpSchool\PhpWorkshop\Result\Success; |
| 26 | + |
| 27 | +class InfiniteDivisions extends AbstractExercise implements |
| 28 | + ExerciseInterface, |
| 29 | + CliExercise, |
| 30 | + FunctionRequirementsExerciseCheck, |
| 31 | + SelfCheck |
| 32 | +{ |
| 33 | + public function __construct(private Parser $parser, private FakerGenerator $faker) |
| 34 | + { |
| 35 | + } |
| 36 | + |
| 37 | + public function getName(): string |
| 38 | + { |
| 39 | + return 'Infinite Divisions'; |
| 40 | + } |
| 41 | + |
| 42 | + public function getDescription(): string |
| 43 | + { |
| 44 | + return 'PHP 8\'s fdiv function and DivisionByZeroError exception'; |
| 45 | + } |
| 46 | + |
| 47 | + public function getType(): ExerciseType |
| 48 | + { |
| 49 | + return ExerciseType::CLI(); |
| 50 | + } |
| 51 | + |
| 52 | + public function configure(ExerciseDispatcher $dispatcher): void |
| 53 | + { |
| 54 | + $dispatcher->requireCheck(FunctionRequirementsCheck::class); |
| 55 | + } |
| 56 | + |
| 57 | + public function getArgs(): array |
| 58 | + { |
| 59 | + return [ |
| 60 | + [ |
| 61 | + (string) $this->faker->randomFloat(3, 10, 100), |
| 62 | + '0' |
| 63 | + ], |
| 64 | + [ |
| 65 | + (string) $this->faker->randomFloat(3, 10, 100), |
| 66 | + (string) $this->faker->randomFloat(3, 0, 10) |
| 67 | + ] |
| 68 | + ]; |
| 69 | + } |
| 70 | + |
| 71 | + public function getRequiredFunctions(): array |
| 72 | + { |
| 73 | + return ['fdiv', 'round']; |
| 74 | + } |
| 75 | + |
| 76 | + public function getBannedFunctions(): array |
| 77 | + { |
| 78 | + return []; |
| 79 | + } |
| 80 | + |
| 81 | + public function check(Input $input): ResultInterface |
| 82 | + { |
| 83 | + /** @var array<Stmt> $statements */ |
| 84 | + $statements = $this->parser->parse((string) file_get_contents($input->getRequiredArgument('program'))); |
| 85 | + |
| 86 | + $finder = new NodeFinder(); |
| 87 | + |
| 88 | + /** @var Stmt\TryCatch|null $tryCatch */ |
| 89 | + $tryCatch = $finder->findFirstInstanceOf($statements, Stmt\TryCatch::class); |
| 90 | + |
| 91 | + if (!$tryCatch) { |
| 92 | + return new Failure($this->getName(), 'No try/catch statement could be found'); |
| 93 | + } |
| 94 | + |
| 95 | + /** @var Div|null $divOp */ |
| 96 | + $divOp = $finder->findFirstInstanceOf($tryCatch->stmts, Div::class); |
| 97 | + |
| 98 | + if (!$divOp) { |
| 99 | + return new Failure($this->getName(), 'No division operation could be found in the try block'); |
| 100 | + } |
| 101 | + |
| 102 | + /** @var Catch_|null $catch */ |
| 103 | + $catch = $finder->findFirst($tryCatch->catches, function (Node $node) { |
| 104 | + if ($node instanceof Catch_) { |
| 105 | + return in_array( |
| 106 | + DivisionByZeroError::class, |
| 107 | + array_map(fn (Name $n) => $n->toString(), $node->types), |
| 108 | + true |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + return false; |
| 113 | + }); |
| 114 | + |
| 115 | + if (!$catch) { |
| 116 | + return new Failure($this->getName(), 'No catch block for the DivisionByZeroError exception found'); |
| 117 | + } |
| 118 | + |
| 119 | + return new Success($this->getName()); |
| 120 | + } |
| 121 | +} |
0 commit comments