-
Notifications
You must be signed in to change notification settings - Fork 582
Narrow foreach body to non-empty regardless of polluteScopeWithAlwaysIterableForeach #6094
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.2.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 Bug13312NoPolluteTest extends TypeInferenceTestCase | ||
| { | ||
|
|
||
| public static function dataFileAsserts(): iterable | ||
| { | ||
| yield from self::gatherAssertTypes(__DIR__ . '/data/bug-13312-no-pollute.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-13312-no-pollute.neon', | ||
| ]; | ||
| } | ||
|
|
||
| } |
| 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 Bug13312StableTest extends TypeInferenceTestCase | ||
| { | ||
|
|
||
| public static function dataFileAsserts(): iterable | ||
| { | ||
| yield from self::gatherAssertTypes(__DIR__ . '/data/bug-13312-stable.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-13312-stable.neon', | ||
| ]; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| parameters: | ||
| polluteScopeWithAlwaysIterableForeach: false | ||
| featureToggles: | ||
| narrowForeachBodyNonEmpty: true | ||
|
Comment on lines
+2
to
+4
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need more tests with other combinations of this toggles?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| parameters: | ||
| polluteScopeWithAlwaysIterableForeach: false |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug13312NoPollute; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** @param list<mixed> $arr */ | ||
| function foo(array $arr): void { | ||
| assertType('list<mixed>', $arr); | ||
| foreach ($arr as $v) { | ||
| assertType('non-empty-list<mixed>', $arr); | ||
| } | ||
| assertType('list<mixed>', $arr); | ||
|
|
||
| for ($i = 0; $i < count($arr); ++$i) { | ||
| assertType('non-empty-list<mixed>', $arr); | ||
| } | ||
| assertType('list<mixed>', $arr); | ||
| } | ||
|
|
||
| /** @param array<string, int> $arr */ | ||
| function fooStringKeyed(array $arr): void { | ||
| assertType('array<string, int>', $arr); | ||
| foreach ($arr as $v) { | ||
| assertType('non-empty-array<string, int>', $arr); | ||
| } | ||
| assertType('array<string, int>', $arr); | ||
| } | ||
|
|
||
| /** @param list<mixed> $arr */ | ||
| function fooReassign(array $arr): void { | ||
| foreach ($arr as $v) { | ||
| $arr = []; | ||
| assertType('array{}', $arr); | ||
| } | ||
| assertType('array{}', $arr); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug13312Stable; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| // With polluteScopeWithAlwaysIterableForeach off and narrowForeachBodyNonEmpty off | ||
| // (the stable default), the foreach body does not narrow the iterated expression. | ||
| // The for-loop narrowing is a separate mechanism and still applies. | ||
|
|
||
| /** @param list<mixed> $arr */ | ||
| function foo(array $arr): void { | ||
| assertType('list<mixed>', $arr); | ||
| foreach ($arr as $v) { | ||
| assertType('list<mixed>', $arr); | ||
| } | ||
| assertType('list<mixed>', $arr); | ||
|
|
||
| for ($i = 0; $i < count($arr); ++$i) { | ||
| assertType('non-empty-list<mixed>', $arr); | ||
| } | ||
| assertType('list<mixed>', $arr); | ||
| } | ||
|
|
||
| /** @param array<string, int> $arr */ | ||
| function fooStringKeyed(array $arr): void { | ||
| assertType('array<string, int>', $arr); | ||
| foreach ($arr as $v) { | ||
| assertType('array<string, int>', $arr); | ||
| } | ||
| assertType('array<string, int>', $arr); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.