From d773467e0ba3c2479224f2aee956c3f1683d436c Mon Sep 17 00:00:00 2001 From: Tymoteusz Motylewski Date: Sat, 5 Mar 2022 22:40:18 +0100 Subject: [PATCH] Avoid phpstan throwing error when variable is passed to factory eg. Mage::getModel($modelName); --- src/Type/Mage/MethodReturnTypeDetector.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Type/Mage/MethodReturnTypeDetector.php b/src/Type/Mage/MethodReturnTypeDetector.php index 691fd8d..c5580db 100644 --- a/src/Type/Mage/MethodReturnTypeDetector.php +++ b/src/Type/Mage/MethodReturnTypeDetector.php @@ -7,7 +7,7 @@ use PhpParser\Node\Scalar\String_; use PHPStan\Analyser\Scope; use PHPStan\Reflection\MethodReflection; -use PHPStan\ShouldNotHappenException; +use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Type\ObjectType; use PHPStan\Type\Type; use PHPStanMagento1\Config\MagentoCore; @@ -32,15 +32,15 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method /** * @param MethodCall|\PhpParser\Node\Expr\StaticCall $methodCall - * @throws \PHPStan\ShouldNotHappenException */ protected function getTypeFromExpr(MethodReflection $methodReflection, $methodCall, Scope $scope): Type { - if (! isset($methodCall->args[0]) || ! $methodCall->args[0]->value instanceof String_) { - throw new ShouldNotHappenException("type:" . \get_class($methodCall->args[0]->value)); + $argument = $methodCall->getArgs()[0] ?? null; + if ($argument === null || ! $argument->value instanceof String_) { + return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); } - $modelName = $methodCall->args[0]->value->value; + $modelName = $argument->value->value; $modelClassName = $this->getMagentoClassName($modelName); return new ObjectType($modelClassName);