Slide 1

Slide 1 text

Don't work for PHPCS Make PHPCS work for you

Slide 2

Slide 2 text

Hello! Juliette Reinders Folmer @jrf_nl @jrfnl

Slide 3

Slide 3 text

PHP CodeSniffer by

Slide 4

Slide 4 text

Justin Luebke

Slide 5

Slide 5 text

GlennForrest

Slide 6

Slide 6 text

cohdra

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Code Style Documentation Code Smells Code Metrics Best Practices Code Compatibility

Slide 9

Slide 9 text

PHP (+ inc) JS CSS

Slide 10

Slide 10 text

Phar Composer Git clone PEAR Installation

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Userland PHPCS Standards Laravel Symfony1 Symfony2 CodeIgniter Drupal Magento WordPress

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Jack Moreh

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Standard Standard A Standard B Standard A Standard B Standard A Standard B

Slide 21

Slide 21 text

Standard

Slide 22

Slide 22 text

Standard A Standard B

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Standard.Subset.Sniff.ErrorCode Include: Exclude: ~~~~~~~~

Slide 25

Slide 25 text

Standard A Standard B

Slide 26

Slide 26 text

*/vendor/* */MyFile.php Standard A Standard B

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Explain

Slide 29

Slide 29 text

Customizing Messages Please review this TODO comment: %s 8 error

Slide 30

Slide 30 text

Customizing Sniffs

Slide 31

Slide 31 text

Customizing Sniffs

Slide 32

Slide 32 text

fui

Slide 33

Slide 33 text

Strategy for Existing Codebases XWP: wp-dev-lib

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

Handling Rule Exceptions // @codingStandardsIgnoreLine // @codingStandardsIgnoreStart ... ... // @codingStandardsIgnoreEnd // @codingStandardsIgnoreFile

Slide 37

Slide 37 text

Marnhe du Plooy

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

Only run PHPCS once per build.

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

Kaboompics

Slide 42

Slide 42 text

Review Your Ruleset Jack Moreh

Slide 43

Slide 43 text

PHPCS 3.0 Coming soon...

Slide 44

Slide 44 text

JR P

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

unknown Questions ?