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 code (3.x) 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 A Standard B

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Standard A Standard B

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

Explain

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Customizing Sniffs

Slide 32

Slide 32 text

Customizing Sniffs

Slide 33

Slide 33 text

fui

Slide 34

Slide 34 text

Strategy for Existing Codebases XWP: wp-dev-lib

Slide 35

Slide 35 text

--filter=gitmodified

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

Marnhe du Plooy

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

Only run PHPCS once per build.

Slide 42

Slide 42 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/bin/phpcs; fi

Slide 43

Slide 43 text

Kaboompics

Slide 44

Slide 44 text

Review Your Ruleset Jack Moreh

Slide 45

Slide 45 text

PHPCS 3.0

Slide 46

Slide 46 text

JR P

Slide 47

Slide 47 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/21480

Slide 48

Slide 48 text

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