standards: don't pee in the pool

12
Standards Don’t pee in the pool

Upload: david-yell

Post on 16-Apr-2017

119 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Standards: Don't pee in the pool

Standards

Don’t pee in the pool

Page 2: Standards: Don't pee in the pool

Why use a standard?

● Silos suck● Collaboration● Portability● Readability● Easier to share● Consistent diffs

Page 3: Standards: Don't pee in the pool

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

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

$ 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

$ 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

$ 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

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

PHPStorm integration

Page 10: Standards: Don't pee in the pool

PHPStorm integration

Page 11: Standards: Don't pee in the pool

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

Thanks!Happy Holidays!