The document discusses standards for PHP code and tools for enforcing code standards. It recommends adopting a coding standard like PSR2 for consistency, readability, and easier code sharing. It introduces PHPCS for checking code against a standard, PHPCBF for automatically fixing issues, and PHPMD for detecting suboptimal code. It provides examples of running these tools on a codebase and automatically fixing issues found by PHPCS. Finally, it mentions integrating these tools into PHPStorm and creating a Git pre-commit hook to run PHPCS on commits.
2. Why use a standard?
Silos suck
Collaboration
Portability
Readability
Easier to share
Consistent diffs
3. Which standard should I pick?
Your framework standard
or
PSR2
http://www.php-fig.org/psr/psr-2/
4. Tooling
PHPCS
Code sniffer for enforcing standards
PHPCBF
Code beautifier and fixer to automatically fix most phpcs errors
PHPMD
Mess detector for finding suboptimal code
$ composer require --dev squizlabs/php_codesniffer
$ composer require --dev phpmd/phpmd
$ vendor/bin/phpmd Example.php text cleancode
5. $ vendor/bin/phpcs --standard=vendor/cakephp/cakephp-codesniffer/CakePHP/
src/Controller/AppController.php
FILE: .../david/Sites/Example/src/Controller/AppController.php
----------------------------------------------------------------------
FOUND 1 ERROR AND 1 WARNING AFFECTING 2 LINES
----------------------------------------------------------------------
95 | ERROR | Missing parameter comment
97 | WARNING | Function return type is not void, but function has no
return statement
----------------------------------------------------------------------
Time: 100ms; Memory: 8Mb
Check your code
6. $ vendor/bin/phpcs --standard=vendor/cakephp/cakephp-codesniffer/CakePHP/
src/Controller/AppController.php
FILE: .../david/Sites/Example/src/Controller/AppController.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
42 | ERROR | [x] Opening brace of a class must be on the line after
| | the definition
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------
Time: 99ms; Memory: 8Mb
Auto fixable errors