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

Rector, time to refactor your code easily

Rector, time to refactor your code easily

27021de2df79d619a3ebf52ab6392e82?s=128

Loulier Guillaume

July 04, 2022
Tweet

More Decks by Loulier Guillaume

Other Decks in Programming

Transcript

  1. Rector, time to refactor your code easily

  2. Plan it 01 - One tool to refactor everything 02

    - Detect, refactor, test, repeat 03 - Not so live demo 04 - Sky is the limit 05 - Tomorrow is in your hands
  3. Trying to escape the “legacy hell”

  4. - Hard to plan - Cost a lot of time,

    money and sweat - Nobody wants to do it, talk about it and/or even think about it - Can introduce errors and BC breaks Refactoring?
  5. - Improving the code - Preparing for future PHP versions

    and features - Establishing and/or enforcing new standards - Saving time (and money?) for future features Why?
  6. - Most of the time, no or highly opinionated -

    Static analysis? Not perfect and not built for - Unit tests? Used for validation and regression handling - Human intervention? If you want random errors, yes Any built-in solution?
  7. Introducing Rector

  8. - Built in PHP by Tomas Votruba on top of

    nikic/php-parser - Support for PHP >= 5.2 up to 8.2 - Support for Symfony, Laravel, PHPUnit, Nette, Doctrine and more - More than 500+ rules Time to be a corrector
  9. - Fully extensible - Based on Symfony DIC component for

    the configuration - Can be tweaked for almost every project / need - Need a specific rule? Create your own! - Can be used in both modern and legacy projects Wait, there’s more
  10. How? I Prepare phase Loop through each classes / traits

    / interfaces / enums / etc Prepare phase - Build Build a tree of node along with metadata II Rectify phase Apply each rector (aka rule) to each node III Save phase Save diffs to a file “one by one” and go to the next node IV Once done, a report is generated
  11. Show me the code

  12. Configure it Image

  13. Launch it

  14. Review and apply

  15. Run it

  16. Test it

  17. Extending it

  18. - Most cases are built-in but what about yours? -

    Processing PHP files is great but what about YAML? What about JSON files? - Want to rename __get() to getters? - Moving from docblocks to custom attributes? Tailor-made
  19. Fair well __get

  20. Here stand the future

  21. - What about PHPStan? - What if Twig templates could

    be refactored? - CI and PR integrations? - Maybe one day, PHP will provide a native tool? Rector is just the beginning
  22. The end