Skip to content

Conversation

@peterfox
Copy link
Contributor

@peterfox peterfox commented Feb 8, 2025

Changes

  • Adds a new AssertCountWithZeroToAssertEmptyRector.
  • Adds tests for the rule.
  • Added to code quality set.

Why

This is just a small but useful rector rule. It will convert use of assertCount with a first parameter of zero to be assertEmpty. Also handles the not use as well.

-$this->assertCount(0, $array);
+$this->assertEmpty($array);
-$this->assertNotCount(0, $array);
+$this->assertNotEmpty($array);

@TomasVotruba
Copy link
Member

Thanks Peter, this looks good 👍

We could add it to the code quality set, what do you think?

@peterfox
Copy link
Contributor Author

peterfox commented Feb 8, 2025

Will do @TomasVotruba !

@TomasVotruba TomasVotruba merged commit 45b0f6b into rectorphp:main Feb 9, 2025
6 checks passed
@TomasVotruba
Copy link
Member

TomasVotruba commented Feb 9, 2025

Lets ship it 😉

Thank you

@samsonasik
Copy link
Member

@peterfox It seems buggy on combine with AssertEmptyNullableObjectToAssertInstanceofRector

-        $this->assertCount(0, $this->bar->baz());
+        $this->assertNotInstanceOf(Collection::class, $this->bar->baz());
     }
    ----------- end diff -----------

Applied rules:
 * AssertCountWithZeroToAssertEmptyRector
 * AssertEmptyNullableObjectToAssertInstanceofRector

that cause error:

Failed asserting that an object is not an instance of interface Doctrine\Common\Collections\Collection.

I will check if that resolvable

@samsonasik
Copy link
Member

@peterfox I created new PR to skip union type on :

@TomasVotruba
Copy link
Member

TomasVotruba commented Mar 19, 2025

I'm looking into this rule once again, as it actually creates less readable code. We ignore it completely on couple of projects.
empty() is too vague structure and PHPStan and Rector rules try to refactor away form it.

Also in $this->assertCount(x, ...) is more readable in context of similar counter tests:

$this->assertCount(1, ...);
$this->assertCount(5, ...);
$this->assertCount(0, ...);

Saying that, I'll deprecate and remove this rule form the set, to promote higher quality code.

Thanks for understanding 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants