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

Level Up Your Code Quality

Level Up Your Code Quality

It's dangerous to go alone! Just as Link was warned and equipped before setting off on his first quest, there are tools that can assist a PHP developer to level up and write better quality code. Join me as I demonstrate a few of these tools and show how easy it is to set them up to work well in a PHP project, regardless of what framework you use.

Joel Clermont

October 16, 2021
Tweet

More Decks by Joel Clermont

Other Decks in Technology

Transcript

  1. Level Up Your Code Quality
    Joel Clermont
    @jclermont

    View Slide

  2. Introduction
    @jclermont

    View Slide

  3. What is code quality?
    @jclermont

    View Slide

  4. Easier to make changes
    Lower number of defects
    @jclermont

    View Slide

  5. Disclaimer

    Don’t do this just because I say so
    @jclermont

    View Slide

  6. Let’s dive in
    @jclermont

    View Slide

  7. This page intentionally left blank
    @jclermont

    View Slide

  8. @jclermont

    View Slide

  9. @jclermont

    View Slide

  10. Tool 1: PHP linter
    @jclermont

    View Slide

  11. @jclermont
    https://github.com/overtrue/phplint

    View Slide

  12. Getting started
    https://github.com/overtrue/phplint
    • Composer install as dev dependency

    • Ignore .phplint
    -
    cache in version control

    • Setup .phplint.yml con
    fi
    guration

    • What directories to scan

    • What to ignore, vendor for example

    • Enable warnings
    @jclermont

    View Slide

  13. @jclermont

    View Slide

  14. Summary
    https://github.com/overtrue/phplint
    @jclermont
    Setup e
    ff
    ort: Easy
    Risk: None
    Impact on quality: Relatively low
    Recommendation: Install on every project

    View Slide

  15. Tool 2: PHP CodeSniffer
    @jclermont

    View Slide

  16. Different philosophies
    • “Tell me to
    fi
    x it” versus “
    fi
    x it for me”
    @jclermont

    View Slide

  17. Different philosophies
    • “Tell me to
    fi
    x it” versus “
    fi
    x it for me”

    • Local versus remote
    @jclermont

    View Slide

  18. Getting started
    https://github.com/squizlabs/PHP_CodeSni
    ff
    er
    • Composer install as dev dependency

    • Pick a standard (I like PSR-12)

    • Add a few tweaks as needed (slevomat)
    @jclermont

    View Slide

  19. @jclermont

    View Slide

  20. @jclermont

    View Slide

  21. Your mileage may vary
    @jclermont

    View Slide

  22. Summary
    https://github.com/squizlabs/PHP_CodeSni
    ff
    er
    @jclermont
    Setup e
    ff
    ort: Easy (if you’re not too opinionated)
    Risk: Minimal
    Impact on quality: Higher than you’d expect
    Recommendation: Install on every project

    View Slide

  23. Tool 3: phpcpd
    @jclermont

    View Slide

  24. Summary
    https://github.com/sebastianbergmann/phpcpd
    @jclermont
    Setup e
    ff
    ort: Easy
    Risk: None
    Impact on quality: Minimal
    Recommendation: Don’t blindly follow advice

    View Slide

  25. Tool 4: phpmd
    @jclermont

    View Slide

  26. Summary
    https://phpmd.org
    @jclermont
    Setup e
    ff
    ort: Easy
    Risk: None
    Impact on quality: Low
    Recommendation: Numbers are nice, but aren’t everything

    View Slide

  27. Tool 5: PHPUnit
    @jclermont

    View Slide

  28. Test it like you run it prod
    @jclermont

    View Slide

  29. @jclermont

    View Slide

  30. @jclermont

    View Slide

  31. @jclermont

    View Slide

  32. @jclermont

    View Slide

  33. @jclermont
    Infection - mutation testing

    View Slide

  34. @jclermont

    View Slide

  35. @jclermont

    View Slide

  36. Summary
    https://phpunit.de
    @jclermont
    Setup e
    ff
    ort: Depends
    Risk: Writing tests is addictive
    Impact on quality: To the moon!
    Recommendation: If you only do one thing, this should be it.

    View Slide

  37. Tool 6: PHP Insights
    @jclermont

    View Slide

  38. @jclermont

    View Slide

  39. @jclermont

    View Slide

  40. @jclermont

    View Slide

  41. @jclermont

    View Slide

  42. Summary
    https://phpinsights.com
    @jclermont
    Setup e
    ff
    ort: Easy
    Risk: None
    Impact on quality: Pretty good
    Recommendation: Scores are nice top-level metric

    View Slide

  43. About types in PHP…
    @jclermont

    View Slide

  44. @jclermont

    View Slide

  45. @jclermont

    View Slide

  46. Tool 7: PHPStan
    @jclermont

    View Slide

  47. But there’s more…
    @jclermont

    View Slide

  48. Getting started
    https://phpstan.org
    • So many errors to start

    • Establish baseline

    • Pick a level

    • Rinse and repeat with new versions of PHPStan
    @jclermont

    View Slide

  49. Summary
    https://phpstan.org
    @jclermont
    Setup e
    ff
    ort: Medium-to-high
    Risk: Minimal
    Impact on quality: Signi
    fi
    cant
    Recommendation: Even better with tests

    View Slide

  50. Tool 8: Rector
    @jclermont

    View Slide

  51. How it works
    @jclermont

    View Slide

  52. Summary
    https://getrector.org
    @jclermont
    Setup e
    ff
    ort: Prepare for pain
    Risk: Only your time
    Impact on quality: Signi
    fi
    cant
    Recommendation: Start with Shift Workbench

    View Slide

  53. Now my code is perfect, right?
    @jclermont

    View Slide

  54. Final thoughts
    @jclermont

    View Slide

  55. Shameless plugs @jclermont
    show.nocompromises.io
    No Compromises podcast

    View Slide

  56. Thank you! @jclermont
    show.nocompromises.io
    No Compromises podcast

    View Slide