|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | + |
| 4 | +namespace TheCodingMachine\Safe\PHPStan\Type\Php; |
| 5 | + |
| 6 | +use PhpParser\Node\Expr\FuncCall; |
| 7 | +use PHPStan\Analyser\Scope; |
| 8 | +use PHPStan\Reflection\FunctionReflection; |
| 9 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 10 | +use PHPStan\Type\ArrayType; |
| 11 | +use PHPStan\Type\DynamicFunctionReturnTypeExtension; |
| 12 | +use PHPStan\Type\MixedType; |
| 13 | +use PHPStan\Type\StringType; |
| 14 | +use PHPStan\Type\Type; |
| 15 | +use PHPStan\Type\TypeCombinator; |
| 16 | +use PHPStan\Type\TypeUtils; |
| 17 | + |
| 18 | +class ReplaceSafeFunctionsDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension |
| 19 | +{ |
| 20 | + |
| 21 | + /** @var array<string, int> */ |
| 22 | + private $functions = [ |
| 23 | + 'Safe\preg_replace' => 2, |
| 24 | + ]; |
| 25 | + |
| 26 | + public function isFunctionSupported(FunctionReflection $functionReflection): bool |
| 27 | + { |
| 28 | + return array_key_exists($functionReflection->getName(), $this->functions); |
| 29 | + } |
| 30 | + |
| 31 | + public function getTypeFromFunctionCall( |
| 32 | + FunctionReflection $functionReflection, |
| 33 | + FuncCall $functionCall, |
| 34 | + Scope $scope |
| 35 | + ): Type { |
| 36 | + $type = $this->getPreliminarilyResolvedTypeFromFunctionCall($functionReflection, $functionCall, $scope); |
| 37 | + |
| 38 | + $possibleTypes = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(); |
| 39 | + |
| 40 | + if (TypeCombinator::containsNull($possibleTypes)) { |
| 41 | + $type = TypeCombinator::addNull($type); |
| 42 | + } |
| 43 | + |
| 44 | + return $type; |
| 45 | + } |
| 46 | + |
| 47 | + private function getPreliminarilyResolvedTypeFromFunctionCall( |
| 48 | + FunctionReflection $functionReflection, |
| 49 | + FuncCall $functionCall, |
| 50 | + Scope $scope |
| 51 | + ): Type { |
| 52 | + $argumentPosition = $this->functions[$functionReflection->getName()]; |
| 53 | + if (count($functionCall->args) <= $argumentPosition) { |
| 54 | + return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(); |
| 55 | + } |
| 56 | + |
| 57 | + $subjectArgumentType = $scope->getType($functionCall->args[$argumentPosition]->value); |
| 58 | + $defaultReturnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(); |
| 59 | + if ($subjectArgumentType instanceof MixedType) { |
| 60 | + return TypeUtils::toBenevolentUnion($defaultReturnType); |
| 61 | + } |
| 62 | + $stringType = new StringType(); |
| 63 | + $arrayType = new ArrayType(new MixedType(), new MixedType()); |
| 64 | + |
| 65 | + $isStringSuperType = $stringType->isSuperTypeOf($subjectArgumentType); |
| 66 | + $isArraySuperType = $arrayType->isSuperTypeOf($subjectArgumentType); |
| 67 | + $compareSuperTypes = $isStringSuperType->compareTo($isArraySuperType); |
| 68 | + if ($compareSuperTypes === $isStringSuperType) { |
| 69 | + return $stringType; |
| 70 | + } elseif ($compareSuperTypes === $isArraySuperType) { |
| 71 | + if ($subjectArgumentType instanceof ArrayType) { |
| 72 | + return $subjectArgumentType->generalizeValues(); |
| 73 | + } |
| 74 | + return $subjectArgumentType; |
| 75 | + } |
| 76 | + |
| 77 | + return $defaultReturnType; |
| 78 | + } |
| 79 | +} |
0 commit comments