-
Notifications
You must be signed in to change notification settings - Fork 578
Report always-true in_array(..., true) when every finite needle value is guaranteed in the haystack
#5944
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
Merged
staabm
merged 10 commits into
phpstan:2.2.x
from
phpstan-bot:create-pull-request/patch-u73aorw
Jun 29, 2026
Merged
Report always-true in_array(..., true) when every finite needle value is guaranteed in the haystack
#5944
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6b6dfca
Report always-true `in_array(..., true)` when every finite needle val…
VincentLanglet a8f5f99
Update bug-14873.php
staabm 0f0314b
more tests
staabm 0db41f4
Do not report always-true in_array() for needles without finite values
phpstan-bot 74be15a
strip comments
staabm a9635ab
Bail conservatively for general-array haystack in strict in_array()
phpstan-bot 565576e
Explain why the combined in_array() root expression is cleared
phpstan-bot 55d0901
Add rule-level coverage for general-array haystacks in strict in_array()
phpstan-bot 201382d
Shorten the general-array haystack comment
phpstan-bot b282391
cs
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,114 @@ | ||
| <?php // lint >= 8.1 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug14873; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
| use function in_array; | ||
|
|
||
| enum Suit: string | ||
| { | ||
|
|
||
| case Hearts = 'H'; | ||
| case Spades = 'S'; | ||
| case Clubs = 'C'; | ||
|
|
||
| } | ||
|
|
||
| class HelloWorld | ||
| { | ||
|
|
||
| /** | ||
| * @param 'a'|'b'|'c' $full | ||
| * @param 'a'|'b' $subset | ||
| * @param 'a'|'x' $partial | ||
| */ | ||
| public function variableHaystack(string $full, string $subset, string $partial): void | ||
| { | ||
| $a = ['a', 'b', 'c']; | ||
|
|
||
| assertType('true', in_array($full, $a, true)); | ||
| assertType('true', in_array($subset, $a, true)); | ||
| assertType('bool', in_array($partial, $a, true)); | ||
| } | ||
|
|
||
| /** | ||
| * @param 'a'|'b'|'c' $full | ||
| * @param 'a'|'b' $subset | ||
| * @param 'a'|'x' $partial | ||
| */ | ||
| public function literalHaystack(string $full, string $subset, string $partial): void | ||
| { | ||
| assertType('true', in_array($full, ['a', 'b', 'c'], true)); | ||
| assertType('bool', in_array($full, ['a', 'b', 'c'], false)); // non-strict | ||
| assertType('true', in_array($subset, ['a', 'b', 'c'], true)); | ||
| assertType('bool', in_array($partial, ['a', 'b', 'c'], true)); | ||
| assertType('false', in_array($subset, ['x', 'y'], true)); | ||
|
|
||
| $fullOrEmpty = rand(0,1) ? $full : []; | ||
| assertType('bool', in_array($fullOrEmpty, ['a', 'b', 'c'], true)); | ||
| } | ||
|
|
||
| /** | ||
| * @param 1|2 $full | ||
| * @param 1 $subset | ||
| */ | ||
| public function integers(int $full, int $subset): void | ||
| { | ||
| $a = [1, 2]; | ||
|
|
||
| assertType('true', in_array($full, $a, true)); | ||
| assertType('true', in_array($subset, $a, true)); | ||
| assertType('true', in_array($full, [1, 2, 3], true)); | ||
| } | ||
|
|
||
| /** | ||
| * @param Suit::Hearts|Suit::Spades $subset | ||
| */ | ||
| public function enums(Suit $subset): void | ||
| { | ||
| $a = [Suit::Hearts, Suit::Spades, Suit::Clubs]; | ||
|
|
||
| assertType('true', in_array($subset, $a, true)); | ||
| assertType('true', in_array($subset, [Suit::Hearts, Suit::Spades, Suit::Clubs], true)); | ||
| assertType('bool', in_array($subset, [Suit::Hearts], true)); | ||
| } | ||
|
|
||
| /** | ||
| * Plain objects do not have a finite set of possible values, so in_array() | ||
| * must not be reported as always-true even when the needle's class matches | ||
| * every haystack value type. | ||
| */ | ||
| public function objects(Article $article, ?Article $a, ?Article $b): void | ||
| { | ||
| $haystack = [$a, $b]; | ||
|
|
||
| assertType('bool', in_array($article, $haystack, true)); | ||
| assertType('bool', in_array($article, [$a, $b], true)); | ||
| } | ||
|
|
||
| /** | ||
| * A general (non-constant) array does not guarantee that any particular value | ||
| * is present, so a subset needle must not be reported as always-true. Only a | ||
| * non-empty array whose values all share a single finite type guarantees that | ||
| * value's presence. | ||
| * | ||
| * @param 1|2 $needle | ||
| * @param array<int, 1|2> $maybeEmpty | ||
| * @param non-empty-array<int, 1|2> $nonEmptyMulti | ||
| * @param non-empty-array<int, 1> $nonEmptySingle | ||
| */ | ||
| public function generalArrays(int $needle, array $maybeEmpty, array $nonEmptyMulti, array $nonEmptySingle): void | ||
| { | ||
| assertType('bool', in_array($needle, $maybeEmpty, true)); | ||
| assertType('bool', in_array($needle, $nonEmptyMulti, true)); | ||
| assertType('true', in_array(1, $nonEmptySingle, true)); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| class Article | ||
| { | ||
|
|
||
| } |
This file contains hidden or 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 hidden or 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,50 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug14873Rule; | ||
|
|
||
| class HelloWorld | ||
| { | ||
|
|
||
| /** | ||
| * @param 'a'|'b'|'c' $full | ||
| * @param 'a'|'b' $subset | ||
| * @param 'a'|'x' $partial | ||
| */ | ||
| public function sayHello(string $full, string $subset, string $partial): void | ||
| { | ||
| $a = ['a', 'b', 'c']; | ||
|
|
||
| if (in_array($full, $a, true)) { | ||
| echo 'full'; | ||
| } | ||
|
|
||
| if (in_array($subset, $a, true)) { | ||
| echo 'subset'; | ||
| } | ||
|
|
||
| if (in_array($partial, $a, true)) { | ||
| echo 'partial'; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A general (non-constant) array does not guarantee that any particular value | ||
| * is present, so a subset needle must not be reported as always-true - even | ||
| * when every finite needle value appears in the array's value type. | ||
| * | ||
| * @param 1|2 $needle | ||
| * @param array<int, 1|2> $maybeEmpty | ||
| * @param non-empty-array<int, 1|2> $nonEmptyMulti | ||
| */ | ||
| public function generalArrays(int $needle, array $maybeEmpty, array $nonEmptyMulti): void | ||
| { | ||
| if (in_array($needle, $maybeEmpty, true)) { | ||
| echo 'maybeEmpty'; | ||
| } | ||
|
|
||
| if (in_array($needle, $nonEmptyMulti, true)) { | ||
| echo 'nonEmptyMulti'; | ||
| } | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need setRootExpr ?