|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace ARiddlestone\PHPStanCakePHP2; |
| 6 | + |
| 7 | +use PHPStan\Reflection\ClassReflection; |
| 8 | +use PHPStan\Reflection\PropertiesClassReflectionExtension; |
| 9 | +use PHPStan\Reflection\PropertyReflection; |
| 10 | +use PHPStan\Reflection\ReflectionProvider; |
| 11 | + |
| 12 | +final class ComponentComponentsExtension implements |
| 13 | + PropertiesClassReflectionExtension |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var ReflectionProvider |
| 17 | + */ |
| 18 | + private $reflectionProvider; |
| 19 | + |
| 20 | + public function __construct(ReflectionProvider $reflectionProvider) |
| 21 | + { |
| 22 | + $this->reflectionProvider = $reflectionProvider; |
| 23 | + } |
| 24 | + |
| 25 | + public function hasProperty( |
| 26 | + ClassReflection $classReflection, |
| 27 | + string $propertyName |
| 28 | + ): bool { |
| 29 | + if (! $classReflection->is('Component')) { |
| 30 | + return false; |
| 31 | + } |
| 32 | + |
| 33 | + $className = $this->getClassName($propertyName); |
| 34 | + |
| 35 | + if (! $this->reflectionProvider->hasClass($className)) { |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + if ( |
| 40 | + ! $this->reflectionProvider |
| 41 | + ->getClass($className) |
| 42 | + ->is('Component') |
| 43 | + ) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + return true; |
| 48 | + } |
| 49 | + |
| 50 | + public function getProperty( |
| 51 | + ClassReflection $classReflection, |
| 52 | + string $propertyName |
| 53 | + ): PropertyReflection { |
| 54 | + return new PublicReadOnlyPropertyReflection( |
| 55 | + $this->getClassName($propertyName), |
| 56 | + $classReflection |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + private function getClassName(string $propertyName): string |
| 61 | + { |
| 62 | + return $propertyName . 'Component'; |
| 63 | + } |
| 64 | +} |
0 commit comments