|
| 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\Constant\ConstantStringType; |
| 9 | +use PHPStan\Type\DynamicFunctionReturnTypeExtension; |
| 10 | +use PHPStan\Type\IntersectionType; |
| 11 | +use PHPStan\Type\ResourceType; |
| 12 | +use PHPStan\Type\Type; |
| 13 | +use PHPStan\Type\TypeCombinator; |
| 14 | +use PHPStan\Type\TypeTraverser; |
| 15 | +use PHPStan\Type\UnionType; |
| 16 | +use function count; |
| 17 | + |
| 18 | +class GettypeFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension |
| 19 | +{ |
| 20 | + |
| 21 | + public function isFunctionSupported(FunctionReflection $functionReflection): bool |
| 22 | + { |
| 23 | + return $functionReflection->getName() === 'gettype'; |
| 24 | + } |
| 25 | + |
| 26 | + public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type |
| 27 | + { |
| 28 | + if (count($functionCall->getArgs()) < 1) { |
| 29 | + return null; |
| 30 | + } |
| 31 | + |
| 32 | + $valueType = $scope->getType($functionCall->getArgs()[0]->value); |
| 33 | + |
| 34 | + return TypeTraverser::map($valueType, static function (Type $valueType, callable $traverse): Type { |
| 35 | + if ($valueType instanceof UnionType || $valueType instanceof IntersectionType) { |
| 36 | + return $traverse($valueType); |
| 37 | + } |
| 38 | + |
| 39 | + if ($valueType->isString()->yes()) { |
| 40 | + return new ConstantStringType('string'); |
| 41 | + } |
| 42 | + if ($valueType->isArray()->yes()) { |
| 43 | + return new ConstantStringType('array'); |
| 44 | + } |
| 45 | + |
| 46 | + if ($valueType->isBoolean()->yes()) { |
| 47 | + return new ConstantStringType('boolean'); |
| 48 | + } |
| 49 | + |
| 50 | + $resource = new ResourceType(); |
| 51 | + if ($resource->isSuperTypeOf($valueType)->yes()) { |
| 52 | + return new UnionType([ |
| 53 | + new ConstantStringType('resource'), |
| 54 | + new ConstantStringType('resource (closed)'), |
| 55 | + ]); |
| 56 | + } |
| 57 | + |
| 58 | + if ($valueType->isInteger()->yes()) { |
| 59 | + return new ConstantStringType('integer'); |
| 60 | + } |
| 61 | + |
| 62 | + if ($valueType->isFloat()->yes()) { |
| 63 | + // for historical reasons "double" is returned in case of a float, and not simply "float" |
| 64 | + return new ConstantStringType('double'); |
| 65 | + } |
| 66 | + |
| 67 | + if ($valueType->isNull()->yes()) { |
| 68 | + return new ConstantStringType('NULL'); |
| 69 | + } |
| 70 | + |
| 71 | + if ($valueType->isObject()->yes()) { |
| 72 | + return new ConstantStringType('object'); |
| 73 | + } |
| 74 | + |
| 75 | + return TypeCombinator::union( |
| 76 | + new ConstantStringType('string'), |
| 77 | + new ConstantStringType('array'), |
| 78 | + new ConstantStringType('boolean'), |
| 79 | + new ConstantStringType('resource'), |
| 80 | + new ConstantStringType('resource (closed)'), |
| 81 | + new ConstantStringType('integer'), |
| 82 | + new ConstantStringType('double'), |
| 83 | + new ConstantStringType('NULL'), |
| 84 | + new ConstantStringType('object'), |
| 85 | + new ConstantStringType('unknown type'), |
| 86 | + ); |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments