Slide 1

Slide 1 text

Getting Started with PHP Codesniffer Thaliesin

Slide 2

Slide 2 text

Agenda 1. Intro round 2. Creating a style guide 3. Creating a ruleset === BREAK === 4. Fine tuning the ruleset 5. Fixing the code base 6. Keeping the code base clean 7. Closing

Slide 3

Slide 3 text

Hello! Juliette Reinders Folmer @jrf_nl @jrfnl

Slide 4

Slide 4 text

Previous experience with PHP Codesniffer ? What project did you bring with you ? What do you hope to learn today ? Who are you ? Marnhe du Plooy

Slide 5

Slide 5 text

Install Check phpcs –i // Composer setup (from project root): vendor/bin/phpcs -i

Slide 6

Slide 6 text

Creating a Code Style Handbook StockSnap

Slide 7

Slide 7 text

Why Code Style Consistency Is Important Easier to maintain & debug Easier to start contributing Easier to comprehend

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

Firkin

Slide 11

Slide 11 text

Required or recommended ? What rules should these comply with ? What code structures do you want to address ?

Slide 12

Slide 12 text

Existing Code Consistency phpcs –p . --standard=Generic,PSR1,PSR2,PEAR,MySource,Squiz,Zend --report-info --extensions=php --ignore=*/vendor/* =./2017029-phpcs-info.txt

Slide 13

Slide 13 text

Creating a PHP Codesniffer Ruleset Adrian

Slide 14

Slide 14 text

How It Works PHP CodeSniffer Coding Standard phpcs phpcbf ruleset sniffs checks + fixers

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Which Standard Reports on What ? phpcs –p . --standard=PSR1 --extensions=php --report-source --report-summary Repeat for PSR2, PEAR, Squiz, Zend

Slide 17

Slide 17 text

Which Rules Are Contained in Each Standard ? phpcs –e --standard=Generic Repeat for PSR1, PSR2, PEAR, Squiz, Zend, MySource

Slide 18

Slide 18 text

Starting a Custom Ruleset Some description . .phpcs.xml.dist

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Standard A Standard B

Slide 22

Slide 22 text

Adding More Standards phpcs --config-set installed_paths /path/to/dir/above/standard phpcs --config-show phpcs --config-set installed_paths /path/to/dir/above/standard1,/path/to/dir/above/standard2

Slide 23

Slide 23 text

Finding Suitable Sniff(s)

Slide 24

Slide 24 text

Comparing Behaviour of Sniffs phpcs –p –s ./test-function-declaration.php --extensions=php --standard=PEAR --sniffs=PEAR.Functions.FunctionDeclaration vs phpcs –p –s ./test-function-declaration.php --extensions=php --standard=Squiz --sniffs=Squiz.Functions.FunctionDeclaration

Slide 25

Slide 25 text

https://github.com/squizlabs/PHP_CodeSniffer/wiki/ Customisable-Sniff-Properties

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

MarcoMaru

Slide 28

Slide 28 text

Fine Tuning Your Ruleset falconp4

Slide 29

Slide 29 text

Run PHP Codesniffer phpcs --report-full --report-source --report-summary =./2017029-phpcs-full.txt =./2017029-phpcs-source.txt =./2017029-phpcs-summary.txt

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Customizing Sniffs

Slide 33

Slide 33 text

Customizing Sniffs

Slide 34

Slide 34 text

Marnhe du Plooy

Slide 35

Slide 35 text

Auto-Fixing Ulrike Mai

Slide 36

Slide 36 text

Running the Fixer phpcbf

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

Review, Fine Tune & Review Some More Manual Fixes

Slide 40

Slide 40 text

Ryan McGuire

Slide 41

Slide 41 text

mzsu Keeping the Code base Clean

Slide 42

Slide 42 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 --runtime-set ignore_warnings_on_exit 1

Slide 43

Slide 43 text

Only run PHPCS once per build.

Slide 44

Slide 44 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 45

Slide 45 text

Lessons Learned

Slide 46

Slide 46 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/22139