Skip to content
Merged
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
8 changes: 8 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4094,6 +4094,14 @@ private function createConditionalExpressions(
continue;
}

if (
array_key_exists($exprString, $newVariableTypes)
&& !$newVariableTypes[$exprString]->getCertainty()->equals($holder->getCertainty())
&& $newVariableTypes[$exprString]->equalTypes($holder)
) {
continue;
}

unset($newVariableTypes[$exprString]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
return null;
}

[$reflectionCacheKey, $variableCacheKey] = $this->getCacheKeys($file, $identifier); // @phpstan-ignore variable.undefined
[$reflectionCacheKey, $variableCacheKey] = $this->getCacheKeys($file, $identifier);
$classReflection = $this->nodeToReflection($reflector, $fetchedClassNode);
$this->cache->save($reflectionCacheKey, $variableCacheKey, $classReflection->exportToCache());

Expand Down Expand Up @@ -171,7 +171,7 @@ public function locateIdentifier(Reflector $reflector, Identifier $identifier):
return null;
}

[$reflectionCacheKey, $variableCacheKey] = $this->getCacheKeys($file, $identifier); // @phpstan-ignore variable.undefined
[$reflectionCacheKey, $variableCacheKey] = $this->getCacheKeys($file, $identifier);
$constantReflection = $this->nodeToReflection(
$reflector,
$fetchedConstantNode,
Expand Down
67 changes: 67 additions & 0 deletions tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,26 @@ public function testBug5919(): void
$this->analyse([__DIR__ . '/data/bug-5919.php'], []);
}

public function testBug8430(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = false;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;

$this->analyse([__DIR__ . '/data/bug-8430.php'], []);
}

public function testBug8430b(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = false;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;

$this->analyse([__DIR__ . '/data/bug-8430b.php'], []);
}

public function testBug5477(): void
{
$this->cliArgumentsVariablesRegistered = true;
Expand All @@ -1333,4 +1353,51 @@ public function testBug5477(): void
$this->analyse([__DIR__ . '/data/bug-5477.php'], []);
}

public function testBug10657(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = false;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;

$this->analyse([__DIR__ . '/data/bug-10657.php'], []);
}

public function testBug6830(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = false;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;

$this->analyse([__DIR__ . '/data/bug-6830.php'], []);
}

public function testBug14117(): void
{
$this->cliArgumentsVariablesRegistered = true;
$this->polluteScopeWithLoopInitialAssignments = false;
$this->checkMaybeUndefinedVariables = true;
$this->polluteScopeWithAlwaysIterableForeach = true;

$this->analyse([__DIR__ . '/data/bug-14117.php'], [
[
'Variable $value might not be defined.',
33,
],
[
'Variable $value might not be defined.',
49,
],
[
'Undefined variable: $value',
65,
],
[
'Undefined variable: $value',
81,
],
]);
}

}
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-10657.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Bug10657;

class HelloWorld
{
public function sayHello(bool $flag): void
{
for ($i = 0; $i < 10; $i++) {
if ($flag) {
$foo = 'bar';
}
if ($flag) {
echo $foo;
}
}
}
}
83 changes: 83 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-14117.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php declare(strict_types = 1);

namespace Bug14117;

function foo(): void {
$key = rand(0, 2);

if ($key === 2) {
$value = 'test';
}

if ($key === 1) {
$value = 'test';
}

if ($key === 1) {
echo $value;
}
}

function bar(): void {
$key = rand(0, 2);

if ($key === 2) {
$value = 'two';
}

if ($key === 1) {
$value = 'one';
}

if ($key === 2) {
echo $value;
}
}

function baz(): void {
Copy link
Contributor

@staabm staabm Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this file I added a few more tests which e.g. also check the interaction with unset().

while doing so I found a case which does not yet work: phpstan/phpstan#14227

$key = rand(0, 3);

if ($key === 1) {
$value = 'one';
}

if ($key === 2) {
$value = 'two';
}

if ($key === 3) {
echo $value; // SHOULD report "is not defined"
}
}

function boo(): void {
$key = rand(0, 2);

if ($key === 1) {
$value = 'test';
}

if ($key === 1) {
unset($value);
}

if ($key === 1) {
echo $value;
}
}

function loo(): void {
$key = rand(0, 2);

if ($key === 1) {
$value = 'test';
}

if ($key === 1 || $key === 2) {
unset($value);
}

if ($key === 1) {
echo $value;
}
}
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-6830.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Bug6830;

/** @param array<bool> $bools */
function test(array $bools): void
{
foreach ($bools as $bool) {
if ($bool) {
$foo = 'foo';
}
if ($bool) {
echo $foo;
}
}
}
31 changes: 31 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-8430.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Bug8430;

class B
{
public function abc(string $a, bool $b): void
{
for ($i = 1; $i <= 5; $i++) {
if (!$b) {
$arr = ['a' => 1];
}
if (!$a && !$b) {
echo $arr['a'];
}
}
}

public function def(string $a, bool $b): void
{
if (!$b) {
$arr = ['a' => 1];
}
if (!$a && !$b) {
echo $arr['a'];
}
}
}

111 changes: 111 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-8430b.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

declare(strict_types=1);

namespace Bug8430b;

class A
{
/** @var A[][] */
public array $a;
/** @var A[] */
public array $b;
/** @var array<string,string|string[]> */
public array $c;
public string $d;
}

class B
{
private function abc(): void
{
}

/**
* @param A[] $a
* @param array<string,string> $b
*/
public function def(array $a, array $b, string $c, bool $d): void
{
$e = false;
$f = false;
switch ($b['repeat'] ?? null) {
case 'Y':
$e = true;
break;
case 'A':
$e = true;
$f = true;
break;
}
$g = 5;
$h = 0;
for ($i = 1; $i <= $g; $i++) {
if (!$d) {
$arr = ['a' => 1];
}
$j = $a[$i] ?? null;
if ($j) {
/** @var array<A[]> $k */
$k = [];
if ($e) {
foreach ($j->a as $l) {
/** @var A[] $m */
$m = $f || empty($k) ? $j->b : [];
array_push($m, ...$l);
array_push($k, $m);
}
if (empty($k)) {
array_push($k, $j->b);
}
} else {
array_push($k, $j->b);
foreach ($j->a as $l) {
array_push($k[0], ...$l);
break;
}
}
foreach ($k as $n) {
if (!$d) {
foreach ($n as $o) {
switch ($o->c['x'] ?? '') {
case 'y':
$p = $o->c[$o->d] ?? null;
if (is_array($p)) {
$this->abc();
}
break;
case 'z':
$p = $o->c[$o->d] ?? null;
if (is_array($p)) {
$this->abc();
}
break;
default:
$this->abc();
break;
}
}
}
if (!empty($n)) {
$h++;
}
}
}
if (!$c && !$d) {
echo $arr['a'];
}
}
}

public function ghi(string $a, bool $b): void
{
if (!$b) {
$arr = ['a' => 1];
}
$this->abc();
if (!$a && !$b) {
echo $arr['a'];
}
}
}
Loading