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

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

Presented on January 26 2018 at the PHPBenelux conference, Antwerp, Belgium.
https://conference.phpbenelux.eu/2018/
---------------------------------------------------------------
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 properties: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Customisable-Sniff-Properties
* Step-by-step strategy: https://github.com/xwp/wp-dev-lib#limiting-scope-of-checks
* Feedback: https://joind.in/23051

2776198ea9584b6c0d4b494293b8d635?s=128

Juliette Reinders Folmer

January 26, 2018
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. Running PHP_CodeSniffer phpcs /path/to/code –-standard=StandardName -p (show progress) -s (show

    source/errorcodes) -l (local, do not recurse into subdirs) -n (no warnings) -vvv (verbosity) -i (show installed standards) -h (help, shows lots more options)
  12. PHPCS Build-in Standards PEAR PSR1 PSR2 Zend MySource PHPCS Squiz

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

    Object Calisthenics PHP Compatibility Variable Analysis Security Audit
  14. How It Works Standard (ruleset) Sniffs Checks (errorcodes)

  15. None
  16. Report Types diff code (3.0+) svnblame gitblame emacs xml checkstyle

    source full summary info csv json junit
  17. None
  18. Jack Moreh

  19. None
  20. ruleset.xml .phpcs.xml.dist • Add new sniffs • Register path with

    PHPCS
  21. Custom Ruleset Files Command line --standard=myphpcs.xml .phpcs.xml (PHPCS 3.1.0+) phpcs.xml

    .phpcs.xml.dist (PHPCS 3.1.0+) phpcs.xml.dist
  22. Standard Standard A Standard B Standard A Standard B Standard

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

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

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

    /> <rule ref="PSR2" /> </ruleset>
  26. Standard.Subset.Sniff.ErrorCode Standard.Subset.Sniff.ErrorCode Standard.Subset.Sniff.ErrorCode Standard.Subset.Sniff.ErrorCode

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

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

    name="Generic.Files.LineEndings" /> </rule> <config name="testVersion" value="5.4-"/> <rule ref="PHPCompatibility"> <exclude name="PHPCompatibility.PHP. DeprecatedIniDirectives.safe_modeRemoved"> </rule> </ruleset> Standard A Standard B
  29. <?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.4-"/> <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
  30. <?xml version="1.0"?> <ruleset name="MyProject"> <file>.</file> <arg value="ps"/> <arg name="report" value="summary"/>

    <arg name="extensions" value="php,inc,js,css,lib/php"/> <ini name="memory_limit" value="128M" /> <arg name="parallel" value="8"/> <config name="installed_paths" value="./vendor/org/lib"/> </ruleset> phpcs . -p –s --report=summary --extensions= php,inc,js,css,lib/php -d memory_limit=128M --parallel=8 (PHPCS 3+)
  31. Explain

  32. phpcs --standard=PSR2 --generator=Text (Markdown|HTML)

  33. 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>
  34. 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>
  35. 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>
  36. fui

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

  38. --filter=gitmodified

  39. Fixing errors phpcbf /path/to/code –-standard=.phpcs.xml.dist phpcbf /path/to/code –-standard=.phpcs.xml.dist --suffix=.fixed

  40. PHPCS vs PHPCBF phpcs-only="true" vs phpcbf-only="true"

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

    // @codingStandardsIgnoreStart ... <code> ... // @codingStandardsIgnoreEnd // @codingStandardsIgnoreFile - pre-PHPCS 3.2 (Deprecated)
  42. Handling Rule Exceptions // phpcs:ignore Stnd.Cat.Sniff <code line to be

    ignored> // phpcs:disable StndA,StndB.Cat.Sniff -- for reasons ... <code> ... // phpcs:enable // phpcs:ignoreFile
  43. Marnhe du Plooy

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

    clone -b master --depth 1 https://github.com/squizlabs/PHP_CodeSniffer.git $PHPCS_DIR script: - $PHPCS_DIR/bin/phpcs . –-standard=.phpcs.xml.dist --runtime-set ignore_warnings_on_exit 1
  45. Only run PHPCS once per build.

  46. Running PHPCS Selectively matrix: include: ... - php: '7.1' 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/bin/phpcs; fi
  47. Kaboompics

  48. Review Your Ruleset Jack Moreh

  49. JR P

  50. unknown Questions ?

  51. 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/23051