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

Loulier Guillaume

July 04, 2022
Tweet

More Decks by Loulier Guillaume

Other Decks in Programming

Transcript

  1. Rector, time to
    refactor your
    code easily

    View Slide

  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

    View Slide

  3. Trying to
    escape the
    “legacy hell”

    View Slide

  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?

    View Slide

  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?

    View Slide

  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?

    View Slide

  7. Introducing
    Rector

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  11. Show me
    the code

    View Slide

  12. Configure it
    Image

    View Slide

  13. Launch it

    View Slide

  14. Review and
    apply

    View Slide

  15. Run it

    View Slide

  16. Test it

    View Slide

  17. Extending it

    View Slide

  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

    View Slide

  19. Fair well __get

    View Slide

  20. Here stand
    the future

    View Slide

  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

    View Slide

  22. The end

    View Slide