phparchitect-2019-08

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

Can You Migrate Any Legacy Code Under One Month?

If you’re like me, you better understand the business logic
“size,” like routes—this application had 151 unique routes.


What About Tests?
Of course, better-tested applications are easier to migrate,
because you get faster feedback if you break some code.
Honza Mikeš is also a lazy developer and doesn’t like to
repeat his mistakes. That’s why there are 136 PHPUnit tests,
that cover mostly the endpoints.

Why Did We Do It?
The application was written in Nette, which worked and
met the technical requirements in the past.
The primary motivation for the transition was the dying
Nette ecosystem and over-integration of Symfony. Nette was
used only for controllers and dependency injection, while
Symfony was integrated with console, events, translations,
monolog, migrations, and Doctrine.
What does “dying ecosystem” mean? Nette released just
one minor version since July 2016, while Symfony had six
releases during the same period. Also, the most promoted
new feature of Nette 3.0 was the addition of type declaration
in PHP 7.1, which is not really a framework feature and only
adds maintenance work.
The Entrydo project needed to develop faster, in a more
reliable way, and with less code. They wanted to be in an
ecosystem that is alive and kicking.

How We Did It
I offered Mikeš^5 a deal he couldn’t refuse:

“We will give it a week, and if we feel stuck, we’ll give up
soon enough.”

This sentence contains three basic rules for successful
migration of a massive project that you have to set beforehand.


  1. Fast And Deadlined
    If you have a year to write a prototype for your new startup,
    it most likely will take a year. If you have a week, it will take
    a week.
    That’s why we picked one week as a soft deadline and one
    month as a hard deadline. If we’re not finished after one
    month of work, we’re closing it.

  2. Human Independent
    Have you ever migrated or upgraded a huge project? I bet
    you’ve picked full-rewrite or step-by-step refactoring, right? I
    admit, I did too, but it’s a complete nonsense way to refactor
    large applications.
    Both of these approaches depend on humans, both their
    skills and their speed to refactor. Even if you have a team of
    five super-smart developers, a manual rewrite of 10,000,000


5 Mikeš: http://github.com/JanMikes

lines of code might take them a considerable amount of time.
They would tell you it’ll be done in three months, but you
know how it goes with developers and estimates.
Instead, you have to think about the way based on
machine-time. It’s not that popular, even though The Prag-
matic Programmer book mentions it.
I call it pattern refactoring^6 because it focuses on patterns
in code, not on size or version of the programming language.
A pattern can be one of many, like the familiar service
lo cator.

$someService = Container::get('someService');

There’s constructor injection.

public function __construct(SomeService $someService)
{
$this->someService = $someService;
}

And, static instantiation (singletons).

$someService = new SomeService();

But also, active record:

$product = $products->get( 1 );
$product->changeName('Swim suit');

$product->save()

And repository and entity:

$product = $productRepository->get( 1 );
$product->changeName('Swim suit');

$productRepository->save($product);

An example of pattern refactoring is, that’s right: services
locator to constructor injection. Does this sound hard?
There are actually four to five ways to use service locators
and two to use constructor injection. Once you’ve had them
covered in automated migration, you just run the scripts, and
it works on all the PHP code in the world, forever. :)
That sounds unbelievable, right? Keep on reading.


  1. In Cooperation With Business
    I think this is the number one reason why these migrations
    don’t happen more often.
    Programmers speak about migration as something “must-
    have” and “needed” for them:



  • It’s dangerous to use PHP 5.3; there might be security
    issues.

  • PHP 7.0 is twice faster as PHP 5.6; we should upgrade.

  • We should upgrade to the Symfony 4; it has some cool
    new features.


6 pattern refactoring: https://phpa.me/votruba-pattern-refactoring
Free download pdf