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
Tweet

More Decks by Juliette Reinders Folmer

Other Decks in Programming

Transcript

  1. Managing Code Quality
    and Code Consistency
    Juliette Reinders Folmer

    View full-size slide

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

    View full-size slide

  3. Functional Technical
    Architectural
    Conceptual

    View full-size slide

  4. Importance
    Conceptual
    Architectural
    Functional
    Technical

    View full-size slide

  5. Attention
    Conceptual
    Architectural
    Functional
    Technical

    View full-size slide

  6. 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 ...

    View full-size slide

  7. Pull request
    Merged
    ... please add some
    tests ...
    ... is situation x handled ...

    View full-size slide

  8. Marnhe du Plooy

    View full-size slide

  9. Psalm
    CS-
    Fixer
    PHP
    Stan
    PHP
    CS
    Exakat
    PHP
    CPD
    PHP
    Insights
    PHP
    MD
    Phan
    Rector
    PHP
    DCD
    Static Analysis Tools
    PHP
    MND

    View full-size slide

  10. PHP
    CS
    Static Analysis Tools

    View full-size slide

  11. Jon Ng
    Getting Started
    with
    PHP_CodeSniffer

    View full-size slide

  12. Phar Composer Git clone PEAR
    Installation

    View full-size slide

  13. 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)

    View full-size slide

  14. Fixing errors
    phpcs /path/to/code –-standard=StandardName
    phpcbf /path/to/code –-standard=StandardName

    View full-size slide

  15. Which Standard Will Be Used ?
    Command line
    .phpcs.xml
    phpcs.xml
    .phpcs.xml.dist
    phpcs.xml.dist
    --standard=Standard
    --standard=myrules.xml

    View full-size slide

  16. Standard: ruleset.xml .phpcs.xml.dist
    • Can add new sniffs
    • Can be used by name when
    registered with PHPCS

    View full-size slide

  17. Brief
    (Re-)Introduction
    to PHP_CodeSniffer
    Nightsabre

    View full-size slide

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

    View full-size slide

  19. Standard
    Standard
    A
    Standard
    B
    Standard
    A
    Standard
    B
    Standard
    A
    Standard
    B

    View full-size slide

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

    View full-size slide

  21. Standard.Subset.Sniff.ErrorCode
    Include:

    Exclude:

    ~~~~~~~~

    View full-size slide



  22. .
    */vendor/*








    */tests/*


    Standard
    A
    Standard
    B

    View full-size slide

  23. Customizing
    Sniffs













    View full-size slide



  24. .


    value="php,inc,js,css,lib/php"/>



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

    View full-size slide

  25. elvis santana
    Choose Your Poison

    View full-size slide

  26. JS
    CSS
    PHP
    (+ inc)

    View full-size slide

  27. Code Style
    Documentation
    Code Smells
    Code Metrics
    Best
    Practices
    Code
    Compatibility

    View full-size slide

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

    View full-size slide

  29. Framework/CMS Standards
    Laravel
    Joomla
    Symfony2
    CodeIgniter4 Drupal
    Magento
    WordPress
    MediaWiki
    Zend
    Framework
    (WebImpress)
    TYPO3 Yii
    CakePHP

    View full-size slide

  30. Functional Standards and Sniff Collections
    Object
    Calisthenics
    Variable
    Analysis
    Universal Slevomat
    Cognitive
    Complexity
    PHP
    Compatibility
    Import
    Detection
    Security
    Audit
    Normalized
    Arrays
    PHPCSExtra

    View full-size slide

  31. Recognizing External
    Standards
    jeltovski

    View full-size slide

  32. 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

    View full-size slide

  33. Registering External Standards [2]
    composer require –-dev
    dealerdirect/phpcodesniffer-composer-installer

    View full-size slide

  34. Tips and Tricks
    kenbrasier

    View full-size slide

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

    View full-size slide

  36. Report Types
    diff
    code
    svnblame
    gitblame
    emacs
    xml
    checkstyle
    source
    full
    summary
    info
    csv
    json
    junit

    View full-size slide

  37. Interactive Mode

    View full-size slide

  38. --filter=GitModified
    --filter=GitStaged

    View full-size slide

  39. Boni Idem
    Ignoring Issues

    View full-size slide

  40. Handling Rule Exceptions
    // phpcs:ignore Stnd.Cat.Sniff.ErrorCode

    // phpcs:disable StndA,StndB.Cat.Sniff -- for reasons
    ...

    ...
    // phpcs:enable
    // phpcs:ignoreFile

    View full-size slide

  41. --ignore-annotations

    View full-size slide

  42. 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

    View full-size slide

  43. Questions ?
    Clodiney Cruz

    View full-size slide

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

    View full-size slide