Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/Type/PHPUnit/Assert/AssertTypeSpecifyingExtensionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,18 @@ private static function createExpression(
{
$trimmedName = self::trimName($name);
$resolvers = self::getExpressionResolvers();
$isNegated = strpos($name, 'Not') !== false;
if ($isNegated && array_key_exists('Not' . $trimmedName, $resolvers)) {
return $resolvers['Not' . $trimmedName]($scope, ...$args);
}
Comment on lines +121 to +124

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?


$resolver = $resolvers[$trimmedName];
$expression = $resolver($scope, ...$args);
if ($expression === null) {
return null;
}

if (strpos($name, 'Not') !== false) {
if ($isNegated) {
$expression = new BooleanNot($expression);
}

Expand Down Expand Up @@ -301,6 +306,14 @@ private static function getExpressionResolvers(): array
new FuncCall(new Name('in_array'), [$needle, $haystack, new Arg(new ConstFetch(new Name('false')))]),
),
),
'NotContains' => static fn (Scope $scope, Arg $needle, Arg $haystack): Expr => new Expr\BinaryOp\BooleanOr(
new Expr\Instanceof_($haystack->value, new Name('Traversable')),
new Expr\BooleanNot(new FuncCall(new Name('in_array'), [$needle, $haystack, new Arg(new ConstFetch(new Name('true')))])),
),
'NotContainsEquals' => static fn (Scope $scope, Arg $needle, Arg $haystack): Expr => new Expr\BinaryOp\BooleanOr(
new Expr\Instanceof_($haystack->value, new Name('Traversable')),
new Expr\BooleanNot(new FuncCall(new Name('in_array'), [$needle, $haystack, new Arg(new ConstFetch(new Name('false')))])),
),
'ContainsOnlyInstancesOf' => static fn (Scope $scope, Arg $className, Arg $haystack): Expr => new Expr\BinaryOp\BooleanOr(
new Expr\Instanceof_($haystack->value, new Name('Traversable')),
new Identical(
Expand Down
26 changes: 26 additions & 0 deletions tests/Type/PHPUnit/data/assert-function.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use function PHPUnit\Framework\assertArrayHasKey;
use function PHPUnit\Framework\assertContains;
use function PHPUnit\Framework\assertContainsEquals;
use function PHPUnit\Framework\assertNotContains;
use function PHPUnit\Framework\assertNotContainsEquals;
use function PHPUnit\Framework\assertContainsOnlyInstancesOf;
use function PHPUnit\Framework\assertCount;
use function PHPUnit\Framework\assertNotCount;
Expand Down Expand Up @@ -86,6 +88,30 @@ public function containsEquals(array $a, \Traversable $b): void
assertType('Traversable', $b);
}

/**

@staabm staabm Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)         │
  └────────────────────────────────────────────────────────────────┴─────────────────────────────────────────────┴─────────────────────────────────────┘

* @param 'bar'|'foo' $c
*/
public function notContains(array $a, \Traversable $b, string $c): void
{
assertNotContains('foo', $a);
assertType("array<mixed~'foo'>", $a);

assertNotContains('foo', $b);
assertType('Traversable', $b);

assertNotContains($c, ['foo']);
assertType("'bar'", $c);
}

public function notContainsEquals(array $a, \Traversable $b): void
{
assertNotContainsEquals('foo', $a);
assertType('array', $a);

assertNotContainsEquals('foo', $b);
assertType('Traversable', $b);
}

public function containsOnlyInstancesOf(array $a, \Traversable $b): void
{
assertContainsOnlyInstancesOf(\stdClass::class, $a);
Expand Down