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

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

Presented on June 9th 2017 at PHP Southcoast, Portsmouth, United Kingdom.
https://2017.phpsouthcoast.co.uk/
---------------------------------------------------------------
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
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/21480

2776198ea9584b6c0d4b494293b8d635?s=128

Juliette Reinders Folmer

June 09, 2017
Tweet

Transcript

  1. 6.
  2. 7.

    Static Analysis • Tokenizes • Per file • Light weight

    Dynamic Analysis • Parses • Across files
  3. 13.
  4. 14.
  5. 15.
  6. 17.
  7. 26.

    <?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
  8. 27.

    <?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
  9. 28.

    <?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
  10. 29.
  11. 30.

    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>
  12. 31.

    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>
  13. 32.

    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>
  14. 33.

    fui

  15. 38.

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

    // @codingStandardsIgnoreStart ... <code> ... // @codingStandardsIgnoreEnd // @codingStandardsIgnoreFile
  16. 40.

    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
  17. 42.

    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/bin/phpcs; fi
  18. 45.
  19. 46.
  20. 48.

    unknown Questions ? Want to get started ? Join me

    tomorrow in the Hands on Space @ 11 am.