|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\Symfony; |
| 4 | + |
| 5 | +use PhpParser\Node\Expr\MethodCall; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Reflection\ClassReflection; |
| 8 | +use PHPStan\Reflection\MethodReflection; |
| 9 | +use PHPStan\Reflection\ReflectionProvider; |
| 10 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 11 | +use PHPStan\Type\NullType; |
| 12 | +use PHPStan\Type\ObjectType; |
| 13 | +use PHPStan\Type\Type; |
| 14 | +use PHPStan\Type\TypeCombinator; |
| 15 | +use function str_contains; |
| 16 | +use function strrpos; |
| 17 | +use function substr_replace; |
| 18 | + |
| 19 | +class ExtensionGetConfigurationReturnTypeExtension implements DynamicMethodReturnTypeExtension |
| 20 | +{ |
| 21 | + |
| 22 | + /** @var ReflectionProvider */ |
| 23 | + private $reflectionProvider; |
| 24 | + |
| 25 | + public function __construct(ReflectionProvider $reflectionProvider) |
| 26 | + { |
| 27 | + $this->reflectionProvider = $reflectionProvider; |
| 28 | + } |
| 29 | + |
| 30 | + public function getClass(): string |
| 31 | + { |
| 32 | + return 'Symfony\Component\DependencyInjection\Extension\Extension'; |
| 33 | + } |
| 34 | + |
| 35 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 36 | + { |
| 37 | + return $methodReflection->getName() === 'getConfiguration' |
| 38 | + && $methodReflection->getDeclaringClass()->getName() === 'Symfony\Component\DependencyInjection\Extension\Extension'; |
| 39 | + } |
| 40 | + |
| 41 | + public function getTypeFromMethodCall( |
| 42 | + MethodReflection $methodReflection, |
| 43 | + MethodCall $methodCall, |
| 44 | + Scope $scope |
| 45 | + ): ?Type |
| 46 | + { |
| 47 | + $types = []; |
| 48 | + $extensionType = $scope->getType($methodCall->var); |
| 49 | + $classes = $extensionType->getObjectClassNames(); |
| 50 | + |
| 51 | + foreach ($classes as $extensionName) { |
| 52 | + if (str_contains($extensionName, "\0")) { |
| 53 | + $types[] = new NullType(); |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + $lastBackslash = strrpos($extensionName, '\\'); |
| 58 | + if ($lastBackslash === false) { |
| 59 | + $types[] = new NullType(); |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + $configurationName = substr_replace($extensionName, '\Configuration', $lastBackslash); |
| 64 | + if (!$this->reflectionProvider->hasClass($configurationName)) { |
| 65 | + $types[] = new NullType(); |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + $reflection = $this->reflectionProvider->getClass($configurationName); |
| 70 | + if ($this->hasRequiredConstructor($reflection)) { |
| 71 | + $types[] = new NullType(); |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + $types[] = new ObjectType($configurationName); |
| 76 | + } |
| 77 | + |
| 78 | + return TypeCombinator::union(...$types); |
| 79 | + } |
| 80 | + |
| 81 | + private function hasRequiredConstructor(ClassReflection $class): bool |
| 82 | + { |
| 83 | + if (!$class->hasConstructor()) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + $constructor = $class->getConstructor(); |
| 88 | + foreach ($constructor->getVariants() as $variant) { |
| 89 | + $anyRequired = false; |
| 90 | + foreach ($variant->getParameters() as $parameter) { |
| 91 | + if (!$parameter->isOptional()) { |
| 92 | + $anyRequired = true; |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if (!$anyRequired) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return true; |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments