Advertisement
Advertisement

More Related Content

Advertisement

Standards: Don't pee in the pool

  1. Standards Don’t pee in the pool
  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
  7. $ 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
  8. PHPStorm integration https://www.jetbrains.com/help/phpstorm/2016.3/using-php-code-sniffer-tool.html
  9. PHPStorm integration
  10. PHPStorm integration
  11. Creating a ‘pre-commit’ hook for Git to run PHPCS when code is committed. Commit hooks $ vim .git/hooks/pre-commit
  12. Thanks! Happy Holidays!
Advertisement