Skip to content

Commit b68353e

Browse files
committed
Keep a merged call without a narrowable argument in the scope
Since bcca2b0, a merge drops a remembered call whose merged type is what the call resolves to on its own, so that later narrowing of the call's arguments reaches it. A call without an argument that narrowing can change, such as isMulti() or can('update'), never goes stale, but dropping its entry still changes later merges: a branch without the entry merges into a Maybe holder, which derives no conditional guards and is intersected as if it held the call's type when a conditional expression narrows the call. Such calls now keep their entry. Amp-Thread-ID: https://ampcode.com/threads/T-01a1d722-5ae1-772c-a909-1fc9b8e04be4
1 parent ee76055 commit b68353e

6 files changed

Lines changed: 450 additions & 222 deletions

File tree

‎src/Analyser/MutatingScope.php‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4501,12 +4501,45 @@ private static function findMergedCallCandidates(array $differingExpressionKeys,
45014501
if (!$expr instanceof FuncCall && !$expr instanceof MethodCall && !$expr instanceof Expr\StaticCall) {
45024502
continue;
45034503
}
4504+
if (!self::hasNarrowableArgument($expr)) {
4505+
continue;
4506+
}
45044507
$candidates[$exprString] = $holder;
45054508
}
45064509

45074510
return $candidates;
45084511
}
45094512

