Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Filter scope by non-empty array after foreach regardless of `polluteS…
…copeWithAlwaysIterableForeach`
- Loading branch information
1 parent
83ccb7f
commit 27e2b53
Showing
5 changed files
with
92 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
class Bug10922Test extends TypeInferenceTestCase | ||
{ | ||
|
||
public function dataFileAsserts(): iterable | ||
{ | ||
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10922.php'); | ||
} | ||
|
||
/** | ||
* @dataProvider dataFileAsserts | ||
* @param mixed ...$args | ||
*/ | ||
public function testFileAsserts( | ||
string $assertType, | ||
string $file, | ||
...$args, | ||
): void | ||
{ | ||
$this->assertFileAsserts($assertType, $file, ...$args); | ||
} | ||
|
||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [ | ||
__DIR__ . '/../../../conf/bleedingEdge.neon', | ||
__DIR__ . '/bug-10922.neon', | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
parameters: | ||
polluteScopeWithAlwaysIterableForeach: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Bug10922; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class HelloWorld | ||
{ | ||
/** @param array<string, array{foo: string}> $array */ | ||
public function sayHello(array $array): void | ||
{ | ||
foreach ($array as $key => $item) { | ||
$array[$key]['bar'] = ''; | ||
} | ||
assertType("array<string, array{foo: string, bar: ''}>", $array); | ||
} | ||
|
||
/** @param array<string, array{foo: string}> $array */ | ||
public function sayHello2(array $array): void | ||
{ | ||
if (count($array) > 0) { | ||
return; | ||
} | ||
|
||
foreach ($array as $key => $item) { | ||
$array[$key]['bar'] = ''; | ||
} | ||
assertType("array{}", $array); | ||
} | ||
|
||
/** @param array<string, array{foo: string}> $array */ | ||
public function sayHello3(array $array): void | ||
{ | ||
if (count($array) === 0) { | ||
return; | ||
} | ||
|
||
foreach ($array as $key => $item) { | ||
$array[$key]['bar'] = ''; | ||
} | ||
assertType("non-empty-array<string, array{foo: string, bar: ''}>", $array); | ||
} | ||
} |