phpstan-phpunit icon indicating copy to clipboard operation
phpstan-phpunit copied to clipboard

Ideas for additional strict rules

Open mhujer opened this issue 7 years ago • 5 comments

Here is a list of a ideas that may be implemented in the future:

  • [ ] suggest assertInstanceOf(Foo::class, $a) instead of assertTrue($a instanceof Foo)
  • [ ] suggest assertNotInstanceOf(Foo::class, $a) instead of assertFalse($a instanceof Foo)
  • [ ] suggest assertArrayHasKey() instead of assertTrue(array_key_exists(..))
  • [ ] suggest assertFileExists() instead of assertTrue(file_exists($filePath))

mhujer avatar Dec 28 '17 15:12 mhujer

I think that assertArrayHasKey and assertFileExists are not that common and can be written in multiple ways, so I'm not sure about them.

BTW: It might be interesting to suggest assertCount and other already implemented rules also for assertEquals and not just assertSame. I know that it's a loose comparison but those more-specific asserts might also be loose comparisons as well?

ondrejmirtes avatar Dec 28 '17 16:12 ondrejmirtes

Suggestions for specific rules instead of assertEquals should be handled by https://github.com/phpstan/phpstan-phpunit/pull/6.

  1. If both operands are boolean/string/integer, it would recommend using assertSame
  2. And then the other rule would suggest changing it to a more specific assert 😄

mhujer avatar Dec 28 '17 16:12 mhujer

I have more suggestions, and if I could help implement them :smile_cat: :

  • [ ] Suggest assertCount(1, $foo) instead of assertSame(1, sizeof($foo))
  • [ ] Suggest assertContains('foo', [...]) instead of assertTrue(in_array('foo', [...]))
  • [ ] Suggest assertEmpty($foo) instead of assertTrue(empty($foo))

carusogabriel avatar Dec 30 '17 21:12 carusogabriel

Go for it 😊

On Sat, 30 Dec 2017 at 22:03, Gabriel Caruso [email protected] wrote:

I have more suggestions, and if I could help implement them 😸 :

  • Suggest assertCount(1, $foo) instead of assertSame(1, sizeof($foo))
  • Suggest assertContains('foo', [...]) instead of assertTrue(in_array('foo', [...]))
  • Suggest assertEmpty($foo) instead of assertTrue(empty($foo))

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/phpstan/phpstan-phpunit/issues/7#issuecomment-354568210, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGZuFsRghGOxp9uhDRItjYVN_z_i1_hks5tFqUDgaJpZM4ROcPW .

--

Ondřej Mirtes

ondrejmirtes avatar Dec 30 '17 21:12 ondrejmirtes

More ideas https://docs.google.com/spreadsheets/d/145hef6esG-B09AfooY-4e0oFKa1eVDoeUeo-eXVZRoA/edit?usp=sharing...

carusogabriel avatar Feb 02 '18 12:02 carusogabriel