|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpSchool\PHP8Appreciate\Exercise; |
| 4 | + |
| 5 | +use Faker\Generator as FakerGenerator; |
| 6 | +use PhpParser\BuilderFactory; |
| 7 | +use PhpParser\Node\Expr\Assign; |
| 8 | +use PhpParser\Node\Identifier; |
| 9 | +use PhpParser\Node\Stmt; |
| 10 | +use PhpParser\Node\Stmt\Expression; |
| 11 | +use PhpParser\Node\Stmt\Foreach_; |
| 12 | +use PhpParser\Node\Stmt\Function_; |
| 13 | +use PhpParser\NodeFinder; |
| 14 | +use PhpParser\Parser; |
| 15 | +use PhpSchool\PhpWorkshop\Check\FileComparisonCheck; |
| 16 | +use PhpSchool\PhpWorkshop\Check\FunctionRequirementsCheck; |
| 17 | +use PhpSchool\PhpWorkshop\Exercise\AbstractExercise; |
| 18 | +use PhpSchool\PhpWorkshop\Exercise\CliExercise; |
| 19 | +use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface; |
| 20 | +use PhpSchool\PhpWorkshop\Exercise\ExerciseType; |
| 21 | +use PhpSchool\PhpWorkshop\Exercise\SubmissionPatchable; |
| 22 | +use PhpSchool\PhpWorkshop\ExerciseCheck\FileComparisonExerciseCheck; |
| 23 | +use PhpSchool\PhpWorkshop\ExerciseCheck\FunctionRequirementsExerciseCheck; |
| 24 | +use PhpSchool\PhpWorkshop\ExerciseCheck\SelfCheck; |
| 25 | +use PhpSchool\PhpWorkshop\ExerciseDispatcher; |
| 26 | +use PhpSchool\PhpWorkshop\Input\Input; |
| 27 | +use PhpSchool\PhpWorkshop\Patch; |
| 28 | +use PhpSchool\PhpWorkshop\Result\Failure; |
| 29 | +use PhpSchool\PhpWorkshop\Result\ResultInterface; |
| 30 | +use PhpSchool\PhpWorkshop\Result\Success; |
| 31 | + |
| 32 | +class AllMixedUp extends AbstractExercise implements |
| 33 | + ExerciseInterface, |
| 34 | + CliExercise, |
| 35 | + SubmissionPatchable, |
| 36 | + FunctionRequirementsExerciseCheck, |
| 37 | + FileComparisonExerciseCheck, |
| 38 | + SelfCheck |
| 39 | +{ |
| 40 | + private ?Patch $patch = null; |
| 41 | + |
| 42 | + public function __construct(private Parser $parser, private FakerGenerator $faker) |
| 43 | + { |
| 44 | + } |
| 45 | + |
| 46 | + public function getName(): string |
| 47 | + { |
| 48 | + return 'All Mixed Up'; |
| 49 | + } |
| 50 | + |
| 51 | + public function getDescription(): string |
| 52 | + { |
| 53 | + return 'PHP 8\'s mixed type'; |
| 54 | + } |
| 55 | + |
| 56 | + public function getType(): ExerciseType |
| 57 | + { |
| 58 | + return ExerciseType::CLI(); |
| 59 | + } |
| 60 | + |
| 61 | + public function configure(ExerciseDispatcher $dispatcher): void |
| 62 | + { |
| 63 | + $dispatcher->requireCheck(FunctionRequirementsCheck::class); |
| 64 | + $dispatcher->requireCheck(FileComparisonCheck::class); |
| 65 | + } |
| 66 | + |
| 67 | + public function getArgs(): array |
| 68 | + { |
| 69 | + return []; |
| 70 | + } |
| 71 | + |
| 72 | + public function getPatch(): Patch |
| 73 | + { |
| 74 | + if ($this->patch) { |
| 75 | + return $this->patch; |
| 76 | + } |
| 77 | + |
| 78 | + $factory = new BuilderFactory(); |
| 79 | + |
| 80 | + $statements = [ |
| 81 | + new Expression( |
| 82 | + new Assign( |
| 83 | + $factory->var('items'), |
| 84 | + $factory->val([ |
| 85 | + $this->faker->word(), |
| 86 | + $this->faker->randomNumber(3), |
| 87 | + $this->faker->words($this->faker->numberBetween(3, 7)), |
| 88 | + $this->faker->randomFloat(), |
| 89 | + $this->faker->boolean(), |
| 90 | + null, |
| 91 | + $factory->new('stdClass') |
| 92 | + ]) |
| 93 | + ) |
| 94 | + ) |
| 95 | + ]; |
| 96 | + |
| 97 | + $statements[] = new Foreach_( |
| 98 | + $factory->var('items'), |
| 99 | + $factory->var('item'), |
| 100 | + [ |
| 101 | + 'stmts' => [ |
| 102 | + new Expression( |
| 103 | + $factory->funcCall( |
| 104 | + 'logParameter', |
| 105 | + $factory->args([$factory->var('item')]) |
| 106 | + ) |
| 107 | + ) |
| 108 | + ] |
| 109 | + ] |
| 110 | + ); |
| 111 | + |
| 112 | + return $this->patch = (new Patch()) |
| 113 | + ->withTransformer(function (array $originalStatements) use ($statements) { |
| 114 | + return array_merge($originalStatements, $statements); |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + public function getFilesToCompare(): array |
| 119 | + { |
| 120 | + return [ |
| 121 | + 'param.log' => [ |
| 122 | + 'strip' => '/\d{2}:\d{2}:\d{2}/' //strip out time strings |
| 123 | + ] |
| 124 | + ]; |
| 125 | + } |
| 126 | + |
| 127 | + public function getRequiredFunctions(): array |
| 128 | + { |
| 129 | + return ['get_debug_type']; |
| 130 | + } |
| 131 | + |
| 132 | + public function getBannedFunctions(): array |
| 133 | + { |
| 134 | + return []; |
| 135 | + } |
| 136 | + |
| 137 | + public function check(Input $input): ResultInterface |
| 138 | + { |
| 139 | + /** @var array<Stmt> $statements */ |
| 140 | + $statements = $this->parser->parse((string) file_get_contents($input->getRequiredArgument('program'))); |
| 141 | + |
| 142 | + /** @var Function_|null $logger */ |
| 143 | + $logger = (new NodeFinder())->findFirst($statements, function (\PhpParser\Node $node) { |
| 144 | + return $node instanceof Function_ && $node->name->toString() === 'logParameter'; |
| 145 | + }); |
| 146 | + |
| 147 | + if (null === $logger) { |
| 148 | + return Failure::fromNameAndReason($this->getName(), 'No function named logParameter was found'); |
| 149 | + } |
| 150 | + |
| 151 | + if (!isset($logger->params[0])) { |
| 152 | + return Failure::fromNameAndReason($this->getName(), 'Function logParameter has no parameters'); |
| 153 | + } |
| 154 | + |
| 155 | + if ($logger->params[0]->type === null) { |
| 156 | + return Failure::fromNameAndReason( |
| 157 | + $this->getName(), |
| 158 | + 'Function logParameter has no type for it\'s for first parameter' |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + $type = $logger->params[0]->type; |
| 163 | + |
| 164 | + if (!$type instanceof Identifier || $type->name !== 'mixed') { |
| 165 | + return Failure::fromNameAndReason( |
| 166 | + $this->getName(), |
| 167 | + 'Function logParameter does not use the mixed type for it\'s first param' |
| 168 | + ); |
| 169 | + } |
| 170 | + |
| 171 | + return new Success($this->getName()); |
| 172 | + } |
| 173 | +} |
0 commit comments