Skip to content

Commit

Permalink
Merging scopes in root scope - create maybe-existent variables from t…
Browse files Browse the repository at this point in the history
…he other scope too
  • Loading branch information
ondrejmirtes committed Nov 11, 2020
1 parent aa2907d commit 310cb83
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3353,6 +3353,14 @@ public function mergeWith(?self $otherScope): self

$ourVariableTypes[$name] = VariableTypeHolder::createMaybe(new MixedType());
}

foreach (array_keys($ourVariableTypes) as $name) {
if (array_key_exists($name, $theirVariableTypes)) {
continue;
}

$theirVariableTypes[$name] = VariableTypeHolder::createMaybe(new MixedType());
}
}

return $this->scopeFactory->create(
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/DeadCode/UnreachableStatementRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,16 @@ public function testBugWithoutGitHubIssue1(bool $treatPhpDocTypesAsCertain): voi
$this->analyse([__DIR__ . '/data/bug-without-issue-1.php'], []);
}

public function testBug4070(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-4070.php'], []);
}

public function testBug4070Two(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-4070_2.php'], []);
}

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

namespace Bug4070;

array_shift($argv);

while ($argv) {
$arg = array_shift($argv);
if ($arg === 'foo') {
continue;
}
die();
}

echo "finished\n";
17 changes: 17 additions & 0 deletions tests/PHPStan/Rules/DeadCode/data/bug-4070_2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Bug4070Two;

function () {
array_shift($argv);

while ($argv) {
$arg = array_shift($argv);
if ($arg === 'foo') {
continue;
}
die();
}

echo "finished\n";
};
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,6 @@ public function testUnpackOperator(): void
public function testFunctionWithNumericParameterThatCreatedByAddition(): void
{
$this->analyse([__DIR__ . '/data/function-with-int-parameter-that-created-by-addition.php'], [
[
'Parameter #1 $num of function dechex expects int, float|int given.',
9,
],
[
'Parameter #1 $num of function dechex expects int, float|int given.',
40,
Expand Down

0 comments on commit 310cb83

Please sign in to comment.