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

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)

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Jack Moreh

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Standard

Slide 24

Slide 24 text

Standard A Standard B

Slide 25

Slide 25 text

Standard A Standard B

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Standard A Standard B

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Explain

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Customizing Sniffs

Slide 35

Slide 35 text

Customizing Sniffs

Slide 36

Slide 36 text

fui

Slide 37

Slide 37 text

Strategy for Existing Codebases XWP: wp-dev-lib

Slide 38

Slide 38 text

--filter=gitmodified

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

Handling Rule Exceptions // @codingStandardsIgnoreLine // @codingStandardsIgnoreStart ... ... // @codingStandardsIgnoreEnd // @codingStandardsIgnoreFile - pre-PHPCS 3.2 (Deprecated)

Slide 42

Slide 42 text

Handling Rule Exceptions // phpcs:ignore Stnd.Cat.Sniff // phpcs:disable StndA,StndB.Cat.Sniff -- for reasons ... ... // phpcs:enable // phpcs:ignoreFile

Slide 43

Slide 43 text

Marnhe du Plooy

Slide 44

Slide 44 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.dist --runtime-set ignore_warnings_on_exit 1

Slide 45

Slide 45 text

Only run PHPCS once per build.

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

Kaboompics

Slide 48

Slide 48 text

Review Your Ruleset Jack Moreh

Slide 49

Slide 49 text

JR P

Slide 50

Slide 50 text

unknown Questions ?

Slide 51

Slide 51 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/23051