Save 37% off PRO during our Black Friday Sale! »

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.

2fe1b2866cfd498a513295d60d5cad0f?s=128

Joel Clermont

October 16, 2021
Tweet

Transcript

  1. Level Up Your Code Quality Joel Clermont @jclermont

  2. Introduction @jclermont

  3. What is code quality? @jclermont

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

  5. Disclaimer 
 Don’t do this just because I say so

    @jclermont
  6. Let’s dive in @jclermont

  7. This page intentionally left blank @jclermont

  8. @jclermont

  9. @jclermont

  10. Tool 1: PHP linter @jclermont

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

  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
  13. @jclermont

  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
  15. Tool 2: PHP CodeSniffer @jclermont

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

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

    “ fi x it for me” • Local versus remote @jclermont
  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
  19. @jclermont

  20. @jclermont

  21. Your mileage may vary @jclermont

  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
  23. Tool 3: phpcpd @jclermont

  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
  25. Tool 4: phpmd @jclermont

  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
  27. Tool 5: PHPUnit @jclermont

  28. Test it like you run it prod @jclermont

  29. @jclermont

  30. @jclermont

  31. @jclermont

  32. @jclermont

  33. @jclermont Infection - mutation testing

  34. @jclermont

  35. @jclermont

  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.
  37. Tool 6: PHP Insights @jclermont

  38. @jclermont

  39. @jclermont

  40. @jclermont

  41. @jclermont

  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
  43. About types in PHP… @jclermont

  44. @jclermont

  45. @jclermont

  46. Tool 7: PHPStan @jclermont

  47. But there’s more… @jclermont

  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
  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
  50. Tool 8: Rector @jclermont

  51. How it works @jclermont

  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
  53. Now my code is perfect, right? @jclermont

  54. Final thoughts @jclermont

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

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