|
27 | 27 | use PhpParser\Node\Stmt\Function_; |
28 | 28 | use PhpParser\NodeFinder; |
29 | 29 | use PHPStan\Analyser\ExprHandler\Helper\NullsafeShortCircuitingHelper; |
| 30 | +use PHPStan\Analyser\ExprHandler\Helper\VoidToNullTypeTransfomer; |
30 | 31 | use PHPStan\Analyser\Traverser\TransformStaticTypeTraverser; |
31 | | -use PHPStan\Analyser\Traverser\VoidToNullTraverser; |
32 | 32 | use PHPStan\DependencyInjection\Container; |
33 | 33 | use PHPStan\Node\ExecutionEndNode; |
34 | 34 | use PHPStan\Node\Expr\AlwaysRememberedExpr; |
|
87 | 87 | use PHPStan\TrinaryLogic; |
88 | 88 | use PHPStan\Type\Accessory\AccessoryArrayListType; |
89 | 89 | use PHPStan\Type\Accessory\HasOffsetValueType; |
90 | | -use PHPStan\Type\Accessory\HasPropertyType; |
91 | 90 | use PHPStan\Type\Accessory\NonEmptyArrayType; |
92 | 91 | use PHPStan\Type\Accessory\OversizedArrayType; |
93 | 92 | use PHPStan\Type\ArrayType; |
@@ -1149,39 +1148,9 @@ private function resolveType(string $exprString, Expr $node): Type |
1149 | 1148 | } |
1150 | 1149 | } |
1151 | 1150 |
|
1152 | | - if ($node instanceof FuncCall) { |
1153 | | - return $this->getFunctionCallType($node); |
1154 | | - } |
1155 | | - |
1156 | 1151 | return new MixedType(); |
1157 | 1152 | } |
1158 | 1153 |
|
1159 | | - private function getDynamicFunctionReturnType(FuncCall $normalizedNode, FunctionReflection $functionReflection): ?Type |
1160 | | - { |
1161 | | - foreach ($this->dynamicReturnTypeExtensionRegistry->getDynamicFunctionReturnTypeExtensions($functionReflection) as $dynamicFunctionReturnTypeExtension) { |
1162 | | - $resolvedType = $dynamicFunctionReturnTypeExtension->getTypeFromFunctionCall( |
1163 | | - $functionReflection, |
1164 | | - $normalizedNode, |
1165 | | - $this, |
1166 | | - ); |
1167 | | - |
1168 | | - if ($resolvedType !== null) { |
1169 | | - return $resolvedType; |
1170 | | - } |
1171 | | - } |
1172 | | - |
1173 | | - return null; |
1174 | | - } |
1175 | | - |
1176 | | - private function transformVoidToNull(Type $type, Node $node): Type |
1177 | | - { |
1178 | | - if ($node->getAttribute(self::KEEP_VOID_ATTRIBUTE_NAME) === true) { |
1179 | | - return $type; |
1180 | | - } |
1181 | | - |
1182 | | - return TypeTraverser::map($type, new VoidToNullTraverser()); |
1183 | | - } |
1184 | | - |
1185 | 1154 | /** |
1186 | 1155 | * @param callable(Type): ?bool $typeCallback |
1187 | 1156 | */ |
@@ -4628,7 +4597,7 @@ private function methodCallReturnType(Type $typeWithMethod, string $methodName, |
4628 | 4597 | $normalizedMethodCall = ArgumentsNormalizer::reorderStaticCallArguments($parametersAcceptor, $methodCall); |
4629 | 4598 | } |
4630 | 4599 | if ($normalizedMethodCall === null) { |
4631 | | - return $this->transformVoidToNull($parametersAcceptor->getReturnType(), $methodCall); |
| 4600 | + return VoidToNullTypeTransfomer::transform($parametersAcceptor->getReturnType(), $methodCall); |
4632 | 4601 | } |
4633 | 4602 |
|
4634 | 4603 | $resolvedTypes = []; |
@@ -4667,10 +4636,10 @@ private function methodCallReturnType(Type $typeWithMethod, string $methodName, |
4667 | 4636 | } |
4668 | 4637 |
|
4669 | 4638 | if (count($resolvedTypes) > 0) { |
4670 | | - return $this->transformVoidToNull(TypeCombinator::union(...$resolvedTypes), $methodCall); |
| 4639 | + return VoidToNullTypeTransfomer::transform(TypeCombinator::union(...$resolvedTypes), $methodCall); |
4671 | 4640 | } |
4672 | 4641 |
|
4673 | | - return $this->transformVoidToNull($parametersAcceptor->getReturnType(), $methodCall); |
| 4642 | + return VoidToNullTypeTransfomer::transform($parametersAcceptor->getReturnType(), $methodCall); |
4674 | 4643 | } |
4675 | 4644 |
|
4676 | 4645 | /** |
@@ -5227,105 +5196,6 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu |
5227 | 5196 | ); |
5228 | 5197 | } |
5229 | 5198 |
|
5230 | | - private function getFunctionCallType(FuncCall $node): Type |
5231 | | - { |
5232 | | - if ($node->name instanceof Expr) { |
5233 | | - $calledOnType = $this->getType($node->name); |
5234 | | - if ($calledOnType->isCallable()->no()) { |
5235 | | - return new ErrorType(); |
5236 | | - } |
5237 | | - |
5238 | | - $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs( |
5239 | | - $this, |
5240 | | - $node->getArgs(), |
5241 | | - $calledOnType->getCallableParametersAcceptors($this), |
5242 | | - null, |
5243 | | - ); |
5244 | | - |
5245 | | - $functionName = null; |
5246 | | - if ($node->name instanceof String_) { |
5247 | | - /** @var non-empty-string $name */ |
5248 | | - $name = $node->name->value; |
5249 | | - $functionName = new Name($name); |
5250 | | - } elseif ( |
5251 | | - $node->name instanceof FuncCall |
5252 | | - && $node->name->name instanceof Name |
5253 | | - && $node->name->isFirstClassCallable() |
5254 | | - ) { |
5255 | | - $functionName = $node->name->name; |
5256 | | - } |
5257 | | - |
5258 | | - $normalizedNode = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node); |
5259 | | - if ($normalizedNode !== null && $functionName !== null && $this->reflectionProvider->hasFunction($functionName, $this)) { |
5260 | | - $functionReflection = $this->reflectionProvider->getFunction($functionName, $this); |
5261 | | - $resolvedType = $this->getDynamicFunctionReturnType($normalizedNode, $functionReflection); |
5262 | | - if ($resolvedType !== null) { |
5263 | | - return $resolvedType; |
5264 | | - } |
5265 | | - } |
5266 | | - |
5267 | | - return $parametersAcceptor->getReturnType(); |
5268 | | - } |
5269 | | - |
5270 | | - if (!$this->reflectionProvider->hasFunction($node->name, $this)) { |
5271 | | - return new ErrorType(); |
5272 | | - } |
5273 | | - |
5274 | | - $functionReflection = $this->reflectionProvider->getFunction($node->name, $this); |
5275 | | - if ($this->nativeTypesPromoted) { |
5276 | | - return ParametersAcceptorSelector::combineAcceptors($functionReflection->getVariants())->getNativeReturnType(); |
5277 | | - } |
5278 | | - |
5279 | | - if ($functionReflection->getName() === 'call_user_func') { |
5280 | | - $result = ArgumentsNormalizer::reorderCallUserFuncArguments($node, $this); |
5281 | | - if ($result !== null) { |
5282 | | - [, $innerFuncCall] = $result; |
5283 | | - |
5284 | | - return $this->getType($innerFuncCall); |
5285 | | - } |
5286 | | - } |
5287 | | - |
5288 | | - $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs( |
5289 | | - $this, |
5290 | | - $node->getArgs(), |
5291 | | - $functionReflection->getVariants(), |
5292 | | - $functionReflection->getNamedArgumentsVariants(), |
5293 | | - ); |
5294 | | - $normalizedNode = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node); |
5295 | | - if ($normalizedNode !== null) { |
5296 | | - if ($functionReflection->getName() === 'clone' && count($normalizedNode->getArgs()) > 0) { |
5297 | | - $cloneType = $this->getType(new Expr\Clone_($normalizedNode->getArgs()[0]->value)); |
5298 | | - if (count($normalizedNode->getArgs()) === 2) { |
5299 | | - $propertiesType = $this->getType($normalizedNode->getArgs()[1]->value); |
5300 | | - if ($propertiesType->isConstantArray()->yes()) { |
5301 | | - $constantArrays = $propertiesType->getConstantArrays(); |
5302 | | - if (count($constantArrays) === 1) { |
5303 | | - $accessories = []; |
5304 | | - foreach ($constantArrays[0]->getKeyTypes() as $keyType) { |
5305 | | - $constantKeyTypes = $keyType->getConstantScalarValues(); |
5306 | | - if (count($constantKeyTypes) !== 1) { |
5307 | | - return $cloneType; |
5308 | | - } |
5309 | | - $accessories[] = new HasPropertyType((string) $constantKeyTypes[0]); |
5310 | | - } |
5311 | | - if (count($accessories) > 0 && count($accessories) <= 16) { |
5312 | | - return TypeCombinator::intersect($cloneType, ...$accessories); |
5313 | | - } |
5314 | | - } |
5315 | | - } |
5316 | | - } |
5317 | | - |
5318 | | - return $cloneType; |
5319 | | - } |
5320 | | - $resolvedType = $this->getDynamicFunctionReturnType($normalizedNode, $functionReflection); |
5321 | | - if ($resolvedType !== null) { |
5322 | | - return $resolvedType; |
5323 | | - } |
5324 | | - } |
5325 | | - |
5326 | | - return $this->transformVoidToNull($parametersAcceptor->getReturnType(), $node); |
5327 | | - } |
5328 | | - |
5329 | 5199 | private function getStaticCallType(Expr\StaticCall $node): ?Type |
5330 | 5200 | { |
5331 | 5201 | if ($node->name instanceof Node\Identifier) { |
|
0 commit comments