Menu
Error Identifier: phpunit.assertNull
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 testFoo(): void
{
$value = null;
$this->assertSame(null, $value);
}
}
Why is it reported? #
This rule is part of phpstan-phpunit.
When asserting that a value is null, assertNull() should be used instead of assertSame(null, $value). The dedicated method is more readable and provides clearer failure messages.
How to fix it #
Replace assertSame(null, ...) with assertNull():
<?php declare(strict_types = 1);
use PHPUnit\Framework\TestCase;
class MyTest extends TestCase
{
public function testFoo(): void
{
$value = null;
- $this->assertSame(null, $value);
+ $this->assertNull($value);
}
}
How to ignore this error #
You can use the identifier phpunit.assertNull to ignore this error using a comment:
// @phpstan-ignore phpunit.assertNull
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.assertNull
Rules that report this error #
- PHPStan\Rules\PHPUnit\AssertSameNullExpectedRule [1] phpstan/phpstan-phpunit