|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Type\Php; |
| 4 | + |
| 5 | +use PhpParser\Node\Expr\FuncCall; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\Reflection\FunctionReflection; |
| 8 | +use PHPStan\Type\BenevolentUnionType; |
| 9 | +use PHPStan\Type\DynamicFunctionReturnTypeExtension; |
| 10 | +use PHPStan\Type\FloatType; |
| 11 | +use PHPStan\Type\IntegerType; |
| 12 | +use PHPStan\Type\MixedType; |
| 13 | +use PHPStan\Type\ObjectWithoutClassType; |
| 14 | +use PHPStan\Type\Type; |
| 15 | +use PHPStan\Type\TypeCombinator; |
| 16 | + |
| 17 | +class PowFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension |
| 18 | +{ |
| 19 | + |
| 20 | + public function isFunctionSupported(FunctionReflection $functionReflection): bool |
| 21 | + { |
| 22 | + return $functionReflection->getName() === 'pow'; |
| 23 | + } |
| 24 | + |
| 25 | + public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type |
| 26 | + { |
| 27 | + $defaultReturnType = new BenevolentUnionType([ |
| 28 | + new FloatType(), |
| 29 | + new IntegerType(), |
| 30 | + ]); |
| 31 | + if (count($functionCall->args) < 2) { |
| 32 | + return $defaultReturnType; |
| 33 | + } |
| 34 | + |
| 35 | + $firstArgType = $scope->getType($functionCall->args[0]->value); |
| 36 | + $secondArgType = $scope->getType($functionCall->args[1]->value); |
| 37 | + if ($firstArgType instanceof MixedType || $secondArgType instanceof MixedType) { |
| 38 | + return $defaultReturnType; |
| 39 | + } |
| 40 | + |
| 41 | + $object = new ObjectWithoutClassType(); |
| 42 | + if ( |
| 43 | + !$object->isSuperTypeOf($firstArgType)->no() |
| 44 | + || !$object->isSuperTypeOf($secondArgType)->no() |
| 45 | + ) { |
| 46 | + return TypeCombinator::union($firstArgType, $secondArgType); |
| 47 | + } |
| 48 | + |
| 49 | + return $defaultReturnType; |
| 50 | + } |
| 51 | + |
| 52 | +} |
0 commit comments