Back to Phpstan

phpunit.assertNull

website/errors/phpunit.assertNull.md

2.2.1835 B
Original Source

Code example

php
<?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():

diff-php
 <?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);
 	}
 }