Skip to content

Commit 3b21093

Browse files
committed
Fixed variable certainty of a nonexistent variable after isset()
1 parent 71349b4 commit 3b21093

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/Analyser/TypeSpecifier.php

+8
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
use PHPStan\Type\TypeUtils;
4646
use PHPStan\Type\TypeWithClassName;
4747
use PHPStan\Type\UnionType;
48+
use function array_reverse;
4849

4950
class TypeSpecifier
5051
{
@@ -572,8 +573,15 @@ public function specifyTypesInCondition(
572573
throw new \PHPStan\ShouldNotHappenException();
573574
}
574575

576+
$vars = array_reverse($vars);
577+
575578
$types = null;
576579
foreach ($vars as $var) {
580+
if ($var instanceof Expr\Variable && is_string($var->name)) {
581+
if ($scope->hasVariableType($var->name)->no()) {
582+
return new SpecifiedTypes([], []);
583+
}
584+
}
577585
if ($expr instanceof Expr\Isset_) {
578586
if (
579587
$var instanceof ArrayDimFetch

tests/PHPStan/Analyser/NodeScopeResolverTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -10209,6 +10209,11 @@ public function dataCount(): array
1020910209
return $this->gatherAssertTypes(__DIR__ . '/data/count-type.php');
1021010210
}
1021110211

10212+
public function dataBug3985(): array
10213+
{
10214+
return $this->gatherAssertTypes(__DIR__ . '/data/bug-3985.php');
10215+
}
10216+
1021210217
/**
1021310218
* @dataProvider dataBug2574
1021410219
* @dataProvider dataBug2577
@@ -10293,6 +10298,7 @@ public function dataCount(): array
1029310298
* @dataProvider dataBug1924
1029410299
* @dataProvider dataExtraIntTypes
1029510300
* @dataProvider dataCount
10301+
* @dataProvider dataBug3985
1029610302
* @param string $assertType
1029710303
* @param string $file
1029810304
* @param mixed ...$args
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Bug3985;
4+
5+
use PHPStan\TrinaryLogic;
6+
use function PHPStan\Analyser\assertVariableCertainty;
7+
8+
class Foo
9+
{
10+
public function doFoo(array $array): void
11+
{
12+
foreach ($array as $val) {
13+
if (isset($foo[1])) {
14+
assertVariableCertainty(TrinaryLogic::createNo(), $foo);
15+
}
16+
}
17+
}
18+
19+
public function doBar(): void
20+
{
21+
if (isset($foo[1])) {
22+
assertVariableCertainty(TrinaryLogic::createNo(), $foo);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)