diff --git a/src/Analyser/ExprHandler/AssignHandler.php b/src/Analyser/ExprHandler/AssignHandler.php index 426d4aa65f1..83d945e3d26 100644 --- a/src/Analyser/ExprHandler/AssignHandler.php +++ b/src/Analyser/ExprHandler/AssignHandler.php @@ -2635,6 +2635,13 @@ private function isExprSafeToProjectThroughVariable(Expr $expr, string $variable return is_string($expr->name) && $expr->name !== $variableName; } + // an expression reading the assigned variable, like strlen($s ?: '') in + // `$s = strlen($s ?: '') ?: 0`, is about the variable's previous value - + // a narrowing guarded by its new value does not apply to it + if ((new NodeFinder())->findFirst($expr, static fn (Node $node): bool => $node instanceof Variable && $node->name === $variableName) !== null) { + return false; + } + if ( $expr instanceof PropertyFetch || $expr instanceof ArrayDimFetch diff --git a/tests/PHPStan/Analyser/nsrt/bug-15754.php b/tests/PHPStan/Analyser/nsrt/bug-15754.php new file mode 100644 index 00000000000..022de3081e7 --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-15754.php @@ -0,0 +1,51 @@ += 8.0 + +declare(strict_types = 1); + +namespace Bug15754; + +use function PHPStan\Testing\assertType; + +function f(?string $s): void { $s = strlen($s ?: "") ?: 0; } + +function g(?string $s): void +{ + $s = strlen($s ?: "") ?: 0; + assertType('int<0, max>', $s); + if ($s === 0) { + assertType('0', $s); + } else { + assertType('int<1, max>', $s); + } +} + +function h(?string $businessTypeIds): void +{ + $businessTypeIds = preg_split('/,/', $businessTypeIds ?: '', flags: PREG_SPLIT_NO_EMPTY) ?: []; + assertType('list', $businessTypeIds); +} + +final class Foo +{ + + public ?int $p = null; + + public ?Foo $next = null; + +} + +function i(Foo $f): void +{ + $f = $f->p !== null ? $f->next : null; + if ($f !== null) { + assertType('int|null', $f->p); + } +} + +function j(Foo $f): void +{ + $f = $f->p === null ? null : $f->next; + if ($f !== null) { + assertType('int|null', $f->p); + } +} diff --git a/turbo-ext/src/AssignHandler.cpp b/turbo-ext/src/AssignHandler.cpp index d6ff3f394d1..de064f34bb1 100644 --- a/turbo-ext/src/AssignHandler.cpp +++ b/turbo-ext/src/AssignHandler.cpp @@ -2924,6 +2924,39 @@ class AssignHandler return true; } + struct ReadsVariableCtx + { + pt_find_ctx base; + zend_class_entry *variableCe; + zend_string *variableName; + }; + + /* matcher for the shared findFirst walker: a Variable named variableName */ + static bool isVariableNamed(zend_object *node, void *vctx) + { + ReadsVariableCtx *ctx = (ReadsVariableCtx *) vctx; + if (!instanceof_function(node->ce, ctx->variableCe)) return false; + zval nodeZv; + ZVAL_OBJ(&nodeZv, node); + zend_string *name = ahVariableName(&nodeZv); + return name != NULL && zend_string_equals(name, ctx->variableName); + } + + /* (new NodeFinder())->findFirst($expr, fn ($node) => $node instanceof Variable + * && $node->name === $variableName) !== null; false = pending exception */ + [[nodiscard]] static bool exprReadsVariable(zval *expr, zend_string *variableName, bool &out) + { + ReadsVariableCtx ctx; + memset(&ctx, 0, sizeof(ctx)); + ctx.variableCe = pt_class(PT_CLASS_VARIABLE); + if (UNEXPECTED(ctx.variableCe == NULL)) return false; + ctx.variableName = variableName; + zend_object *found = pt_find_first_recursive(Z_OBJ_P(expr), isVariableNamed, &ctx); + if (UNEXPECTED(ctx.base.failed)) return false; + out = found != NULL; + return true; + } + /* (twin 2448) false = pending exception */ [[nodiscard]] static bool isExprSafeToProjectThroughVariable(zval *expr, zend_string *variableName, zval *rhsImpurePoints, zval *assignedExpr, bool &out) { @@ -2946,6 +2979,16 @@ class AssignHandler return true; } + // an expression reading the assigned variable, like strlen($s ?: '') in + // `$s = strlen($s ?: '') ?: 0`, is about the variable's previous value - + // a narrowing guarded by its new value does not apply to it + bool readsVariable = false; + AH_OKB(exprReadsVariable(expr, variableName, readsVariable)); + if (readsVariable) { + out = false; + return true; + } + if (ahIs(expr, PT_CLASS_PROPERTY_FETCH) || ahIs(expr, PT_CLASS_ARRAY_DIM_FETCH)) { out = true; return true;