phpstan / phpstan-phpunit

PHPUnit extensions and rules for PHPStan
MIT License
468 stars 46 forks source link

Setting checkUninitializedProperties to true reports errors in all tests for properties that are initialised in setUp #196

Closed MetalArend closed 1 year ago

MetalArend commented 1 year ago

Many tests written for phpunit initialise properties only in the setUp method. There's not a constructor, or the need for a default value, it's enought if the setUp method handles the initialisation.

However, PHPStan's strict rules don't seem to know that. Would it be possible and correct to make this package make PHPStan understand that when checkUninitializedProperties is set to true, a property initialised in the setUp method is still perfectly fine?

ondrejmirtes commented 1 year ago

This is supposed to already work as setUp is configured as an additional constructor.

Please demonstrate how it does not work for you in a small reproducing repository, thank you. You should need just a handful of files to do that.

MetalArend commented 1 year ago

Sorry it took me a while to set up and reproduce it. Here is a repository: https://github.com/MetalArend/strict-phpstan

php vendor/bin/phpstan is producing a single error: Class App\Tests\Test has an uninitialized property $name. Give it default value or assign it in the constructor.

ondrejmirtes commented 1 year ago

You haven't configured the phpstan-phpunit extension. I recommend you to use phpstan/extension-installer, that way you don't need to include - vendor/phpstan/phpstan-strict-rules/rules.neon either.

MetalArend commented 1 year ago

:man_facepalming: :blush:

MetalArend commented 1 year ago

Thank you!

github-actions[bot] commented 1 year ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.