Don't work for PHPCS, make PHPCS work for you

Don't work for PHPCS, make PHPCS work for you

Presented on February 3 2017 at SunshinePHP, Miami, Florida, United States.
http://2017.sunshinephp.com/
---------------------------------------------------------------
Congratulations! Your team has chosen a coding standard to use and you're well on your way to a consistent code style for all your projects. But... there are some extra things you'd like to check for, some rules you really can't be bothered with and some which sort of fit your needs, but not completely. Now what?

Come and learn how to make the PHP Codesniffer work for you and how to streamline the PHPCS related work-flow along the way.

Links:
* Code: https://github.com/jrfnl/make-phpcs-work-for-you
* Docs: https://github.com/squizlabs/PHP_Codesniffer/wiki
* Customizable sniff properties: https://gist.github.com/jrfnl/ff1555af507a03f3df00462b94d029ab (will be added to the PHPCS wiki in due time)
* Step-by-step strategy: https://github.com/xwp/wp-dev-lib#limiting-scope-of-checks
* Feedback: https://joind.in/20086

2776198ea9584b6c0d4b494293b8d635?s=128

Juliette Reinders Folmer

February 03, 2017
Tweet

Transcript

  1. Don't work for PHPCS Make PHPCS work for you

  2. Hello! Juliette Reinders Folmer @jrf_nl @jrfnl

  3. PHP CodeSniffer by

  4. Justin Luebke

  5. GlennForrest

  6. cohdra

  7. Static Analysis • Tokenizes • Per file • Light weight

    Dynamic Analysis • Parses • Across files
  8. Code Style Documentation Code Smells Code Metrics Best Practices Code

    Compatibility
  9. PHP (+ inc) JS CSS

  10. Phar Composer Git clone PEAR Installation

  11. PHPCS Build-in Standards PEAR PSR1 PSR2 Zend MySource PHPCS Squiz

  12. Userland PHPCS Standards Laravel Symfony1 Symfony2 CodeIgniter Drupal Magento WordPress

  13. None
  14. Report Types diff svnblame gitblame emacs xml checkstyle source full

    summary info csv json junit
  15. None
  16. Jack Moreh

  17. None
  18. ruleset.xml phpcs.xml • Add new sniffs • Register path with

    PHPCS
  19. Custom Ruleset Files Command line --standard=myphpcs.xml phpcs.xml phpcs.xml.dist

  20. Standard Standard A Standard B Standard A Standard B Standard

    A Standard B
  21. <?xml version="1.0"?> <ruleset name="MyProject"> <rule ref="PSR1" /> </ruleset> Standard

  22. <?xml version="1.0"?> <ruleset name="MyProject"> <rule ref="PSR1" /> <rule ref="PSR2" />

    </ruleset> Standard A Standard B
  23. Standard.Subset.Sniff.ErrorCode Standard.Subset.Sniff.ErrorCode Standard.Subset.Sniff.ErrorCode Standard.Subset.Sniff.ErrorCode

  24. Standard.Subset.Sniff.ErrorCode Include: <rule ref="…" /> Exclude: <exclude name="…" /> ~~~~~~~~

  25. <?xml version="1.0"?> <ruleset name="MyProject"> <rule ref="PSR1" /> <rule ref="PSR2"> <exclude

    name="Generic.Files.LineEndings" /> </rule> <config name="testVersion" value="5.1-99.0"/> <rule ref="PHPCompatibility"> <exclude name="PHPCompatibility.PHP. DeprecatedIniDirectives.safe_modeRemoved"> </rule> </ruleset> Standard A Standard B
  26. <?xml version="1.0"?> <ruleset name="MyProject"> <exclude-pattern>*/vendor/*</exclude-pattern> <rule ref="PSR1" /> <rule ref="PSR2">

    <exclude name="Generic.Files.LineEndings" /> </rule> <config name="testVersion" value="5.1-99.0"/> <rule ref="PHPCompatibility"> <exclude name="PHPCompatibility.PHP. DeprecatedIniDirectives.safe_modeRemoved"> </rule> <rule ref="PHPCompatibility.PHP.Deprecated IniDirectives.magic_quotes_runtimeRemoved"> <exclude-pattern>*/MyFile.php</exclude-pattern> </rule> <rule ref="Generic.Metrics.NestingLevelSniff" /> <rule ref="PEAR.Commenting" /> </ruleset> Standard A Standard B
  27. <?xml version="1.0"?> <ruleset name="MyProject"> <arg value="ps"/> <arg name="report" value="summary"/> <arg

    name="extensions" value="php,inc,js,css,lib/php"/> <ini name="memory_limit" value="128M" /> </ruleset> phpcs . -p –s -report=summary -extensions= php,inc,js,css,lib/php -d memory_limit=128M
  28. Explain

  29. Customizing Messages <?xml version="1.0"?> <ruleset name="MyProject"> <rule ref="Generic.Commenting.Todo.CommentFound"> <message>Please review

    this TODO comment: %s</message> <severity>8</severity> <type>error</type> </rule> </ruleset>
  30. Customizing Sniffs <?xml version="1.0"?> <ruleset name="MyProject"> <rule ref="Generic.Files.LineLength"> <properties> <property

    name="lineLimit" value="90"/> <property name="absoluteLineLimit" value="100"/> </properties> </rule> </ruleset>
  31. Customizing Sniffs <?xml version="1.0"?> <ruleset name="MyProject"> <rule ref="Generic.PHP.ForbiddenFunctions"> <properties> <property

    name="forbiddenFunctions" type="array" value="delete=>unset,print=>echo, create_function=>null" /> </properties> </rule> </ruleset>
  32. fui

  33. Strategy for Existing Codebases XWP: wp-dev-lib

  34. Fixing errors phpcbf /path/to/code –-standard=phpcs.xml --no-patch phpcbf /path/to/code –-standard=phpcs.xml --suffix=.fixed

    phpcs /path/to/code –-standard=phpcs.xml –-report=diff -vv
  35. PHPCS vs PHPCBF phpcs-only="true" vs phpcbf-only="true"

  36. Handling Rule Exceptions // @codingStandardsIgnoreLine <code line to be ignored>

    // @codingStandardsIgnoreStart ... <code> ... // @codingStandardsIgnoreEnd // @codingStandardsIgnoreFile
  37. Marnhe du Plooy

  38. Running PHPCS via Travis before_install: - export PHPCS_DIR=/tmp/phpcs - export

    PHPCOMPAT_DIR=/tmp/phpcompat/stnd - git clone -b master --depth 1 https://github.com/squizlabs/PHP_CodeSniffer.git $PHPCS_DIR - git clone -b master --depth 1 https://github.com/wimg/PHPCompatibility.git $PHPCOMPAT_DIR - cd $PHPCS_DIR - $PHPCS_DIR/scripts/phpcs --config-set installed_paths /tmp/phpcompat - cd $TRAVIS_BUILD_DIR - phpenv rehash script: - $PHPCS_DIR/scripts/phpcs . –-standard=phpcs.xml
  39. Only run PHPCS once per build.

  40. Running PHPCS Selectively matrix: include: ... - php: '5.6' env:

    SNIFF=1 before_install: ... - if [[ "$SNIFF" == "1" ]]; then git clone -b master --depth 1 https://github.com/squizlabs/PHP_CodeSniffer.git $PHPCS_DIR; fi script: - if [[ "$SNIFF" == "1" ]]; then $PHPCS_DIR/scripts/phpcs; fi
  41. Kaboompics

  42. Review Your Ruleset Jack Moreh

  43. PHPCS 3.0 Coming soon...

  44. JR P

  45. Thanks! Slides: https://speakerdeck.com/jrf Code: https://github.com/jrfnl/ make-phpcs-work-for-you Docs: https://github.com/squizlabs/ PHP_Codesniffer/wiki Feedback:

    https://joind.in/20086
  46. unknown Questions ?