Menu

← Back to phpunit.*

Error Identifier: phpunit.assertCount

Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.

Code example #

<?php declare(strict_types = 1);

use PHPUnit\Framework\TestCase;

class MyTest extends TestCase
{
	public function testItems(): void
	{
		$items = [1, 2, 3];
		$this->assertSame(3, count($items));
	}
}

Why is it reported? #

This rule is part of phpstan-phpunit.

Using assertSame() with count() to verify the number of elements in an array or countable object is less readable and produces worse failure messages than the dedicated assertCount() method. PHPUnit’s assertCount() is specifically designed for this purpose and provides clearer output when the assertion fails.

This is also reported for assertSame($expected, $variable->count()) on Countable objects.

How to fix it #

Replace assertSame() with count() by using assertCount():

 public function testItems(): void
 {
 	$items = [1, 2, 3];
-	$this->assertSame(3, count($items));
+	$this->assertCount(3, $items);
 }

For Countable objects:

-$this->assertSame(3, $collection->count());
+$this->assertCount(3, $collection);

How to ignore this error #

You can use the identifier phpunit.assertCount to ignore this error using a comment:

// @phpstan-ignore phpunit.assertCount
codeThatProducesTheError();

You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:

parameters:
	ignoreErrors:
		-
			identifier: phpunit.assertCount

Rules that report this error #

  • PHPStan\Rules\PHPUnit\AssertSameWithCountRule [1] [2] phpstan/phpstan-phpunit

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.