Skip to content
Open
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
16 changes: 7 additions & 9 deletions src/Analyser/ExprHandler/NewHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/Analyser/ExprHandler/StaticCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

}
121 changes: 121 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-15837.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php // lint >= 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<Example> $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);
}
65 changes: 29 additions & 36 deletions turbo-ext/src/NewHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
3 changes: 2 additions & 1 deletion turbo-ext/src/StaticCallHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Loading