|
4 | 4 |
|
5 | 5 | namespace PHPStanCakePHP2; |
6 | 6 |
|
7 | | -final class ClassComponentsExtension extends ClassPropertiesExtension |
| 7 | +use PhpParser\Node\Expr\Array_; |
| 8 | +use PhpParser\Node\Expr\ClassConstFetch; |
| 9 | +use PhpParser\Node\Name\FullyQualified; |
| 10 | +use PhpParser\Node\Scalar\String_; |
| 11 | +use PHPStan\Reflection\ClassReflection; |
| 12 | +use PHPStan\Reflection\PropertiesClassReflectionExtension; |
| 13 | +use PHPStan\Reflection\PropertyReflection; |
| 14 | +use PHPStan\Reflection\ReflectionProvider; |
| 15 | + |
| 16 | +final class ClassComponentsExtension implements PropertiesClassReflectionExtension |
8 | 17 | { |
9 | | - protected function getPropertyParentClassName(): string |
| 18 | + private ReflectionProvider $reflectionProvider; |
| 19 | + |
| 20 | + public function __construct(ReflectionProvider $reflectionProvider) |
10 | 21 | { |
11 | | - return 'Component'; |
| 22 | + $this->reflectionProvider = $reflectionProvider; |
| 23 | + } |
| 24 | + |
| 25 | + public function hasProperty(ClassReflection $classReflection, string $propertyName): bool |
| 26 | + { |
| 27 | + if (!array_filter($this->getContainingClassNames(), [$classReflection, 'is'])) { |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + $isDefinedInComponentsProperty = (bool) array_filter( |
| 32 | + $this->getDefinedComponentsAsList($classReflection), |
| 33 | + static fn (string $componentName): bool => $componentName === $propertyName |
| 34 | + ); |
| 35 | + |
| 36 | + if (!$isDefinedInComponentsProperty) { |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + $propertyClassName = $this->getClassNameFromPropertyName($propertyName); |
| 41 | + |
| 42 | + return $this->reflectionProvider->hasClass($propertyClassName) |
| 43 | + && $this->reflectionProvider->getClass($propertyClassName) |
| 44 | + ->is('Component'); |
| 45 | + } |
| 46 | + |
| 47 | + public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection |
| 48 | + { |
| 49 | + return new PublicReadOnlyPropertyReflection( |
| 50 | + $this->getClassNameFromPropertyName($propertyName), |
| 51 | + $classReflection |
| 52 | + ); |
12 | 53 | } |
13 | 54 |
|
14 | 55 | /** |
15 | 56 | * @return array<string> |
16 | 57 | */ |
17 | | - protected function getContainingClassNames(): array |
| 58 | + private function getContainingClassNames(): array |
18 | 59 | { |
19 | 60 | return [ |
20 | 61 | 'Controller', |
21 | 62 | 'Component', |
22 | 63 | ]; |
23 | 64 | } |
24 | 65 |
|
25 | | - protected function getClassNameFromPropertyName( |
26 | | - string $propertyName |
27 | | - ): string { |
28 | | - return $propertyName . 'Component'; |
| 66 | + private function getClassNameFromPropertyName(string $propertyName): string |
| 67 | + { |
| 68 | + return str_contains($propertyName, 'Component') ? $propertyName : $propertyName . 'Component'; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @return list<string> |
| 73 | + */ |
| 74 | + private function getDefinedComponentsAsList(ClassReflection $classReflection): array |
| 75 | + { |
| 76 | + $definedComponents = []; |
| 77 | + |
| 78 | + foreach (array_merge([$classReflection], $classReflection->getParents()) as $class) { |
| 79 | + if (!$class->hasProperty('components')) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + $defaultValue = $class->getNativeProperty('components') |
| 84 | + ->getNativeReflection() |
| 85 | + ->getDefaultValueExpression(); |
| 86 | + |
| 87 | + if (!$defaultValue instanceof Array_) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + foreach ($defaultValue->items as $item) { |
| 92 | + if ($item->value instanceof String_) { |
| 93 | + $definedComponents[] = $item->value->value; |
| 94 | + |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + if ($item->value instanceof ClassConstFetch && $item->value->class instanceof FullyQualified) { |
| 99 | + $definedComponents[] = $item->value->class->toString(); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return $definedComponents; |
29 | 105 | } |
30 | 106 | } |
0 commit comments