Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 14 additions & 21 deletions src/Analyser/ExprHandler/FuncCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,28 +484,21 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
private function getFunctionThrowPoint(
FunctionReflection $functionReflection,
?ParametersAcceptor $parametersAcceptor,
FuncCall $funcCall,
FuncCall $normalizedFuncCall,
MutatingScope $scope,
): ?InternalThrowPoint
{
$normalizedFuncCall = $funcCall;
if ($parametersAcceptor !== null) {
$normalizedFuncCall = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $funcCall);
}

if ($normalizedFuncCall !== null) {
foreach ($this->dynamicThrowTypeExtensionProvider->getDynamicFunctionThrowTypeExtensions() as $extension) {
if (!$extension->isFunctionSupported($functionReflection)) {
continue;
}

$throwType = $extension->getThrowTypeFromFunctionCall($functionReflection, $normalizedFuncCall, $scope);
if ($throwType === null) {
return null;
}
foreach ($this->dynamicThrowTypeExtensionProvider->getDynamicFunctionThrowTypeExtensions() as $extension) {
if (!$extension->isFunctionSupported($functionReflection)) {
continue;
}

return InternalThrowPoint::createExplicit($scope, $throwType, $funcCall, false);
$throwType = $extension->getThrowTypeFromFunctionCall($functionReflection, $normalizedFuncCall, $scope);
if ($throwType === null) {
return null;
}

return InternalThrowPoint::createExplicit($scope, $throwType, $normalizedFuncCall, false);
}

$throwType = $functionReflection->getThrowType();
Expand All @@ -518,7 +511,7 @@ private function getFunctionThrowPoint(

if ($throwType !== null) {
if (!$throwType->isVoid()->yes()) {
return InternalThrowPoint::createExplicit($scope, $throwType, $funcCall, true);
return InternalThrowPoint::createExplicit($scope, $throwType, $normalizedFuncCall, true);
}
} elseif ($this->implicitThrows) {
$requiredParameters = null;
Expand All @@ -536,11 +529,11 @@ private function getFunctionThrowPoint(
!$functionReflection->isBuiltin()
|| $requiredParameters === null
|| $requiredParameters > 0
|| count($funcCall->getArgs()) > 0
|| count($normalizedFuncCall->getArgs()) > 0
) {
$functionReturnedType = $scope->getType($funcCall);
$functionReturnedType = $scope->getType($normalizedFuncCall);
if (!(new ObjectType(Throwable::class))->isSuperTypeOf($functionReturnedType)->yes()) {
return InternalThrowPoint::createImplicit($scope, $funcCall);
return InternalThrowPoint::createImplicit($scope, $normalizedFuncCall);
}
}
}
Expand Down
33 changes: 15 additions & 18 deletions src/Analyser/ExprHandler/MethodCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$scope = $argsResult->getScope();

if ($methodReflection !== null) {
$methodThrowPoint = $this->getMethodThrowPoint($methodReflection, $parametersAcceptor, $expr, $scope);
$methodThrowPoint = $this->getMethodThrowPoint($methodReflection, $parametersAcceptor, $normalizedExpr, $scope);
if ($methodThrowPoint !== null) {
$throwPoints[] = $methodThrowPoint;
}
Expand Down Expand Up @@ -235,29 +235,26 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
return $result;
}

private function getMethodThrowPoint(MethodReflection $methodReflection, ParametersAcceptor $parametersAcceptor, MethodCall $methodCall, MutatingScope $scope): ?InternalThrowPoint
private function getMethodThrowPoint(MethodReflection $methodReflection, ParametersAcceptor $parametersAcceptor, MethodCall $normalizedMethodCall, MutatingScope $scope): ?InternalThrowPoint
{
$normalizedMethodCall = ArgumentsNormalizer::reorderMethodArguments($parametersAcceptor, $methodCall);
if ($normalizedMethodCall !== null) {
foreach ($this->dynamicThrowTypeExtensionProvider->getDynamicMethodThrowTypeExtensions() as $extension) {
if (!$extension->isMethodSupported($methodReflection)) {
continue;
}

$throwType = $extension->getThrowTypeFromMethodCall($methodReflection, $normalizedMethodCall, $scope);
if ($throwType === null) {
return null;
}
foreach ($this->dynamicThrowTypeExtensionProvider->getDynamicMethodThrowTypeExtensions() as $extension) {
if (!$extension->isMethodSupported($methodReflection)) {
continue;
}

return InternalThrowPoint::createExplicit($scope, $throwType, $methodCall, false);
$throwType = $extension->getThrowTypeFromMethodCall($methodReflection, $normalizedMethodCall, $scope);
if ($throwType === null) {
return null;
}

return InternalThrowPoint::createExplicit($scope, $throwType, $normalizedMethodCall, false);
}

if (
in_array($methodReflection->getName(), ['invoke', 'invokeArgs'], true)
&& in_array($methodReflection->getDeclaringClass()->getName(), [ReflectionMethod::class, ReflectionFunction::class], true)
) {
return InternalThrowPoint::createImplicit($scope, $methodCall);
return InternalThrowPoint::createImplicit($scope, $normalizedMethodCall);
}

$throwType = $methodReflection->getThrowType();
Expand All @@ -270,12 +267,12 @@ private function getMethodThrowPoint(MethodReflection $methodReflection, Paramet

if ($throwType !== null) {
if (!$throwType->isVoid()->yes()) {
return InternalThrowPoint::createExplicit($scope, $throwType, $methodCall, true);
return InternalThrowPoint::createExplicit($scope, $throwType, $normalizedMethodCall, true);
}
} elseif ($this->implicitThrows) {
$methodReturnedType = $scope->getType($methodCall);
$methodReturnedType = $scope->getType($normalizedMethodCall);
if (!(new ObjectType(Throwable::class))->isSuperTypeOf($methodReturnedType)->yes()) {
return InternalThrowPoint::createImplicit($scope, $methodCall);
return InternalThrowPoint::createImplicit($scope, $normalizedMethodCall);
}
}

Expand Down
32 changes: 14 additions & 18 deletions src/Analyser/ExprHandler/StaticCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use PHPStan\Node\Expr\PossiblyImpureCallExpr;
use PHPStan\Reflection\Callables\SimpleImpurePoint;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\ErrorType;
use PHPStan\Type\MixedType;
Expand Down Expand Up @@ -199,7 +198,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
$scopeFunction = $scope->getFunction();

if ($methodReflection !== null) {
$methodThrowPoint = $this->getStaticMethodThrowPoint($methodReflection, $parametersAcceptor, $expr, $scope);
$methodThrowPoint = $this->getStaticMethodThrowPoint($methodReflection, $normalizedExpr, $scope);
if ($methodThrowPoint !== null) {
$throwPoints[] = $methodThrowPoint;
}
Expand Down Expand Up @@ -269,33 +268,30 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex
);
}

private function getStaticMethodThrowPoint(MethodReflection $methodReflection, ParametersAcceptor $parametersAcceptor, StaticCall $methodCall, MutatingScope $scope): ?InternalThrowPoint
private function getStaticMethodThrowPoint(MethodReflection $methodReflection, StaticCall $normalizedMethodCall, MutatingScope $scope): ?InternalThrowPoint
{
$normalizedMethodCall = ArgumentsNormalizer::reorderStaticCallArguments($parametersAcceptor, $methodCall);
if ($normalizedMethodCall !== null) {
foreach ($this->dynamicThrowTypeExtensionProvider->getDynamicStaticMethodThrowTypeExtensions() as $extension) {
if (!$extension->isStaticMethodSupported($methodReflection)) {
continue;
}

$throwType = $extension->getThrowTypeFromStaticMethodCall($methodReflection, $normalizedMethodCall, $scope);
if ($throwType === null) {
return null;
}
foreach ($this->dynamicThrowTypeExtensionProvider->getDynamicStaticMethodThrowTypeExtensions() as $extension) {
if (!$extension->isStaticMethodSupported($methodReflection)) {
continue;
}

return InternalThrowPoint::createExplicit($scope, $throwType, $methodCall, false);
$throwType = $extension->getThrowTypeFromStaticMethodCall($methodReflection, $normalizedMethodCall, $scope);
if ($throwType === null) {
return null;
}

return InternalThrowPoint::createExplicit($scope, $throwType, $normalizedMethodCall, false);
}

if ($methodReflection->getThrowType() !== null) {
$throwType = $methodReflection->getThrowType();
if (!$throwType->isVoid()->yes()) {
return InternalThrowPoint::createExplicit($scope, $throwType, $methodCall, true);
return InternalThrowPoint::createExplicit($scope, $throwType, $normalizedMethodCall, true);
}
} elseif ($this->implicitThrows) {
$methodReturnedType = $scope->getType($methodCall);
$methodReturnedType = $scope->getType($normalizedMethodCall);
if (!(new ObjectType(Throwable::class))->isSuperTypeOf($methodReturnedType)->yes()) {
return InternalThrowPoint::createImplicit($scope, $methodCall);
return InternalThrowPoint::createImplicit($scope, $normalizedMethodCall);
}
}

Expand Down
Loading