Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

Juliette Reinders Folmer

June 09, 2017
Tweet

More Decks by Juliette Reinders Folmer

Other Decks in Programming

Transcript

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

    View full-size slide

  2. Hello!
    Juliette
    Reinders Folmer
    @jrf_nl @jrfnl

    View full-size slide

  3. PHP CodeSniffer
    by

    View full-size slide

  4. Justin Luebke

    View full-size slide

  5. GlennForrest

    View full-size slide

  6. Static Analysis
    • Tokenizes
    • Per file
    • Light weight
    Dynamic Analysis
    • Parses
    • Across files

    View full-size slide

  7. Code Style
    Documentation
    Code Smells
    Code Metrics
    Best
    Practices
    Code
    Compatibility

    View full-size slide

  8. PHP
    (+ inc)
    JS
    CSS

    View full-size slide

  9. Phar Composer Git clone PEAR
    Installation

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  12. Report Types
    diff
    code (3.x)
    svnblame
    gitblame
    emacs
    xml
    checkstyle
    source
    full
    summary
    info
    csv
    json
    junit

    View full-size slide

  13. ruleset.xml phpcs.xml
    • Add new sniffs
    • Register path with PHPCS

    View full-size slide

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

    View full-size slide

  15. Standard
    Standard
    A
    Standard
    B
    Standard
    A
    Standard
    B
    Standard
    A
    Standard
    B

    View full-size slide






  16. Standard
    A
    Standard
    B

    View full-size slide

  17. Standard
    A
    Standard
    B





    View full-size slide

  18. Standard.Subset.Sniff.ErrorCode
    Standard.Subset.Sniff.ErrorCode
    Standard.Subset.Sniff.ErrorCode
    Standard.Subset.Sniff.ErrorCode

    View full-size slide

  19. Standard.Subset.Sniff.ErrorCode
    Include:

    Exclude:

    ~~~~~~~~

    View full-size slide












  20. Standard
    A
    Standard
    B

    View full-size slide



  21. */vendor/*









    */MyFile.php




    Standard
    A
    Standard
    B

    View full-size slide





  22. value="php,inc,js,css,lib/php"/>


    phpcs .
    -p –s
    -report=summary
    -extensions=
    php,inc,js,css,lib/php
    -d memory_limit=128M

    View full-size slide

  23. Customizing
    Messages


    ref="Generic.Commenting.Todo.CommentFound">
    Please review this TODO
    comment: %s
    8
    error


    View full-size slide

  24. Customizing
    Sniffs





    value="100"/>



    View full-size slide

  25. Customizing
    Sniffs




    type="array"
    value="delete=>unset,print=>echo,
    create_function=>null" />



    View full-size slide

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

    View full-size slide

  27. --filter=gitmodified

    View full-size slide

  28. Fixing errors
    phpcbf /path/to/code –-standard=phpcs.xml
    phpcbf /path/to/code –-standard=phpcs.xml
    --suffix=.fixed
    phpcs /path/to/code –-standard=phpcs.xml
    –-report=diff -vv

    View full-size slide

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

    View full-size slide

  30. Handling Rule Exceptions
    // @codingStandardsIgnoreLine

    // @codingStandardsIgnoreStart
    ...

    ...
    // @codingStandardsIgnoreEnd
    // @codingStandardsIgnoreFile

    View full-size slide

  31. Marnhe du Plooy

    View full-size slide

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

    View full-size slide

  33. Only run PHPCS once
    per build.

    View full-size slide

  34. 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

    View full-size slide

  35. Review
    Your
    Ruleset
    Jack Moreh

    View full-size slide

  36. 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/21480

    View full-size slide

  37. unknown
    Questions ?
    Want to get started ?
    Join me tomorrow in the Hands on Space @ 11 am.

    View full-size slide