Skip to content

Commit cc640d7

Browse files
committed
Fix invalidating conditional expressions after ArrayDimFetch unset
1 parent 871b710 commit cc640d7

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

src/Analyser/MutatingScope.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4170,6 +4170,23 @@ public function specifyExpressionType(Expr $expr, Type $type, ?Type $nativeType
41704170
$exprString = sprintf('$%s', $variableName);
41714171
$nativeTypes[$exprString] = $nativeType;
41724172

4173+
$conditionalExpressions = [];
4174+
foreach ($this->conditionalExpressions as $conditionalExprString => $holders) {
4175+
if ($conditionalExprString === $exprString) {
4176+
continue;
4177+
}
4178+
4179+
foreach ($holders as $holder) {
4180+
foreach (array_keys($holder->getConditionExpressionTypes()) as $conditionExprString2) {
4181+
if ($conditionExprString2 === $exprString) {
4182+
continue 3;
4183+
}
4184+
}
4185+
}
4186+
4187+
$conditionalExpressions[$conditionalExprString] = $holders;
4188+
}
4189+
41734190
return $this->scopeFactory->create(
41744191
$this->context,
41754192
$this->isDeclareStrictTypes(),
@@ -4178,7 +4195,7 @@ public function specifyExpressionType(Expr $expr, Type $type, ?Type $nativeType
41784195
$this->getNamespace(),
41794196
$variableTypes,
41804197
$this->moreSpecificTypes,
4181-
$this->conditionalExpressions,
4198+
$conditionalExpressions,
41824199
$this->inClosureBindScopeClass,
41834200
$this->anonymousFunctionReflection,
41844201
$this->inFirstLevelStatement,

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,7 @@ public function dataFileAsserts(): iterable
912912
yield from $this->gatherAssertTypes(__DIR__ . '/data/non-empty-string-str-containing-fns.php');
913913
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6609.php');
914914
yield from $this->gatherAssertTypes(__DIR__ . '/data/non-empty-string-substr-specifying.php');
915+
yield from $this->gatherAssertTypes(__DIR__ . '/data/unset-conditional-expressions.php');
915916
}
916917

917918
/**
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace UnsetConditionalExpressions;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
10+
/**
11+
* @param array<string, mixed> $filteredParameters
12+
*/
13+
public function doFoo(array $filteredParameters, array $a): void
14+
{
15+
$otherFilteredParameters = $filteredParameters;
16+
foreach ($a as $k => $v) {
17+
if (rand(0, 1)) {
18+
unset($otherFilteredParameters[$k]);
19+
}
20+
}
21+
22+
if (count($otherFilteredParameters) > 0) {
23+
return;
24+
}
25+
26+
assertType('array{}', $otherFilteredParameters);
27+
assertType('array<string, mixed>', $filteredParameters);
28+
}
29+
30+
}

0 commit comments

Comments
 (0)