Repository navigation
Conversation
The Contains and ContainsEquals resolvers accept any Traversable haystack and only narrow arrays. Negating the whole expression for assertNotContains() and assertNotContainsEquals() turned that guard into "the haystack is not Traversable", so a Doctrine collection or an ArrayObject became *NEVER* after the assertion.
staabm
reviewed
Nov 30, 2026
Comment on lines
+121
to
+124
| $isNegated = strpos($name, 'Not') !== false; | ||
| if ($isNegated && array_key_exists('Not' . $trimmedName, $resolvers)) { | ||
| return $resolvers['Not' . $trimmedName]($scope, ...$args); | ||
| } |
Contributor
There was a problem hiding this comment.
trimName has more cases which are handled as negated. should we make trimName do the decision and return the "isNegated", so we don't need to keep this logic in sync?
| assertType('Traversable', $b); | ||
| } | ||
|
|
||
| /** |
Contributor
There was a problem hiding this comment.
please add more tests for cases which are fixed by this PR:
┌────────────────────────────────────────────────────────────────┬─────────────────────────────────────────────┬─────────────────────────────────────┐
│ Case │ Base (2.1.x) │ PR │
├────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────┼─────────────────────────────────────┤
│ Issue #262: assertNotContains($x, $arrayObject); │ reports assertCount() with 0 and *NEVER* │ no error │
│ assertCount(0, $arrayObject); │ │ │
├────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────┼─────────────────────────────────────┤
│ array<string>|ArrayObject haystack │ array<string> (wrong, the ArrayObject part │ array<string>|ArrayObject<int, │
│ │ is dropped) │ string> │
├────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────┼─────────────────────────────────────┤
│ iterable haystack, assertNotContains / assertNotContainsEquals │ array<mixed~'foo'> (wrong) │ array<mixed~'foo'>|Traversable │
├────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────┼─────────────────────────────────────┤
│ 'a'|'b' needle, Traversable haystack │ — │ stays 'a'|'b' (correct) │
├────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────┼─────────────────────────────────────┤
│ 'a'|'b' needle, ['a'] haystack │ — │ 'b' (correct) │
├────────────────────────────────────────────────────────────────┼─────────────────────────────────────────────┼─────────────────────────────────────┤
│ assertNotCount(0, $arr) │ non-empty-array │ non-empty-array (unchanged) │
└────────────────────────────────────────────────────────────────┴─────────────────────────────────────────────┴─────────────────────────────────────┘
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
assertNotContains()andassertNotContainsEquals()turn a Traversable haystack into*NEVER*, so the next use of the same expression is reported. With a Doctrine collection:PHPStan 2.3.7 with phpstan-phpunit 2.1.4 reports on the second line:
The
ContainsandContainsEqualsresolvers build$haystack instanceof Traversable || in_array(...), so that only arrays are narrowed. For the negated assertions,createExpression()wraps the whole expression inBooleanNot, which turns the guard into "the haystack is not Traversable" and removes every Traversable type from the haystack. This has been the case since #191; #197 stopped the report on the assertion call itself, but not the narrowing.NotContainsandNotContainsEqualsnow have their own resolvers that keep the guard and negate only thein_array()call, andcreateExpression()uses such aNotresolver when one exists. The existingNotCountresolver becomes reachable through the same lookup. It builds the same expression as the generic negation, soassertNotCount()does not change.Fixes #262