Skip to content

UnnecessaryAssertion ​

info on by default quick-fix

Group: PHPUnit · PhpStorm name: UnnecessaryAssertionInspection

Some PHPUnit assertions verify something the language already guarantees: asserting null/emptiness on the result of a void function, or the class or type of a value returned by a function with a declared return type. Also, ->expects($this->any()) on a mock asserts nothing and can be dropped.

Example ​

At PHP 7.4:

php
<?php

namespace Shop;

class Cart {}

abstract class CartTest
{
    abstract protected function flush(): void;
    abstract protected function cart(): Cart;
    abstract protected function maybeCart(): ?Cart;
    abstract protected function anything();

    public function testTyped(bool $flag)
    {
        $this->assertNull($this->flush());
        self::assertEmpty($this->flush());
        $this->assertInstanceOf(Cart::class, $this->cart());
        $this->assertInternalType('object', $this->cart());
        $this->assertInternalType('object', $this->cart());
        $this->assertInternalType('array', $this->cart());
        $this->assertInstanceOf(\ArrayObject::class, $this->cart());
        $this->assertInstanceOf(Cart::class, $this->maybeCart());
        $this->assertNull($this->cart());
        $this->assertNull($this->anything());

        $made = $this->cart();
        $this->assertInstanceOf(Cart::class, $made);

        $either = $flag ? $this->cart() : $this->flush();
        $this->assertNull($either);
    }

    public function testMatcher()
    {
        $double = $this->createMock(Cart::class);
        $double->expects($this->any())->method('total')->willReturn(9);
        $double
            ->expects(self::any())
            ->method('count');
        $double->expects($this->once())->method('total');
    }
}
php
<?php

namespace Shop;

class Cart {}

abstract class CartTest
{
    abstract protected function flush(): void;
    abstract protected function cart(): Cart;
    abstract protected function maybeCart(): ?Cart;
    abstract protected function anything();

    public function testTyped(bool $flag)
    {
        $this->assertNull($this->flush());
        self::assertEmpty($this->flush());
        $this->assertInstanceOf(Cart::class, $this->cart());
        $this->assertInternalType('object', $this->cart());
        $this->assertInternalType('array', $this->cart());
        $this->assertInstanceOf(\ArrayObject::class, $this->cart());
        $this->assertInstanceOf(Cart::class, $this->maybeCart());
        $this->assertNull($this->cart());
        $this->assertNull($this->anything());

        $made = $this->cart();
        $this->assertInstanceOf(Cart::class, $made);

        $either = $flag ? $this->cart() : $this->flush();
        $this->assertNull($either);
    }

    public function testMatcher()
    {
        $double = $this->createMock(Cart::class);
        $double->method('total')->willReturn(9);
        $double
            ->method('count');
        $double->expects($this->once())->method('total');
    }
}

Reported:

  • line 16: The declared return type already guarantees this; the assertion can go.
  • line 17: The declared return type already guarantees this; the assertion can go.
  • line 18: The declared return type already guarantees this; the assertion can go.
  • line 19: The declared return type already guarantees this; the assertion can go.
  • line 28: The declared return type already guarantees this; the assertion can go.
  • line 37: expects(any()) verifies nothing; drop the expects() call.
  • line 39: expects(any()) verifies nothing; drop the expects() call.

Configure ​

In custos.json:

json
{
  "rules": {
    "UnnecessaryAssertion": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore UnnecessaryAssertion

/**
 * @noinspection UnnecessaryAssertionInspection
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.