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

Rector, time to refactor your code easily

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on. →

Rector, time to refactor your code easily

Avatar for Loulier Guillaume

Loulier Guillaume

July 04, 2022

More Decks by Loulier Guillaume

Other Decks in Programming

Transcript

  1. 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
  2. - 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?
  3. - Improving the code - Preparing for future PHP versions

    and features - Establishing and/or enforcing new standards - Saving time (and money?) for future features Why?
  4. - 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?
  5. - 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
  6. - 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
  7. 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
  8. - 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
  9. - 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