Skip to content

AssertEmptyNullableObjectToAssertInstanceofRector phpunit rule converting assertNull into assertInstanceOf, which isn't more narrow #9867

Description

@jaydiablo

Bug Report

Subject Details
Rector version 2.6.3

With these config settings in rector.php:

->withComposerBased(phpunit: true)
    ->withPreparedSets(phpunitCodeQuality: true)

We're seeing rector change $this->assertNull($object); into something like $this->assertNotInstanceOf(someClass::class, $object) which, IMO, defeats the purpose of this rule (I believe it's trying to narrow phpunit assertions), as this widens the assertion quite a bit.

I do agree that the opposite behavior this rule does (converts $this->assertNotNull($object) into $this->assertInstanceOf(someClass::class, $object) is helpful.

Is this the intended behavior of this rule (specifically changing assertNull)? If so, is it possible to skip just the assertNull -> assertNotInstanceOf conversion without skipping the entire rule?

Minimal PHP Code Causing Issue

https://getrector.com/demo/d97c4200-16c1-452a-8b45-b4f6b2d6a95c

Expected Behaviour

I'd expect rector to ignore the assertNull assertion, since that seems like a more narrow type than assertNotInstanceOf in this contrived example (but we saw this with real world code as well).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions