$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 May 21 2018 at the PHPKonf conference, Istanbul, Turkey.
http://phpkonf.org/
---------------------------------------------------------------
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

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

    View Slide

  12. How It Works
    Standard
    (ruleset)
    Sniffs Checks
    (errorcodes)

    View Slide

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

    View Slide

  14. Userland PHPCS Standards
    Laravel Symfony1 Symfony2 CodeIgniter
    Drupal Magento WordPress
    Object
    Calisthenics
    PHP
    Compatibility
    Variable
    Analysis
    Security
    Audit

    View Slide

  15. View Slide

  16. Report Types
    diff
    code (3.0+)
    svnblame
    gitblame
    emacs
    xml
    checkstyle
    source
    full
    summary
    info
    csv
    json
    junit

    View Slide

  17. View Slide

  18. Jack Moreh

    View Slide

  19. View Slide

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

    View Slide

  21. Naming 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

    View Slide

  22. Explain

    View Slide

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

    View Slide

  24. NDPetitt

    View Slide

  25. Standard
    Standard
    A
    Standard
    B
    Standard
    A
    Standard
    B
    Standard
    A
    Standard
    B

    View Slide





  26. Standard

    View Slide






  27. Standard
    A
    Standard
    B

    View Slide

  28. Standard
    A
    Standard
    B





    View Slide

  29. Standard.Subset.Sniff.ErrorCode
    Include:

    Exclude:

    ~~~~~~~~

    View Slide












  30. Standard
    A
    Standard
    B

    View Slide



  31. */vendor/*









    */MyFile\.php




    Standard
    A
    Standard
    B

    View Slide



  32. .


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


    value="./vendor/org/lib"/>

    phpcs
    .
    -p –s
    --report=summary
    --extensions=
    php,inc,js,css,lib/php
    -d memory_limit=128M
    --parallel=8 (PHPCS 3+)

    View Slide

  33. Customizing
    Messages


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


    View Slide

  34. Customizing
    Sniffs





    value="100"/>



    View Slide

  35. Customizing
    Sniffs




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



    View Slide

  36. Customizing
    Sniffs




    type="array">


    value="null"/>




    View Slide

  37. fui

    View Slide

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

    View Slide

  39. --filter=gitmodified

    View Slide

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

    View Slide

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

    View Slide

  42. Handling Rule Exceptions
    // @codingStandardsIgnoreLine

    // @codingStandardsIgnoreStart
    ...

    ...
    // @codingStandardsIgnoreEnd
    // @codingStandardsIgnoreFile
    - pre-PHPCS 3.2 (Deprecated)

    View Slide

  43. Handling Rule Exceptions
    // phpcs:ignore Stnd.Cat.Sniff.ErrorCode

    // phpcs:disable StndA,StndB.Cat.Sniff -- for reasons
    ...

    ...
    // phpcs:enable
    // phpcs:ignoreFile

    View Slide

  44. --ignore-annotations

    View Slide

  45. Marnhe du Plooy

    View Slide

  46. 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
    --runtime-set ignore_warnings_on_exit 1

    View Slide

  47. Only run PHPCS once
    per build.

    View Slide

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

    View Slide

  49. Kaboompics

    View Slide

  50. Review
    Your
    Ruleset
    Jack Moreh

    View Slide

  51. JR P

    View Slide

  52. 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/24292

    View Slide

  53. unknown
    Questions ?

    View Slide