$30 off During Our Annual Pro Sale. View Details »

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

Juliette Reinders Folmer

February 03, 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 Slide

  2. Hello!
    Juliette
    Reinders Folmer
    @jrf_nl @jrfnl

    View Slide

  3. PHP CodeSniffer
    by

    View Slide

  4. Justin Luebke

    View Slide

  5. GlennForrest

    View Slide

  6. cohdra

    View Slide

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

    View Slide

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

    View Slide

  9. PHP
    (+ inc)
    JS
    CSS

    View Slide

  10. Phar Composer Git clone PEAR
    Installation

    View Slide

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

    View Slide

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

    View Slide

  13. View Slide

  14. Report Types
    diff
    svnblame
    gitblame
    emacs
    xml
    checkstyle
    source
    full
    summary
    info
    csv
    json
    junit

    View Slide

  15. View Slide

  16. Jack Moreh

    View Slide

  17. View Slide

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

    View Slide

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

    View Slide

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

    View Slide





  21. Standard

    View Slide






  22. Standard
    A
    Standard
    B

    View Slide

  23. Standard.Subset.Sniff.ErrorCode
    Standard.Subset.Sniff.ErrorCode
    Standard.Subset.Sniff.ErrorCode
    Standard.Subset.Sniff.ErrorCode

    View Slide

  24. Standard.Subset.Sniff.ErrorCode
    Include:

    Exclude:

    ~~~~~~~~

    View Slide












  25. Standard
    A
    Standard
    B

    View Slide



  26. */vendor/*









    */MyFile.php




    Standard
    A
    Standard
    B

    View Slide





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


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

    View Slide

  28. Explain

    View Slide

  29. Customizing
    Messages


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


    View Slide

  30. Customizing
    Sniffs





    value="100"/>



    View Slide

  31. Customizing
    Sniffs




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



    View Slide

  32. fui

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

  36. Handling Rule Exceptions
    // @codingStandardsIgnoreLine

    // @codingStandardsIgnoreStart
    ...

    ...
    // @codingStandardsIgnoreEnd
    // @codingStandardsIgnoreFile

    View Slide

  37. Marnhe du Plooy

    View Slide

  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

    View Slide

  39. Only run PHPCS once
    per build.

    View Slide

  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

    View Slide

  41. Kaboompics

    View Slide

  42. Review
    Your
    Ruleset
    Jack Moreh

    View Slide

  43. PHPCS 3.0
    Coming soon...

    View Slide

  44. JR P

    View Slide

  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

    View Slide

  46. unknown
    Questions ?

    View Slide