|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\Nette; |
| 4 | + |
| 5 | +use PhpParser\Node\Expr\MethodCall; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Reflection\MethodReflection; |
| 8 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 9 | +use PHPStan\Type\ArrayType; |
| 10 | +use PHPStan\Type\Constant\ConstantStringType; |
| 11 | +use PHPStan\Type\DynamicMethodReturnTypeExtension; |
| 12 | +use PHPStan\Type\MixedType; |
| 13 | +use PHPStan\Type\NullType; |
| 14 | +use PHPStan\Type\ObjectType; |
| 15 | +use PHPStan\Type\StringType; |
| 16 | +use PHPStan\Type\Type; |
| 17 | + |
| 18 | +class FormContainerUnsafeValuesDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension |
| 19 | +{ |
| 20 | + |
| 21 | + public function getClass(): string |
| 22 | + { |
| 23 | + return 'Nette\Forms\Container'; |
| 24 | + } |
| 25 | + |
| 26 | + public function isMethodSupported(MethodReflection $methodReflection): bool |
| 27 | + { |
| 28 | + return $methodReflection->getName() === 'getUnsafeValues'; |
| 29 | + } |
| 30 | + |
| 31 | + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type |
| 32 | + { |
| 33 | + if (count($methodCall->args) === 0) { |
| 34 | + return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); |
| 35 | + } |
| 36 | + |
| 37 | + $arg = $methodCall->args[0]->value; |
| 38 | + $scopedType = $scope->getType($arg); |
| 39 | + if ($scopedType instanceof NullType) { |
| 40 | + return new ObjectType('Nette\Utils\ArrayHash'); |
| 41 | + } |
| 42 | + |
| 43 | + if ($scopedType instanceof ConstantStringType && $scopedType->getValue() === 'array') { |
| 44 | + return new ArrayType(new StringType(), new MixedType()); |
| 45 | + } |
| 46 | + |
| 47 | + return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); |
| 48 | + } |
| 49 | + |
| 50 | +} |
0 commit comments