Download - Standards: Don't pee in the pool
![Page 1: Standards: Don't pee in the pool](https://reader034.vdocuments.us/reader034/viewer/2022051709/586e12011a28ab8a588b5947/html5/thumbnails/1.jpg)
Standards
Don’t pee in the pool
![Page 2: Standards: Don't pee in the pool](https://reader034.vdocuments.us/reader034/viewer/2022051709/586e12011a28ab8a588b5947/html5/thumbnails/2.jpg)
Why use a standard?
● Silos suck● Collaboration● Portability● Readability● Easier to share● Consistent diffs
![Page 3: Standards: Don't pee in the pool](https://reader034.vdocuments.us/reader034/viewer/2022051709/586e12011a28ab8a588b5947/html5/thumbnails/3.jpg)
Which standard should I pick?
Your framework standard
or
PSR2http://www.php-fig.org/psr/psr-2/
![Page 4: Standards: Don't pee in the pool](https://reader034.vdocuments.us/reader034/viewer/2022051709/586e12011a28ab8a588b5947/html5/thumbnails/4.jpg)
ToolingPHPCS
Code sniffer for enforcing standardsPHPCBF
Code beautifier and fixer to automatically fix most phpcs errors
PHPMDMess detector for finding suboptimal code
$ composer require --dev squizlabs/php_codesniffer
$ composer require --dev phpmd/phpmd$ vendor/bin/phpmd Example.php text cleancode
![Page 5: Standards: Don't pee in the pool](https://reader034.vdocuments.us/reader034/viewer/2022051709/586e12011a28ab8a588b5947/html5/thumbnails/5.jpg)
$ 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
![Page 6: Standards: Don't pee in the pool](https://reader034.vdocuments.us/reader034/viewer/2022051709/586e12011a28ab8a588b5947/html5/thumbnails/6.jpg)
$ 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
![Page 7: Standards: Don't pee in the pool](https://reader034.vdocuments.us/reader034/viewer/2022051709/586e12011a28ab8a588b5947/html5/thumbnails/7.jpg)
$ vendor/bin/phpcbf --standard=vendor/cakephp/cakephp-codesniffer/CakePHP/ src/Controller/AppController.php
Changing into directory /Users/david/Sites/Example/src/ControllerProcessing AppController.php [PHP => 1026 tokens in 146 lines]... DONE in 55ms (1 fixable violations) => Fixing file: 0/1 violations remaining [made 2 passes]... DONE in 125msPatched 1 fileTime: 241ms; Memory: 8Mb
Fix it automatically
![Page 8: Standards: Don't pee in the pool](https://reader034.vdocuments.us/reader034/viewer/2022051709/586e12011a28ab8a588b5947/html5/thumbnails/8.jpg)
PHPStorm integration
https://www.jetbrains.com/help/phpstorm/2016.3/using-php-code-sniffer-tool.html
![Page 9: Standards: Don't pee in the pool](https://reader034.vdocuments.us/reader034/viewer/2022051709/586e12011a28ab8a588b5947/html5/thumbnails/9.jpg)
PHPStorm integration
![Page 10: Standards: Don't pee in the pool](https://reader034.vdocuments.us/reader034/viewer/2022051709/586e12011a28ab8a588b5947/html5/thumbnails/10.jpg)
PHPStorm integration
![Page 11: Standards: Don't pee in the pool](https://reader034.vdocuments.us/reader034/viewer/2022051709/586e12011a28ab8a588b5947/html5/thumbnails/11.jpg)
Creating a ‘pre-commit’ hook for Git to run PHPCS when code is committed.
Commit hooks
$ vim .git/hooks/pre-commit
![Page 12: Standards: Don't pee in the pool](https://reader034.vdocuments.us/reader034/viewer/2022051709/586e12011a28ab8a588b5947/html5/thumbnails/12.jpg)
Thanks!Happy Holidays!