Skip to content
Closed
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
7 changes: 7 additions & 0 deletions src/Analyser/ExprHandler/AssignHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-15754.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php // lint >= 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<non-empty-string>', $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);
}
}
43 changes: 43 additions & 0 deletions turbo-ext/src/AssignHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down
Loading