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

Managing Code Quality and Code Consistency

Managing Code Quality and Code Consistency

Presented on December 13 2021 for for final year Composer Science students at De Montfort University, Leicester, UK.

Juliette Reinders Folmer

December 13, 2021

More Decks by Juliette Reinders Folmer

Other Decks in Programming


  1. Pull request Merged ... typo in documentation ... ... whitespace

    ... ... please add some tests ... ... is situation x handled ... ... use early return ... ... using PHP 8.0 syntax ... ... use strict checking in your tests ...
  2. Psalm CS- Fixer PHP Stan PHP CS Exakat PHP CPD

    PHP Insights PHP MD Phan Rector PHP DCD Static Analysis Tools PHP MND
  3. 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)
  4. Which Standard Will Be Used ? Command line .phpcs.xml phpcs.xml

    .phpcs.xml.dist phpcs.xml.dist --standard=Standard --standard=myrules.xml
  5. <?xml version="1.0"?> <ruleset name="MyProject"> <file>.</file> <exclude-pattern>*/vendor/*</exclude-pattern> <rule ref="Stnd"/> <rule ref="Stnd.Category"/>

    <rule ref="Stnd.Category.SniffName"/> <rule ref="Stnd.Category.SniffName.ErrorCode"/> <rule ref="Stnd"/> <exclude name="Stnd.Category.SniffName"/> </rule> <rule ref="Stnd.Category.SniffName"/> <exclude-pattern>*/tests/*</exclude-pattern> </rule> </ruleset> Standard A Standard B
  6. Customizing Sniffs <?xml version="1.0"?> <ruleset name="MyProject"> <rule ref="Stnd.Category.SniffName"> <properties> <property

    name="propertyA" value="true"/> <property name="propertyB" value="100"/> <property name="propertyC" type="array"> <element key="key1" value="value1"/> <element key="key2" value="value2"/> </property> </properties> </rule> </ruleset>
  7. <?xml version="1.0"?> <ruleset name="MyProject"> <file>.</file> <arg value="ps"/> <arg name="report" value="summary"/>

    <arg name="extensions" value="php,inc,js,css,lib/php"/> <ini name="memory_limit" value="128M" /> <arg name="parallel" value="8"/> </ruleset> phpcs . -p –s --report=summary --extensions= php,inc,js,css,lib/php -d memory_limit=128M --parallel=8 (PHPCS 3+)
  8. Functional Standards and Sniff Collections Object Calisthenics Variable Analysis Universal

    Slevomat Cognitive Complexity PHP Compatibility Import Detection Security Audit Normalized Arrays PHPCSExtra
  9. Registering External Standards [1] 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. Handling Rule Exceptions // phpcs:ignore Stnd.Cat.Sniff.ErrorCode <code line to be

    ignored> // phpcs:disable StndA,StndB.Cat.Sniff -- for reasons ... <code> ... // phpcs:enable // phpcs:ignoreFile
  11. Links ✓ PHP_CodeSniffer https://github.com/squizlabs/PHP_CodeSniffer ✓ Find External Standards on Packagist

    https://packagist.org/?query=phpcs&type=phpcodesniffer-standard https://packagist.org/?query=php_codesniffer&type=phpcodesniffer-standard ✓ DealerDirect Composer PHPCS plugin https://github.com/Dealerdirect/phpcodesniffer-composer-installer ✓ PHPCSDevTools, PHPCSUtils, PHPCSExtra, PHPCompatibility et al https://github.com/PHPCSStandards https://github.com/PHPCompatibility