|
4 | 4 |
|
5 | 5 | namespace PhpSchool\PHP8Appreciate\Exercise;
|
6 | 6 |
|
| 7 | +use PhpParser\Node\Stmt; |
7 | 8 | use PhpParser\Node\Stmt\Class_;
|
8 | 9 | use PhpParser\NodeFinder;
|
9 | 10 | use PhpParser\Parser;
|
@@ -54,17 +55,18 @@ public function getArgs(): array
|
54 | 55 |
|
55 | 56 | public function check(Input $input): ResultInterface
|
56 | 57 | {
|
| 58 | + /** @var array<Stmt> $statements */ |
57 | 59 | $statements = $this->parser->parse((string) file_get_contents($input->getRequiredArgument('program')));
|
| 60 | + /** @var Class_|null $classNode */ |
| 61 | + $classNode = (new NodeFinder())->findFirstInstanceOf($statements, Class_::class); |
58 | 62 |
|
59 |
| - /** @var Class_|null $node */ |
60 |
| - $className = (new NodeFinder())->findFirstInstanceOf($statements, Class_::class)?->name->name; |
61 |
| - |
62 |
| - if (null === $className) { |
| 63 | + if (null === $classNode || null === $className = $classNode->name?->name) { |
63 | 64 | return Failure::fromNameAndReason($this->getName(), 'No class was found');
|
64 | 65 | }
|
65 | 66 |
|
66 | 67 | (static fn () => require $input->getRequiredArgument('program'))();
|
67 | 68 |
|
| 69 | + /** @var class-string $className */ |
68 | 70 | $reflectionClass = new ReflectionClass($className);
|
69 | 71 | $params = collect($reflectionClass->getMethod('__construct')->getParameters());
|
70 | 72 | $params = $params->flatMap(fn (\ReflectionParameter $prop) => [$prop->getName() => $prop]);
|
@@ -138,6 +140,7 @@ public function check(Input $input): ResultInterface
|
138 | 140 | ])
|
139 | 141 | ->getArrayCopy();
|
140 | 142 |
|
| 143 | + /** @var array{string?: string} $dataFailures */ |
141 | 144 | $dataFailures = array_filter([
|
142 | 145 | 'visitor' => $args['visitor'] !== $actual['visitor'],
|
143 | 146 | 'key' => $args['key'] !== $actual['key'],
|
@@ -169,7 +172,7 @@ private function getPropertyType(\ReflectionProperty $prop): string
|
169 | 172 | $type = $prop->getType();
|
170 | 173 |
|
171 | 174 | if (null === $type || !$type instanceof \ReflectionNamedType) {
|
172 |
| - throw new \RuntimeException('Invalid property "%s"', $prop->getName()); |
| 175 | + throw new \RuntimeException(sprintf('Invalid property "%s"', $prop->getName())); |
173 | 176 | }
|
174 | 177 | /** @var \ReflectionNamedType $type */
|
175 | 178 | return $type->getName();
|
|
0 commit comments