phparchitect-2019-08

(Rick Simeone) #1
http://www.phparch.com \ August 2019 \ 23

Can You Migrate Any Legacy Code Under One Month?


  • Latte^9 to Tw i gas the templating
    engine.

  • Presenter to Controller—C part
    of Model-View-Controller (MVC)
    pattern.
    You can move routes from single
    RouteFactory class to @Route annotation
    per Controller __invoke() action as in
    Output 3.
    You can change a few new s to
    services as you can see in Output 4.
    None of these changes were done
    manually. We’re too lazy for that.

    How Long Did It Take?
    On January 27th, 2019, we met above
    Nette application and, on February 13th,
    2019, the Symfony application went to
    the staging server. On February 14th,
    we celebrated a new production appli-
    cation in addition to Valentine’s Day.
    We completed it in less than 17 days.
    Done, finished, deployed, goodbye.
    You’re counting the estimate, 17 * 40



    • 2—stop, I’ll count it for you: 2 * 40
      hours = 80 hours.
      It took roughly 50 hours to prepare
      the pattern set and 10 hours to debug
      one event race-condition. Now the
      migration of a similar project would
      take us 15-20 hours.


    How Can You Do It?
    There is zero chance you have a Nette
    project you want to migrate to Symfony, but just in case, the
    migration set in Rector is ready. Just use it](https://phpa.me/
    rector-nette-symfony):

    composer require rector/rector --dev # install

    vendor/bin/rector process src --set nette-to-symfony -n # dry run
    vendor/bin/rector process src --set nette-to-symfony

    It’s not perfect since you’ll have to cover edge cases that are
    specific to your code, but it can save you 80% of the tedious
    work that is typical for this migration.

    To be Continued
    In the next article, I’ll teach you about pattern refactoring
    with Rector and how you can use it to migrate any PHP code
    you have now to your dream code.

    9 Latte: https://latte.nette.org/en/

    Tomas Votruba is a regular speaker at
    meetups and conferences and writes regu-
    larly at http://www.tomasvotruba.cz. He
    created the Rector project and founded the
    PHP community in the Czech Republic in


    1. He loves meeting people from the PHP
      family so he created https://friendsofphp.org
      which is updated daily with meetups all over
      the world. @votrubaT


    Output 3


    1. namespace App Presenter;



    2. +use Symfony\Component\Routing\Annotation\Route;



    3. final class ProcessGPWebPayResponsePresenter

    4. {



      • /**







        • @Route(path = "/payments/gpwebpay/process-response", methods="GET"})







      • */



    5. public function __invoke()

    6. {

    7. // ...

    8. }

    9. }


    Output 4


    1. <?php



    2. class SomePresenter

    3. {



      • /**







        • @var ResponseFactory







      • */





      • private $responseFactory;











      • public function __construct(ResponseFactory $responseFactory)





      • {





      • $this->responseFactory = $responseFactory;





      • }









    4. public function someAction()

    5. {



      • return new OKResponse($response);





      • return $this->responseFactory->createJsonResponse($response);



    6. }

    7. }


    Related Reading