elijahb / phpcs-testability
PHPCodeSniffer sniffs for unit test code testability check
Installs: 281
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/elijahb/phpcs-testability
Requires (Dev)
- phpunit/phpunit: ^5.7
- squizlabs/php_codesniffer: ^2.7
This package is not auto-updated.
Last update: 2025-10-12 05:16:37 UTC
README
Introduction
This project provides sniffs for some design patterns that improve unit test code testability:
- Use dependency injection (do not create instances using new)
- Limit the length for a call chain ($this->property->service->method->troubles)
- Do not call methods of the same class (this is a sign that your code is too complicated and you should split it in different services)
- Not use static calls (Service::method())
- Use only public methods (private methods cannot be really unit tested, and they indicate that you should be thinking about creating a new service)
It also provides two other sniffs, useful for CI environments:
- Check that a test case exists for every class
- Check that there is at least one test for every method
How to use
- Install PHPCodeSniffer
- Install phpcs-testability using Composer
composer require --dev elijahb/phpcs-testability
- Add the standard to your custom PHPCodeSniffer ruleset.xml file
<rule ref="./vendor/elijahb/phpcs-testability"/>
Recommendations
You could also use another two sniffs to complement the ones in this projects:
- Generic.Metrics.NestingLevel (with a nesting level of 2)
- Generic.Metrics.CyclomaticComplexity (with a complexity level of 3)
Using all this sniffs, ideally inside a CI environment, you will be assured to have maintainable and fully unit tested code.