|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\Symfony; |
| 4 | + |
| 5 | +use PhpParser\Node\Expr\StaticCall; |
| 6 | +use PhpParser\Node\Name; |
| 7 | +use PHPStan\Analyser\Scope; |
| 8 | +use PHPStan\Reflection\MethodReflection; |
| 9 | +use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; |
| 10 | +use PHPStan\Type\Type; |
| 11 | +use PHPStan\Type\TypeUtils; |
| 12 | + |
| 13 | +final class TreeBuilderDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension |
| 14 | +{ |
| 15 | + |
| 16 | + private const MAPPING = [ |
| 17 | + 'variable' => 'Symfony\Component\Config\Definition\Builder\VariableNodeDefinition', |
| 18 | + 'scalar' => 'Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition', |
| 19 | + 'boolean' => 'Symfony\Component\Config\Definition\Builder\BooleanNodeDefinition', |
| 20 | + 'integer' => 'Symfony\Component\Config\Definition\Builder\IntegerNodeDefinition', |
| 21 | + 'float' => 'Symfony\Component\Config\Definition\Builder\FloatNodeDefinition', |
| 22 | + 'array' => 'Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition', |
| 23 | + 'enum' => 'Symfony\Component\Config\Definition\Builder\EnumNodeDefinition', |
| 24 | + ]; |
| 25 | + |
| 26 | + public function getClass(): string |
| 27 | + { |
| 28 | + return 'Symfony\Component\Config\Definition\Builder\TreeBuilder'; |
| 29 | + } |
| 30 | + |
| 31 | + public function isStaticMethodSupported(MethodReflection $methodReflection): bool |
| 32 | + { |
| 33 | + return $methodReflection->getName() === '__construct'; |
| 34 | + } |
| 35 | + |
| 36 | + public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): Type |
| 37 | + { |
| 38 | + if (!$methodCall->class instanceof Name) { |
| 39 | + throw new \PHPStan\ShouldNotHappenException(); |
| 40 | + } |
| 41 | + |
| 42 | + $className = $scope->resolveName($methodCall->class); |
| 43 | + |
| 44 | + $type = 'array'; |
| 45 | + |
| 46 | + if (isset($methodCall->args[1])) { |
| 47 | + $argStrings = TypeUtils::getConstantStrings($scope->getType($methodCall->args[1]->value)); |
| 48 | + if (count($argStrings) === 1 && isset(self::MAPPING[$argStrings[0]->getValue()])) { |
| 49 | + $type = $argStrings[0]->getValue(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + return new TreeBuilderType($className, self::MAPPING[$type]); |
| 54 | + } |
| 55 | + |
| 56 | +} |
0 commit comments