4513+
/**
4514+
* Whether the call has an argument that later narrowing can change - only then
4515+
* can its remembered entry go stale. Any other call keeps its entry even when
4516+
* it is uninformative: merging with a branch that lacks the entry would leave a
4517+
* Maybe holder instead, which derives no conditional guards and is intersected
4518+
* as if it held the call's type when a conditional expression narrows the call.
4519+
*/
4520+
private static function hasNarrowableArgument(FuncCall|MethodCall|Expr\StaticCall $call): bool
4521+
{
4522+
foreach ($call->args as $arg) {
4523+
if (!$arg instanceof Arg) {
4524+
continue;
4525+
}
4526+
$value = $arg->value;
4527+
if ($value instanceof Scalar && !$value instanceof Scalar\InterpolatedString) {
4528+
continue;
4529+
}
4530+
if ($value instanceof ConstFetch) {
4531+
continue;
4532+
}
4533+
if ($value instanceof ClassConstFetch && $value->class instanceof Name && $value->name instanceof Identifier) {
4534+
continue;
4535+
}
4536+
4537+
return true;
4538+
}
4539+
4540+
return false;
4541+
}
4542+
45104543
/**
45114544
* Called on the merged scope without the candidates: keys of candidates whose
45124545
* merged type (and native type) is exactly what the call resolves to here.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15727;
4+
5+
use PHPStan\TrinaryLogic;
6+
use function PHPStan\Testing\assertType;
7+
use function PHPStan\Testing\assertVariableCertainty;
8+
9+
function isMulti(): bool
10+
{
11+
if (defined('MULTI')) {
12+
return (bool) MULTI;
13+
}
14+
15+
return false;
16+
}
17+
18+
function can(string $cap): bool
19+
{
20+
return $cap === 'update';
21+
}
22+
23+
function getS(): string
24+
{
25+
return (string) rand();
26+
}
27+
28+
function variableAssignedUnderOr(): void
29+
{
30+
if (isMulti()) {
31+
echo 'network';
32+
}
33+
34+
if (!isMulti() || can('update')) {
35+
$updateData = 1;
36+
}
37+
38+
if (!isMulti()) {
39+
assertVariableCertainty(TrinaryLogic::createYes(), $updateData);
40+
echo $updateData;
41+
}
42+
}
43+
44+
function callNarrowedAfterIfWithoutElse(): void
45+
{
46+
$client = null;
47+
if (getS()) {
48+
$client = 1;
49+
}
50+
51+
if (getS() && $client !== null) {
52+
echo 1;
53+
}
54+
55+
assertType('string', getS());
56+
}
57+
58+
class Foo
59+
{
60+
61+
public function isMulti(): bool
62+
{
63+
return true;
64+
}
65+
66+
public static function isMultiStatic(): bool
67+
{
68+
return true;
69+
}
70+
71+
public function can(string $cap): bool
72+
{
73+
return $cap === 'update';
74+
}
75+
76+
public function methodCall(): void
77+
{
78+
if ($this->isMulti()) {
79+
echo 'network';
80+
}
81+
82+
if (!$this->isMulti() || $this->can(self::class)) {
83+
$updateData = 1;
84+
}
85+
86+
if (!$this->isMulti()) {
87+
assertVariableCertainty(TrinaryLogic::createYes(), $updateData);
88+
}
89+
}
90+
91+
public function staticCall(): void
92+
{
93+
if (self::isMultiStatic()) {
94+
echo 'network';
95+
}
96+
97+
if (!self::isMultiStatic() || $this->can('update')) {
98+
$updateData = 1;
99+
}
100+
101+
if (!self::isMultiStatic()) {
102+
assertVariableCertainty(TrinaryLogic::createYes(), $updateData);
103+
}
104+
}
105+
106+
}

‎tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,13 @@ public function testBug14418(): void
502502
$this->analyse([__DIR__ . '/data/bug-14418.php'], []);
503503
}
504504

505+
public function testBug15727(): void
506+
{
507+
$this->cliArgumentsVariablesRegistered = true;
508+
$this->checkMaybeUndefinedVariables = true;
509+
$this->analyse([__DIR__ . '/data/bug-15727.php'], []);
510+
}
511+
505512
public function testVariableVariableName(): void
506513
{
507514
$this->cliArgumentsVariablesRegistered = true;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15727Rule;
4+
5+
function isMulti(): bool
6+
{
7+
if (defined('MULTI')) {
8+
return (bool) MULTI;
9+
}
10+
11+
return false;
12+
}
13+
14+
function can(string $cap): bool
15+
{
16+
return $cap === 'update';
17+
}
18+
19+
function variableAssignedUnderOr(): void
20+
{
21+
if (isMulti()) {
22+
echo 'network';
23+
}
24+
25+
if (!isMulti() || can('update')) {
26+
$updateData = 1;
27+
}
28+
29+
if (!isMulti()) {
30+
echo $updateData;
31+
}
32+
}

‎turbo-ext/src/MutatingScope.cpp‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8513,6 +8513,9 @@ class MutatingScope
85138513
if (isCall) break;
85148514
}
85158515
if (!isCall) continue;
8516+
bool narrowable;
8517+
if (UNEXPECTED(!hasNarrowableArgument(expr.ref().asObject(), narrowable))) return zv::Val();
8518+
if (!narrowable) continue;
85168519
candidates.separate();
85178520
Z_TRY_ADDREF_P(holder);
85188521
if (key != NULL) {
@@ -8524,6 +8527,47 @@ class MutatingScope
85248527
return zv::Val(std::move(candidates));
85258528
}
85268529

8530+
/* private static (twin hasNarrowableArgument()): an Arg in $call->args
8531+
* whose value is not a Scalar other than an InterpolatedString, a
8532+
* ConstFetch, or a ClassConstFetch of a Name and an Identifier; false =
8533+
* pending exception */
8534+
[[nodiscard]] static bool hasNarrowableArgument(zend_object *call, bool &out)
8535+
{
8536+
out = false;
8537+
zv::Ref args = nodeProp(call, PT_LC("args"));
8538+
if (UNEXPECTED(args.raw() == NULL)) return false;
8539+
for (auto entry : zv::ArrRef(args.deref().raw())) {
8540+
zv::Ref arg = entry.value().deref();
8541+
bool is;
8542+
if (UNEXPECTED(!isInstance(arg, PT_CLASS_ARG, is))) return false;
8543+
if (!is) continue;
8544+
zv::Ref valueSlot = nodeProp(arg.asObject(), PT_LC("value"));
8545+
if (UNEXPECTED(valueSlot.raw() == NULL)) return false;
8546+
zv::Ref value = valueSlot.deref();
8547+
if (UNEXPECTED(!isInstance(value, PT_CLASS_SCALAR, is))) return false;
8548+
if (is) {
8549+
if (UNEXPECTED(!isInstance(value, PT_CLASS_INTERPOLATED_STRING, is))) return false;
8550+
if (!is) continue;
8551+
} else {
8552+
if (UNEXPECTED(!isInstance(value, PT_CLASS_CONST_FETCH, is))) return false;
8553+
if (is) continue;
8554+
if (UNEXPECTED(!isInstance(value, PT_CLASS_CLASS_CONST_FETCH, is))) return false;
8555+
if (is) {
8556+
zv::Ref classSlot = nodeProp(value.asObject(), PT_LC("class"));
8557+
if (UNEXPECTED(classSlot.raw() == NULL)) return false;
8558+
bool classIsName;
8559+
if (UNEXPECTED(!isInstance(classSlot.deref(), PT_CLASS_NAME, classIsName))) return false;
8560+
bool nameIsIdentifier = false;
8561+
if (classIsName && UNEXPECTED(!nodeNameIs(value.asObject(), PT_CLASS_IDENTIFIER, nameIsIdentifier))) return false;
8562+
if (nameIsIdentifier) continue;
8563+
}
8564+
}
8565+
out = true;
8566+
return true;
8567+
}
8568+
return true;
8569+
}
8570+
85278571
/* private (twin findUninformativeMergedCalls(), called on the merged scope
85288572
* without the candidates): keys of candidates whose merged type (and
85298573
* native type) is exactly what the call resolves to there */

0 commit comments

Comments
 (0)