$30 off During Our Annual Pro Sale. View Details »

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. Managing Code Quality and Code Consistency Juliette Reinders Folmer

  2. “ Only half of programming is coding. The other 90%

    is debugging.
  3. hotblack

  4. Functional Technical Architectural Conceptual

  5. Importance Conceptual Architectural Functional Technical

  6. Attention Conceptual Architectural Functional Technical

  7. 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 ...
  8. Pull request Merged ... please add some tests ... ...

    is situation x handled ...
  9. Marnhe du Plooy

  10. Psalm CS- Fixer PHP Stan PHP CS Exakat PHP CPD

    PHP Insights PHP MD Phan Rector PHP DCD Static Analysis Tools PHP MND
  11. PHP CS Static Analysis Tools

  12. Jon Ng Getting Started with PHP_CodeSniffer

  13. Phar Composer Git clone PEAR Installation

  14. 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)
  15. Fixing errors phpcs /path/to/code –-standard=StandardName phpcbf /path/to/code –-standard=StandardName

  16. Which Standard Will Be Used ? Command line .phpcs.xml phpcs.xml

    .phpcs.xml.dist phpcs.xml.dist --standard=Standard --standard=myrules.xml
  17. Standard: ruleset.xml .phpcs.xml.dist • Can add new sniffs • Can

    be used by name when registered with PHPCS
  18. Brief (Re-)Introduction to PHP_CodeSniffer Nightsabre

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

  20. Standard Standard A Standard B Standard A Standard B Standard

    A Standard B
  21. Standard.Subset.Sniff.ErrorCode Standard.Subset.Sniff.ErrorCode Standard.Subset.Sniff.ErrorCode Standard.Subset.Sniff.ErrorCode

  22. Standard.Subset.Sniff.ErrorCode Include: <rule ref="…" /> Exclude: <exclude name="…" /> ~~~~~~~~

  23. <?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
  24. 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>
  25. <?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+)
  26. elvis santana Choose Your Poison

  27. None
  28. JS CSS PHP (+ inc)

  29. Code Style Documentation Code Smells Code Metrics Best Practices Code

  30. PHPCS Build-in Standards PEAR PSR1 PSR2 Zend MySource Squiz PSR12

  31. Framework/CMS Standards Laravel Joomla Symfony2 CodeIgniter4 Drupal Magento WordPress MediaWiki

    Zend Framework (WebImpress) TYPO3 Yii CakePHP
  32. Functional Standards and Sniff Collections Object Calisthenics Variable Analysis Universal

    Slevomat Cognitive Complexity PHP Compatibility Import Detection Security Audit Normalized Arrays PHPCSExtra
  33. Recognizing External Standards jeltovski

  34. 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
  35. Registering External Standards [2] composer require –-dev dealerdirect/phpcodesniffer-composer-installer

  36. Tips and Tricks kenbrasier

  37. Explain

  38. phpcs --standard=PSR12 --generator=Text (Markdown|HTML)

  39. Seemann

  40. Report Types diff code svnblame gitblame emacs xml checkstyle source

    full summary info csv json junit
  41. Interactive Mode

  42. --filter=GitModified --filter=GitStaged

  43. Boni Idem Ignoring Issues

  44. 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
  45. --ignore-annotations

  46. 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
  47. Questions ? Clodiney Cruz

  48. Thanks! Slides: https://speakerdeck.com/jrf Follow me: https://twitter.com/jrf_nl https://github.com/jrfnl