際際滷

際際滷Share a Scribd company logo
Standards
Dont pee in the pool
Why use a standard?
 Silos suck
 Collaboration
 Portability
 Readability
 Easier to share
 Consistent diffs
Which standard should I pick?
Your framework standard
or
PSR2
http://www.php-fig.org/psr/psr-2/
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
$ 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
$ 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
$ vendor/bin/phpcbf --standard=vendor/cakephp/cakephp-codesniffer/CakePHP/
src/Controller/AppController.php
Changing into directory /Users/david/Sites/Example/src/Controller
Processing 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
125ms
Patched 1 file
Time: 241ms; Memory: 8Mb
Fix it automatically
PHPStorm integration
https://www.jetbrains.com/help/phpstorm/2016.3/using-php-code-sniffer-tool.html
PHPStorm integration
PHPStorm integration
Creating a pre-commit hook for Git to run PHPCS
when code is committed.
Commit hooks
$ vim .git/hooks/pre-commit
Thanks!
Happy Holidays!

More Related Content

Standards: Don't pee in the pool