|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpSchool\PHP8Appreciate\Exercise; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr\Assign; |
| 7 | +use PhpParser\Node\Expr\Clone_; |
| 8 | +use PhpParser\Node\Expr\Variable; |
| 9 | +use PhpParser\Node\Stmt; |
| 10 | +use PhpParser\Node\Stmt\ClassMethod; |
| 11 | +use PhpParser\Node\Stmt\Expression; |
| 12 | +use PhpParser\Node\Stmt\Function_; |
| 13 | +use PhpParser\NodeFinder; |
| 14 | +use PhpParser\Parser; |
| 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\Exercise\ProvidesInitialCode; |
| 20 | +use PhpSchool\PhpWorkshop\ExerciseCheck\SelfCheck; |
| 21 | +use PhpSchool\PhpWorkshop\Input\Input; |
| 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 TheReturnOfStatic extends AbstractExercise implements |
| 29 | + ExerciseInterface, |
| 30 | + ProvidesInitialCode, |
| 31 | + CliExercise, |
| 32 | + SelfCheck |
| 33 | +{ |
| 34 | + public function __construct(private Parser $parser) |
| 35 | + { |
| 36 | + } |
| 37 | + |
| 38 | + public function getName(): string |
| 39 | + { |
| 40 | + return 'The Return of Static'; |
| 41 | + } |
| 42 | + |
| 43 | + public function getDescription(): string |
| 44 | + { |
| 45 | + return 'PHP 8\'s static return types'; |
| 46 | + } |
| 47 | + |
| 48 | + public function getInitialCode(): SolutionInterface |
| 49 | + { |
| 50 | + return SingleFileSolution::fromFile( |
| 51 | + __DIR__ . '/../../exercises/the-return-of-static/initial/the-return-of-static.php' |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + public function getType(): ExerciseType |
| 56 | + { |
| 57 | + return ExerciseType::CLI(); |
| 58 | + } |
| 59 | + |
| 60 | + public function getArgs(): array |
| 61 | + { |
| 62 | + return []; |
| 63 | + } |
| 64 | + |
| 65 | + public function check(Input $input): ResultInterface |
| 66 | + { |
| 67 | + /** @var array<Stmt> $statements */ |
| 68 | + $statements = $this->parser->parse((string) file_get_contents($input->getRequiredArgument('program'))); |
| 69 | + |
| 70 | + $finder = new NodeFinder(); |
| 71 | + |
| 72 | + /** @var Stmt\Class_|null $class */ |
| 73 | + $class = $finder->findFirst($statements, function (Node $node) { |
| 74 | + return $node instanceof Stmt\Class_ && $node->name && $node->name->toString() === 'File'; |
| 75 | + }); |
| 76 | + |
| 77 | + /** @var ClassMethod|null $method */ |
| 78 | + $method = $finder->findFirst($class ? [$class] : [], function (Node $node) { |
| 79 | + return $node instanceof ClassMethod && $node->name->toString() === 'withPermissions'; |
| 80 | + }); |
| 81 | + |
| 82 | + if (!$class || !$method) { |
| 83 | + return new Failure($this->getName(), 'The method withPermissions cannot be found'); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + if (!($assign = $this->findAssignOnFirstLine($method))) { |
| 88 | + return new Failure($this->getName(), 'The first statement in withPermissions is not an assign'); |
| 89 | + } |
| 90 | + |
| 91 | + if ($this->isCloneOfThis($assign)) { |
| 92 | + return new Success($this->getName()); |
| 93 | + } |
| 94 | + |
| 95 | + return new Failure($this->getName(), 'The first statement is not a clone of `$this`'); |
| 96 | + } |
| 97 | + |
| 98 | + private function findAssignOnFirstLine(ClassMethod $method): ?Assign |
| 99 | + { |
| 100 | + if (!isset($method->stmts[0])) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + if (!$method->stmts[0] instanceof Expression) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + if (!$method->stmts[0]->expr instanceof Assign) { |
| 109 | + return null; |
| 110 | + } |
| 111 | + |
| 112 | + return $method->stmts[0]->expr; |
| 113 | + } |
| 114 | + |
| 115 | + private function isCloneOfThis(Assign $assign): bool |
| 116 | + { |
| 117 | + if (!$assign->expr instanceof Clone_) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + if (!$assign->expr->expr instanceof Variable) { |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + return $assign->expr->expr->name === 'this'; |
| 126 | + } |
| 127 | +} |
0 commit comments