Skip to content

Commit 5f7a642

Browse files
committed
Report errors on non-variable foreach targets only from the final walk
The convergence passes assigned a property or list() foreach target through the real node callback, so rules reported it once per pass. Amp-Thread-ID: https://ampcode.com/threads/T-01a235f4-f23b-74d9-9bdf-7d6abde01603
1 parent b8a57b5 commit 5f7a642

4 files changed

Lines changed: 65 additions & 4 deletions

File tree

‎src/Analyser/StmtHandler/ForeachHandler.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ static function () use ($condResult, $emptyArrayType): Type {
234234
} else {
235235
$scope->pushExpressionResultStorage($storage);
236236
try {
237-
$bodyScope = $this->enterForeach($nodeScopeResolver, $originalScope, $storage, $originalScope, $stmt, $foreachIterateeType, $foreachNativeIterateeType, $nodeCallback);
237+
// the passes walk without reporting - only the final walk below enters with the real callback
238+
$bodyScope = $this->enterForeach($nodeScopeResolver, $originalScope, $storage, $originalScope, $stmt, $foreachIterateeType, $foreachNativeIterateeType, new NoopNodeCallback());
238239
} finally {
239240
$scope->popExpressionResultStorage();
240241
}
@@ -271,7 +272,7 @@ static function () use ($condResult, $emptyArrayType): Type {
271272
$bodyRecording = new RecordingNodeCallback();
272273
$scope->pushExpressionResultStorage($storage);
273274
try {
274-
$bodyScope = $this->enterForeach($nodeScopeResolver, $bodyScope, $storage, $originalScope, $stmt, $foreachIterateeType, $foreachNativeIterateeType, $nodeCallback);
275+
$bodyScope = $this->enterForeach($nodeScopeResolver, $bodyScope, $storage, $originalScope, $stmt, $foreachIterateeType, $foreachNativeIterateeType, new NoopNodeCallback());
275276
$passRecord = null;
276277
$bodyScopeResult = $nodeScopeResolver->processLoopPassStmtNodes($stmt, $stmt->stmts, $bodyScope, $storage, $bodyRecording, $context->enterLoopPass($loopHeads), $previousPass, $passRecord)->filterOutLoopExitPoints();
277278
$previousPass = $passRecord;

‎tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,4 +1355,22 @@ public function testBug8964(): void
13551355
]);
13561356
}
13571357

1358+
public function testBug15924(): void
1359+
{
1360+
$this->analyse([__DIR__ . '/data/bug-15924.php'], [
1361+
[
1362+
'Property Bug15924\\T::$webUser (Bug15924\\User|false) does not accept Bug15924\\UserInterface.',
1363+
20,
1364+
],
1365+
[
1366+
'Property Bug15924\\T::$key (string) does not accept int<0, max>.',
1367+
27,
1368+
],
1369+
[
1370+
'Property Bug15924\\T::$webUser (Bug15924\\User|false) does not accept Bug15924\\UserInterface.',
1371+
34,
1372+
],
1373+
]);
1374+
}
1375+
13581376
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15924;
4+
5+
interface UserInterface {}
6+
7+
class User implements UserInterface {}
8+
9+
class T
10+
{
11+
/** @var User|false */
12+
protected $webUser = false;
13+
14+
/** @var string */
15+
protected $key = '';
16+
17+
/** @param list<UserInterface> $users */
18+
public function run(array $users): void
19+
{
20+
foreach ($users as $this->webUser) {
21+
}
22+
}
23+
24+
/** @param list<UserInterface> $users */
25+
public function runWithKey(array $users): void
26+
{
27+
foreach ($users as $this->key => $user) {
28+
}
29+
}
30+
31+
/** @param list<array{UserInterface}> $users */
32+
public function runWithList(array $users): void
33+
{
34+
foreach ($users as [$this->webUser]) {
35+
}
36+
}
37+
}

‎turbo-ext/src/ForeachHandler.cpp‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,11 @@ class ForeachHandler
502502
unrolled = true;
503503
unrolledTotalKeys = unrolledResult.totalKeys;
504504
} else {
505+
// the passes walk without reporting - only the final walk below enters with the real callback
506+
zv::Val entryNoop = ptlh::newNoopNodeCallback();
507+
if (UNEXPECTED(entryNoop.isUndef())) return zv::Val();
505508
if (UNEXPECTED(!pt_mutating_scope_push_expression_result_storage(Z_OBJ_P(scope.raw()), storage.raw()))) return zv::Val();
506-
bodyScope = enterForeach(nodeScopeResolver, originalScope.raw(), storage.raw(), originalScope.raw(), stmt, parts, foreachIterateeType.raw(), foreachNativeIterateeType.raw(), nodeCallback);
509+
bodyScope = enterForeach(nodeScopeResolver, originalScope.raw(), storage.raw(), originalScope.raw(), stmt, parts, foreachIterateeType.raw(), foreachNativeIterateeType.raw(), entryNoop.raw());
507510
pt_finally([&]() { (void) pt_mutating_scope_pop_expression_result_storage(Z_OBJ_P(scope.raw())); });
508511
if (UNEXPECTED(EG(exception))) return zv::Val();
509512
// heads come from pass walks - a top-level walk converges from its own entry
@@ -546,7 +549,9 @@ class ForeachHandler
546549
zv::Val bodyScopeResult;
547550
bool brokeOut = false;
548551
[&]() {
549-
zv::Val entered = enterForeach(nodeScopeResolver, bodyScope.raw(), storage.raw(), originalScope.raw(), stmt, parts, foreachIterateeType.raw(), foreachNativeIterateeType.raw(), nodeCallback);
552+
zv::Val passNoop = ptlh::newNoopNodeCallback();
553+
if (UNEXPECTED(passNoop.isUndef())) return;
554+
zv::Val entered = enterForeach(nodeScopeResolver, bodyScope.raw(), storage.raw(), originalScope.raw(), stmt, parts, foreachIterateeType.raw(), foreachNativeIterateeType.raw(), passNoop.raw());
550555
if (UNEXPECTED(entered.isUndef())) return;
551556
bodyScope = std::move(entered);
552557
zv::Val passContext = pt_statement_context_enter_loop_pass(context, loopHeads.raw());

0 commit comments

Comments
 (0)