diff --git a/src/Analyser/ExprHandler/NewHandler.php b/src/Analyser/ExprHandler/NewHandler.php index fe7611f2bf1..52023313ede 100644 --- a/src/Analyser/ExprHandler/NewHandler.php +++ b/src/Analyser/ExprHandler/NewHandler.php @@ -217,19 +217,17 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex // the not-yet-stored New_ node, which would re-enter this handler. $objectClasses = $classResult->getType()->getObjectTypeOrClassStringObjectType()->getObjectClassNames(); if (count($objectClasses) === 1) { - $objectExprResult = $nodeScopeResolver->processExprNode($stmt, new New_(new Name($objectClasses[0]), attributes: [TemplateArgumentFrame::SYNTHETIC_SITE_ATTRIBUTE => true]), $scope, $storage, new NoopNodeCallback(), $context->enterDeep()->withoutTemplateArgumentResolution()); $className = $objectClasses[0]; - $additionalThrowPoints = $objectExprResult->getThrowPoints(); - } else { - $additionalThrowPoints = [InternalThrowPoint::createImplicit($scope, $expr)]; - } - - $throwPoints = array_merge($throwPoints, $additionalThrowPoints); - - if ($className !== null) { [$constructorReflection, $classReflection, $parametersAcceptor] = $this->processConstructorReflection($className, $expr); $deferredConstructorImpureIsDynamic = true; + + // a resolved constructor gets its throw point from the actual arguments below + if ($constructorReflection === null) { + $objectExprResult = $nodeScopeResolver->processExprNode($stmt, new New_(new Name($className), attributes: [TemplateArgumentFrame::SYNTHETIC_SITE_ATTRIBUTE => true]), $scope, $storage, new NoopNodeCallback(), $context->enterDeep()->withoutTemplateArgumentResolution()); + $throwPoints = array_merge($throwPoints, $objectExprResult->getThrowPoints()); + } } else { + $throwPoints[] = InternalThrowPoint::createImplicit($scope, $expr); $impurePoints[] = new ImpurePoint( $scope, $expr, diff --git a/src/Analyser/ExprHandler/StaticCallHandler.php b/src/Analyser/ExprHandler/StaticCallHandler.php index 3c93f15ae2a..1aa9abc7861 100644 --- a/src/Analyser/ExprHandler/StaticCallHandler.php +++ b/src/Analyser/ExprHandler/StaticCallHandler.php @@ -236,7 +236,8 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $scope = $nameResult->getScope(); } - if ($expr->class instanceof Expr) { + // a resolved method gets its throw point from the actual arguments below + if ($expr->class instanceof Expr && $methodReflection === null) { // the class expr was processed above as the receiver; read its // already-computed result instead of re-walking via Scope::getType(). $objectClasses = $classResult->getType()->getObjectClassNames(); diff --git a/tests/PHPStan/Analyser/data/dynamic-method-throw-type-extension.php b/tests/PHPStan/Analyser/data/dynamic-method-throw-type-extension.php index 9d4aaf48a34..755245731ed 100644 --- a/tests/PHPStan/Analyser/data/dynamic-method-throw-type-extension.php +++ b/tests/PHPStan/Analyser/data/dynamic-method-throw-type-extension.php @@ -42,7 +42,8 @@ class StaticMethodThrowTypeExtension implements DynamicStaticMethodThrowTypeExte public function isStaticMethodSupported(MethodReflection $methodReflection): bool { - return $methodReflection->getDeclaringClass()->getName() === Foo::class && $methodReflection->getName() === 'staticThrowOrNot'; + return ($methodReflection->getDeclaringClass()->getName() === Foo::class && $methodReflection->getName() === 'staticThrowOrNot') + || ($methodReflection->getDeclaringClass()->getName() === ConstructorThrowOrNot::class && $methodReflection->getName() === '__construct'); } public function getThrowTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type @@ -120,4 +121,75 @@ public function doFoo4() } } + public function doFoo5() + { + $class = self::class; + try { + $result = $class::staticThrowOrNot(false); + } finally { + assertVariableCertainty(TrinaryLogic::createYes(), $result); + } + } + + public function doFoo6() + { + try { + $result = $this::staticThrowOrNot(false); + } finally { + assertVariableCertainty(TrinaryLogic::createYes(), $result); + } + } + + public function doFoo7() + { + $class = self::class; + try { + $result = $class::staticThrowOrNot(true); + } finally { + assertVariableCertainty(TrinaryLogic::createMaybe(), $result); + } + } + +} + +class ConstructorThrowOrNot +{ + + /** @throws \Exception */ + public function __construct(bool $need = true) + { + if ($need) { + throw new \Exception(); + } + } + + public function doFoo1() + { + try { + $result = new self(false); + } finally { + assertVariableCertainty(TrinaryLogic::createYes(), $result); + } + } + + public function doFoo2() + { + $class = self::class; + try { + $result = new $class(false); + } finally { + assertVariableCertainty(TrinaryLogic::createYes(), $result); + } + } + + public function doFoo3() + { + $class = self::class; + try { + $result = new $class(true); + } finally { + assertVariableCertainty(TrinaryLogic::createMaybe(), $result); + } + } + } diff --git a/tests/PHPStan/Analyser/nsrt/bug-15837.php b/tests/PHPStan/Analyser/nsrt/bug-15837.php new file mode 100644 index 00000000000..8c8e4ba37ef --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-15837.php @@ -0,0 +1,121 @@ += 8.0 + +namespace Bug15837; + +use RuntimeException; +use function PHPStan\Testing\assertType; + +final class Example +{ + /** @throws ($fail is true ? RuntimeException : void) */ + public static function run(bool $fail = true): void + { + if ($fail) { + throw new RuntimeException(); + } + } +} + +function direct(): void +{ + $result = false; + try { + Example::run(false); + $result = true; + } catch (RuntimeException) { + } + assertType('true', $result); +} + +function indirect(): void +{ + $class = Example::class; + $result = false; + try { + $class::run(false); + $result = true; + } catch (RuntimeException) { + } + assertType('true', $result); +} + +function throughObject(Example $example): void +{ + $result = false; + try { + $example::run(false); + $result = true; + } catch (RuntimeException) { + } + assertType('true', $result); +} + +/** @param class-string $class */ +function throughClassString(string $class): void +{ + $result = false; + try { + $class::run(false); + $result = true; + } catch (RuntimeException) { + } + assertType('true', $result); +} + +function indirectMayThrow(): void +{ + $class = Example::class; + $result = false; + try { + $class::run(); + $result = true; + } catch (RuntimeException) { + } + assertType('bool', $result); +} + +final class ExampleConstructor +{ + /** @throws ($fail is true ? RuntimeException : void) */ + public function __construct(bool $fail = true) + { + if ($fail) { + throw new RuntimeException(); + } + } +} + +function directNew(): void +{ + $result = false; + try { + new ExampleConstructor(false); + $result = true; + } catch (RuntimeException) { + } + assertType('true', $result); +} + +function indirectNew(): void +{ + $class = ExampleConstructor::class; + $result = false; + try { + new $class(false); + $result = true; + } catch (RuntimeException) { + } + assertType('true', $result); +} + +function indirectNewMayThrow(): void +{ + $class = ExampleConstructor::class; + $result = false; + try { + new $class(); + $result = true; + } catch (RuntimeException) { + } + assertType('bool', $result); +} diff --git a/turbo-ext/src/NewHandler.cpp b/turbo-ext/src/NewHandler.cpp index 6cb74d9520e..cdb65a0a13e 100644 --- a/turbo-ext/src/NewHandler.cpp +++ b/turbo-ext/src/NewHandler.cpp @@ -529,49 +529,42 @@ class NewHandler if (UNEXPECTED(objectClasses.isUndef())) return zv::Val(); zend_long objectClassCount = countOf(objectClasses.raw()); if (UNEXPECTED(objectClassCount < 0)) return zv::Val(); - zv::Val additionalThrowPoints; if (objectClassCount == 1) { zval *objectClass = firstOf(objectClasses.raw(), &null); if (UNEXPECTED(objectClass == NULL)) return zv::Val(); - zv::Val objectClassName = pt_name_node_new(PT_CLASS_NAME, objectClass); - if (UNEXPECTED(objectClassName.isUndef())) return zv::Val(); - zv::Val attributes = syntheticSiteAttributes(); - zv::Arr noArgs = zv::Arr::empty(); - zv::Args newArgv{objectClassName.raw(), noArgs.raw(), attributes.raw()}; - zv::Val syntheticNew = pt_type_new(PT_CLASS_NEW, 3, newArgv); - if (UNEXPECTED(syntheticNew.isUndef())) return zv::Val(); - zv::Val noopNodeCallback = pt_type_new(PT_CLASS_NOOP_NODE_CALLBACK, 0, NULL); - if (UNEXPECTED(noopNodeCallback.isUndef())) return zv::Val(); - zv::Val deepContext = pt_expression_context_enter_deep(context); - if (UNEXPECTED(deepContext.isUndef())) return zv::Val(); - zv::Val syntheticContext = pt_expression_context_without_template_argument_resolution(deepContext.raw()); - if (UNEXPECTED(syntheticContext.isUndef())) return zv::Val(); - zv::Val objectExprResult = pt_node_scope_resolver_process_expr_node(nodeScopeResolver, stmt, syntheticNew.raw(), currentScope.raw(), storage, noopNodeCallback.raw(), syntheticContext.raw()); - if (UNEXPECTED(objectExprResult.isUndef())) return zv::Val(); - objectClass = firstOf(objectClasses.raw(), &null); - if (UNEXPECTED(objectClass == NULL)) return zv::Val(); className = zv::Val::copyOf(zv::Ref(objectClass)); - borrowed = pt_expression_result_throw_points(objectExprResult.raw(), hold); - if (UNEXPECTED(borrowed == NULL)) return zv::Val(); - additionalThrowPoints = zv::Val::copyOf(zv::Ref(borrowed)); - } else { - zv::Val throwPoint = pt_internal_throw_point_create_implicit(currentScope.raw(), expr); - if (UNEXPECTED(throwPoint.isUndef())) return zv::Val(); - zv::Arr list = zv::Arr::create(1); - list.push(std::move(throwPoint)); - additionalThrowPoints = zv::Val(std::move(list)); - } - - if (UNEXPECTED(Z_TYPE_P(throwPoints.raw()) != IS_ARRAY || Z_TYPE_P(additionalThrowPoints.raw()) != IS_ARRAY)) { - zend_type_error("array_merge(): Argument #%d must be of type array, %s given", Z_TYPE_P(throwPoints.raw()) != IS_ARRAY ? 1 : 2, zend_zval_value_name(Z_TYPE_P(throwPoints.raw()) != IS_ARRAY ? throwPoints.raw() : additionalThrowPoints.raw())); - return zv::Val(); - } - throwPoints = arrayMerge(throwPoints.raw(), additionalThrowPoints.raw()); - - if (!className.isNull()) { if (UNEXPECTED(!processConstructorReflection(className.raw(), expr, parts))) return zv::Val(); deferredConstructorImpureIsDynamic = 1; + + // a resolved constructor gets its throw point from the actual arguments below + if (parts.constructorReflection.isNull()) { + zv::Val objectClassName = pt_name_node_new(PT_CLASS_NAME, className.raw()); + if (UNEXPECTED(objectClassName.isUndef())) return zv::Val(); + zv::Val attributes = syntheticSiteAttributes(); + zv::Arr noArgs = zv::Arr::empty(); + zv::Args newArgv{objectClassName.raw(), noArgs.raw(), attributes.raw()}; + zv::Val syntheticNew = pt_type_new(PT_CLASS_NEW, 3, newArgv); + if (UNEXPECTED(syntheticNew.isUndef())) return zv::Val(); + zv::Val noopNodeCallback = pt_type_new(PT_CLASS_NOOP_NODE_CALLBACK, 0, NULL); + if (UNEXPECTED(noopNodeCallback.isUndef())) return zv::Val(); + zv::Val deepContext = pt_expression_context_enter_deep(context); + if (UNEXPECTED(deepContext.isUndef())) return zv::Val(); + zv::Val syntheticContext = pt_expression_context_without_template_argument_resolution(deepContext.raw()); + if (UNEXPECTED(syntheticContext.isUndef())) return zv::Val(); + zv::Val objectExprResult = pt_node_scope_resolver_process_expr_node(nodeScopeResolver, stmt, syntheticNew.raw(), currentScope.raw(), storage, noopNodeCallback.raw(), syntheticContext.raw()); + if (UNEXPECTED(objectExprResult.isUndef())) return zv::Val(); + borrowed = pt_expression_result_throw_points(objectExprResult.raw(), hold); + if (UNEXPECTED(borrowed == NULL)) return zv::Val(); + if (UNEXPECTED(Z_TYPE_P(throwPoints.raw()) != IS_ARRAY || Z_TYPE_P(borrowed) != IS_ARRAY)) { + zend_type_error("array_merge(): Argument #%d must be of type array, %s given", Z_TYPE_P(throwPoints.raw()) != IS_ARRAY ? 1 : 2, zend_zval_value_name(Z_TYPE_P(throwPoints.raw()) != IS_ARRAY ? throwPoints.raw() : borrowed)); + return zv::Val(); + } + throwPoints = arrayMerge(throwPoints.raw(), borrowed); + } } else { + zv::Val throwPoint = pt_internal_throw_point_create_implicit(currentScope.raw(), expr); + if (UNEXPECTED(throwPoint.isUndef())) return zv::Val(); + appendTo(throwPoints, std::move(throwPoint)); zv::Val point = pt_impure_point_new(currentScope.raw(), expr, pt_nh_new, pt_nh_instantiation_of_unknown_class, false); if (UNEXPECTED(point.isUndef())) return zv::Val(); appendTo(impurePoints, std::move(point)); diff --git a/turbo-ext/src/StaticCallHandler.cpp b/turbo-ext/src/StaticCallHandler.cpp index cd81bb639f9..68019175fef 100644 --- a/turbo-ext/src/StaticCallHandler.cpp +++ b/turbo-ext/src/StaticCallHandler.cpp @@ -506,7 +506,8 @@ class StaticCallHandler currentScope = zv::Val::copyOf(zv::Ref(borrowed)); } - if (classIsExpr) { + // a resolved method gets its throw point from the actual arguments below + if (classIsExpr && methodReflection.isNull()) { // the class expr was processed above as the receiver; read its // already-computed result instead of re-walking via Scope::getType(). zv::Val classResultType = pt_expression_result_get_type(classResult.raw());