Skip to content
Open
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
20 changes: 19 additions & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3870,14 +3870,32 @@ public function processAlwaysIterableForeachScopeWithoutPollute(self $finalScope
);
}

$conditionalExpressions = $this->conditionalExpressions;
foreach ($this->conditionalExpressions as $conditionalExprString => $holders) {
foreach ($holders as $holder) {
foreach (array_keys($holder->getConditionExpressionTypeHolders()) as $holderExprString) {
if (!isset($finalScope->expressionTypes[$holderExprString])) {
continue 2;
}
if (!isset($this->expressionTypes[$holderExprString])) {
continue 2;
}
if (!$this->expressionTypes[$holderExprString]->getType()->equals($finalScope->expressionTypes[$holderExprString]->getType())) {
unset($conditionalExpressions[$conditionalExprString]);
continue 3;
}
}
}
}

return $this->scopeFactory->create(
$this->context,
$this->isDeclareStrictTypes(),
$this->getFunction(),
$this->getNamespace(),
$expressionTypes,
$nativeTypes,
$this->conditionalExpressions,
$conditionalExpressions,
$this->inClosureBindScopeClasses,
$this->anonymousFunctionReflection,
$this->inFirstLevelStatement,
Expand Down
36 changes: 36 additions & 0 deletions tests/PHPStan/Analyser/Bug14446Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use PHPStan\Testing\TypeInferenceTestCase;
use PHPUnit\Framework\Attributes\DataProvider;

class Bug14446Test extends TypeInferenceTestCase
{

public static function dataFileAsserts(): iterable
{
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-14446.php');
}

/**
* @param mixed ...$args
*/
#[DataProvider('dataFileAsserts')]
public function testFileAsserts(
string $assertType,
string $file,
...$args,
): void
{
$this->assertFileAsserts($assertType, $file, ...$args);
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/bug-14446.neon',
];
}

}
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/bug-14446.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
polluteScopeWithAlwaysIterableForeach: false
49 changes: 49 additions & 0 deletions tests/PHPStan/Analyser/data/bug-14446.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types = 1);

namespace Bug14446;

use function PHPStan\Testing\assertType;

function test(bool $initial): void {
$current = $initial;

while (true) {
assertType('bool', $initial);
if (!$current) {
assertType('bool', $initial);
break;
}

$items = [1];
foreach ($items as $item) {
$current = false;
}
}

assertType('bool', $initial);
}

/**
* @param mixed $value
*/
function testForeachKeyOverwrite($value): void {
if (is_array($value) && $value !== []) {
$hasOnlyStringKey = true;
foreach (array_keys($value) as $key) {
if (is_int($key)) {
$hasOnlyStringKey = false;
break;
}
}

assertType('bool', $hasOnlyStringKey);

if ($hasOnlyStringKey) {
// $key should not be in scope here with polluteScopeWithAlwaysIterableForeach: false
// Second foreach should not report "Foreach overwrites $key with its key variable"
foreach ($value as $key => $element) {
assertType('(int|string)', $key);
}
}
}
}
Loading