Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Tutorial: Getting started with PHP Codesniffer

Tutorial: Getting started with PHP Codesniffer

Tutorial given on September 29, 2017 at the PHP North West Conference, Manchester, United Kingdom.
http://conference.phpnw.org.uk/phpnw17/tutorial-day/
---------------------------------------------------------------
So you'd heard of the PHP Codesniffer and thought you'd give it a try. You set it up & ran it over one of your projects and it tells you there are over 6000 things wrong with your code... Darn... that sounds like work. Let's leave it for another day....

Or not.

In this workshop I'll help you to properly get started with PHPCS. Everyone can bring their own project to work on. We'll look at the different rulesets available, select the one(s) which will work best for your project. Next, we'll look at customizing the ruleset to best suit your project & team's needs and we'll start auto-fixing issues. If all goes well, those 6000 things "wrong" will be down to zilch by the end of the workshop & you've got a pull request ready to prove it!

Juliette Reinders Folmer

September 29, 2017
Tweet

More Decks by Juliette Reinders Folmer

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. Why Code Style Consistency Is Important Easier to maintain &

    debug Easier to start contributing Easier to comprehend
  4. Required or recommended ? What rules should these comply with

    ? What code structures do you want to address ?
  5. Which Standard Reports on What ? phpcs –p . --standard=PSR1

    --extensions=php --report-source --report-summary Repeat for PSR2, PEAR, Squiz, Zend
  6. Which Rules Are Contained in Each Standard ? phpcs –e

    --standard=Generic Repeat for PSR1, PSR2, PEAR, Squiz, Zend, MySource
  7. Starting a Custom Ruleset <?xml version="1.0"?> <ruleset name="Project/Organisation name"> <description>Some

    description</description> <file>.</file> <rule ref="...."/> </ruleset> .phpcs.xml.dist
  8. <?xml version="1.0"?> <ruleset name="MyProject"> <rule ref="PSR1" /> <rule ref="PSR2"> <exclude

    name="Generic.Files.LineEndings" /> </rule> <config name="testVersion" value="5.1-99.0"/> <rule ref="PHPCompatibility"> <exclude name="PHPCompatibility.PHP. DeprecatedIniDirectives.safe_modeRemoved"> </rule> </ruleset> Standard A Standard B
  9. 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
  10. Finding Suitable Sniff(s) <?php // Target style – shouldn't give

    errors. if (true === $something) { } // Wrong style. if( $something==true ) { } phpcs –p –s ./test.php –extensions=php –-standard=Generic,PSR1,PSR2,PEAR,Squiz,MySource,Zend
  11. 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
  12. <?xml version="1.0"?> <ruleset name="MyProject"> <exclude-pattern>*/vendor/*</exclude-pattern> <rule ref="PSR1" /> <rule ref="PSR2">

    <exclude name="Generic.Files.LineEndings" /> </rule> <config name="testVersion" value="5.1-99.0"/> <rule ref="PHPCompatibility"> <exclude name="PHPCompatibility.PHP. DeprecatedIniDirectives.safe_modeRemoved"> </rule> <rule ref="PHPCompatibility.PHP.Deprecated IniDirectives.magic_quotes_runtimeRemoved"> <exclude-pattern>*/MyFile\.php</exclude-pattern> </rule> <rule ref="Generic.Metrics.NestingLevelSniff" /> <rule ref="PEAR.Commenting" /> </ruleset> Standard A Standard B
  13. Customizing Messages <?xml version="1.0"?> <ruleset name="MyProject"> <rule ref="Generic.Commenting.Todo.CommentFound"> <message>Please review

    this TODO comment: %s</message> <severity>8</severity> <type>error</type> </rule> </ruleset>
  14. Customizing Sniffs <?xml version="1.0"?> <ruleset name="MyProject"> <rule ref="Generic.Files.LineLength"> <properties> <property

    name="lineLimit" value="90"/> <property name="absoluteLineLimit" value="100"/> </properties> </rule> </ruleset>
  15. Customizing Sniffs <?xml version="1.0"?> <ruleset name="MyProject"> <rule ref="Generic.PHP.ForbiddenFunctions"> <properties> <property

    name="forbiddenFunctions" type="array" value="delete=>unset,print=>echo, create_function=>null" /> </properties> </rule> </ruleset>
  16. Handling Rule Exceptions // @codingStandardsIgnoreLine <code line to be ignored>

    // @codingStandardsIgnoreStart ... <code> ... // @codingStandardsIgnoreEnd // @codingStandardsIgnoreFile
  17. 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
  18. 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