|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpSchool\PHP8Appreciate\Exercise; |
| 4 | + |
| 5 | +use Faker\Generator as FakerGenerator; |
| 6 | +use Faker\Provider\en_US\Address; |
| 7 | +use Faker\Provider\en_US\Person; |
| 8 | +use PhpParser\BuilderFactory; |
| 9 | +use PhpParser\Node; |
| 10 | +use PhpParser\Node\Expr\Assign; |
| 11 | +use PhpParser\Node\Expr\NullsafePropertyFetch; |
| 12 | +use PhpParser\Node\Expr\Variable; |
| 13 | +use PhpParser\Node\Identifier; |
| 14 | +use PhpParser\Node\NullableType; |
| 15 | +use PhpParser\Node\Stmt; |
| 16 | +use PhpParser\Node\Stmt\Expression; |
| 17 | +use PhpParser\NodeFinder; |
| 18 | +use PhpParser\Parser; |
| 19 | +use PhpSchool\PhpWorkshop\Check\FileComparisonCheck; |
| 20 | +use PhpSchool\PhpWorkshop\Exercise\AbstractExercise; |
| 21 | +use PhpSchool\PhpWorkshop\Exercise\CliExercise; |
| 22 | +use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface; |
| 23 | +use PhpSchool\PhpWorkshop\Exercise\ExerciseType; |
| 24 | +use PhpSchool\PhpWorkshop\Exercise\SubmissionPatchable; |
| 25 | +use PhpSchool\PhpWorkshop\ExerciseCheck\FileComparisonExerciseCheck; |
| 26 | +use PhpSchool\PhpWorkshop\ExerciseCheck\SelfCheck; |
| 27 | +use PhpSchool\PhpWorkshop\ExerciseDispatcher; |
| 28 | +use PhpSchool\PhpWorkshop\Input\Input; |
| 29 | +use PhpSchool\PhpWorkshop\Patch; |
| 30 | +use PhpSchool\PhpWorkshop\Result\Failure; |
| 31 | +use PhpSchool\PhpWorkshop\Result\ResultInterface; |
| 32 | +use PhpSchool\PhpWorkshop\Result\Success; |
| 33 | + |
| 34 | +class ASafeSpaceForNulls extends AbstractExercise implements |
| 35 | + ExerciseInterface, |
| 36 | + CliExercise, |
| 37 | + SubmissionPatchable, |
| 38 | + FileComparisonExerciseCheck, |
| 39 | + SelfCheck |
| 40 | +{ |
| 41 | + |
| 42 | + private ?Patch $patch = null; |
| 43 | + |
| 44 | + public function __construct(private Parser $parser, private FakerGenerator $faker) |
| 45 | + { |
| 46 | + } |
| 47 | + |
| 48 | + public function getName(): string |
| 49 | + { |
| 50 | + return 'A Safe Space For Nulls'; |
| 51 | + } |
| 52 | + |
| 53 | + public function getDescription(): string |
| 54 | + { |
| 55 | + return 'PHP 8\'s Null Safe Operator'; |
| 56 | + } |
| 57 | + |
| 58 | + public function getType(): ExerciseType |
| 59 | + { |
| 60 | + return new ExerciseType(ExerciseType::CLI); |
| 61 | + } |
| 62 | + |
| 63 | + public function getArgs(): array |
| 64 | + { |
| 65 | + return []; |
| 66 | + } |
| 67 | + |
| 68 | + public function configure(ExerciseDispatcher $dispatcher): void |
| 69 | + { |
| 70 | + $dispatcher->requireCheck(FileComparisonCheck::class); |
| 71 | + } |
| 72 | + |
| 73 | + public function getPatch(): Patch |
| 74 | + { |
| 75 | + if ($this->patch) { |
| 76 | + return $this->patch; |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + $factory = new BuilderFactory(); |
| 81 | + |
| 82 | + $statements = []; |
| 83 | + $statements[] = $factory->class('User') |
| 84 | + ->addStmt($factory->property('firstName')->setType('string')->makePublic()) |
| 85 | + ->addStmt($factory->property('lastName')->setType('string')->makePublic()) |
| 86 | + ->addStmt($factory->property('age')->setType(new NullableType('int'))->makePublic()->setDefault(null)) |
| 87 | + ->addStmt($factory->property('address') |
| 88 | + ->setType(new NullableType('Address')) |
| 89 | + ->makePublic() |
| 90 | + ->setDefault(null)) |
| 91 | + ->getNode(); |
| 92 | + |
| 93 | + $statements[] = $factory->class('Address') |
| 94 | + ->addStmt($factory->property('number')->setType('int')->makePublic()) |
| 95 | + ->addStmt($factory->property('addressLine1')->setType('string')->makePublic()) |
| 96 | + ->addStmt($factory->property('addressLine2') |
| 97 | + ->setType(new NullableType('string')) |
| 98 | + ->makePublic() |
| 99 | + ->setDefault(null)) |
| 100 | + ->getNode(); |
| 101 | + |
| 102 | + $addressFaker = new Address($this->faker); |
| 103 | + $personFaker = new Person($this->faker); |
| 104 | + |
| 105 | + $statements[] = new Expression( |
| 106 | + new Assign($factory->var('user'), $factory->new('User')) |
| 107 | + ); |
| 108 | + $statements[] = new Expression( |
| 109 | + new Assign( |
| 110 | + $factory->propertyFetch($factory->var('user'), 'firstName'), |
| 111 | + $factory->val($personFaker->firstName()) |
| 112 | + ) |
| 113 | + ); |
| 114 | + $statements[] = new Expression( |
| 115 | + new Assign( |
| 116 | + $factory->propertyFetch($factory->var('user'), 'lastName'), |
| 117 | + $factory->val($personFaker->lastName()) |
| 118 | + ) |
| 119 | + ); |
| 120 | + |
| 121 | + if ($this->faker->boolean()) { |
| 122 | + $statements[] = new Expression( |
| 123 | + new Assign( |
| 124 | + $factory->propertyFetch($factory->var('user'), 'age'), |
| 125 | + $factory->val($this->faker->numberBetween(18, 100)) |
| 126 | + ) |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + if ($this->faker->boolean()) { |
| 131 | + $statements[] = new Expression( |
| 132 | + new Assign( |
| 133 | + $factory->propertyFetch($factory->var('user'), 'address'), |
| 134 | + $factory->new('Address') |
| 135 | + ) |
| 136 | + ); |
| 137 | + $statements[] = new Expression( |
| 138 | + new Assign( |
| 139 | + $factory->propertyFetch( |
| 140 | + $factory->propertyFetch($factory->var('user'), 'address'), |
| 141 | + 'number' |
| 142 | + ), |
| 143 | + $factory->val($addressFaker->buildingNumber()) |
| 144 | + ) |
| 145 | + ); |
| 146 | + $statements[] = new Expression( |
| 147 | + new Assign( |
| 148 | + $factory->propertyFetch( |
| 149 | + $factory->propertyFetch($factory->var('user'), 'address'), |
| 150 | + 'addressLine1' |
| 151 | + ), |
| 152 | + $factory->val($addressFaker->streetName()) |
| 153 | + ) |
| 154 | + ); |
| 155 | + |
| 156 | + if ($this->faker->boolean()) { |
| 157 | + $statements[] = new Expression( |
| 158 | + new Assign( |
| 159 | + $factory->propertyFetch( |
| 160 | + $factory->propertyFetch($factory->var('user'), 'address'), |
| 161 | + 'addressLine2' |
| 162 | + ), |
| 163 | + $factory->val($addressFaker->secondaryAddress()) |
| 164 | + ) |
| 165 | + ); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + return $this->patch = (new Patch()) |
| 170 | + ->withTransformer(function (array $originalStatements) use ($statements) { |
| 171 | + return array_merge($statements, $originalStatements); |
| 172 | + }); |
| 173 | + } |
| 174 | + |
| 175 | + public function check(Input $input): ResultInterface |
| 176 | + { |
| 177 | + /** @var array<Stmt> $statements */ |
| 178 | + $statements = $this->parser->parse((string) file_get_contents($input->getRequiredArgument('program'))); |
| 179 | + |
| 180 | + $ageFetch = $this->findNullSafePropFetch($statements, 'user', 'age'); |
| 181 | + $addressFetch = $this->findAllNullSafePropertyFetch($statements, 'user', 'address'); |
| 182 | + |
| 183 | + if ($ageFetch === null) { |
| 184 | + return new Failure( |
| 185 | + $this->getName(), |
| 186 | + 'The $user->age property should be accessed with the null safe operator' |
| 187 | + ); |
| 188 | + } |
| 189 | + |
| 190 | + if (count($addressFetch) < 3) { |
| 191 | + return new Failure( |
| 192 | + $this->getName(), |
| 193 | + 'The $user->address property should always be accessed with the null safe operator' |
| 194 | + ); |
| 195 | + } |
| 196 | + |
| 197 | + $props = [ |
| 198 | + '$user->address->number' => $this->findNestedNullSafePropFetch($statements, 'user', 'number'), |
| 199 | + '$user->address->addressLine1' => $this->findNestedNullSafePropFetch($statements, 'user', 'addressLine1'), |
| 200 | + '$user->address->addressLine2' => $this->findNestedNullSafePropFetch($statements, 'user', 'addressLine2'), |
| 201 | + ]; |
| 202 | + |
| 203 | + foreach ($props as $prop => $node) { |
| 204 | + if ($node === null) { |
| 205 | + return new Failure( |
| 206 | + $this->getName(), |
| 207 | + "The $prop property should be accessed with the null safe operator" |
| 208 | + ); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + return new Success($this->getName()); |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * @param array<Node> $statements |
| 217 | + */ |
| 218 | + private function findNullSafePropFetch(array $statements, string $variableName, string $propName): ?Node |
| 219 | + { |
| 220 | + $nodes = $this->findAllNullSafePropertyFetch($statements, $variableName, $propName); |
| 221 | + return count($nodes) > 0 ? $nodes[0] : null; |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * @param array<Node> $statements |
| 226 | + * @return array<Node> |
| 227 | + */ |
| 228 | + private function findAllNullSafePropertyFetch(array $statements, string $variableName, string $propName): array |
| 229 | + { |
| 230 | + return (new NodeFinder())->find($statements, function (Node $node) use ($variableName, $propName) { |
| 231 | + return $node instanceof NullsafePropertyFetch |
| 232 | + && $node->var instanceof Variable |
| 233 | + && $node->var->name === $variableName |
| 234 | + && $node->name instanceof Identifier |
| 235 | + && $node->name->name === $propName; |
| 236 | + }); |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * @param array<Node> $statements |
| 241 | + */ |
| 242 | + private function findNestedNullSafePropFetch(array $statements, string $variableName, string $propName): ?Node |
| 243 | + { |
| 244 | + return (new NodeFinder())->findFirst($statements, function (Node $node) use ($variableName, $propName) { |
| 245 | + return $node instanceof NullsafePropertyFetch |
| 246 | + && $node->var instanceof NullsafePropertyFetch |
| 247 | + && $node->var->var instanceof Variable |
| 248 | + && $node->var->var->name === $variableName |
| 249 | + && $node->name instanceof Identifier |
| 250 | + && $node->name->name === $propName; |
| 251 | + }); |
| 252 | + } |
| 253 | + |
| 254 | + public function getFilesToCompare(): array |
| 255 | + { |
| 256 | + return [ |
| 257 | + 'users.csv' |
| 258 | + ]; |
| 259 | + } |
| 260 | +} |
0 commit comments