How to Upgrade Symfony 2.8 to 3.4

This post was updated at November 2020 with fresh know-how.
What is new?

Switch from deprecated --set option to rector.php config.


Are you Symfony programmer? Do you work on a successful project? Then upgrading the Symfony project is a work you can't avoid. Almost a year ago I wrote about Five and Half Steps to Migrate from Symfony 2.8 LTS to Symfony 3.4 LTS in Real PRs.

Now it's much easier to jump from one LTS to another - with instant upgrades.

Recently, more and more issues pop-up at Symfony repository asking for automated upgrade:

I'm happy Symfony core team is supporting generic solution to all PHP-code upgrades ↓

Upgrade Symfony... and PHP

To make a bad situation more complicated, this upgrade is also related to upgrading of PHP - Symfony 3.4 requires PHP 5.5.

I wrote about PHP upgrades before, but the main away is to upgrade one minor version at once:

If you split each of these lines into standalone pull-requests, you're the best!

Forget UPGRADE.md

You probably know I work almost part-time on the Rector project. I gather feedback from conferences and meetups all over Europe and try to make Rector better every day. Recently he also migrated between 2 PHP frameworks, because why not?

The PHP community gives me positive vibes about going the right direction. It helps me to make PHP and Symfony sets more and more complete.

How to Upgrade then?

All you need to do to upgrade your PHP code is to install Rector and run particular upgrades.

Do you want to upgrade from Symfony 2.8 to 3.4?

composer require rector/rector --dev

Create rector.php config:

vendor/bin/rector init

Add Symfony sets in it:

use Rector\Symfony\Set\SymfonySetList;
use Rector\Config\RectorConfig;

return function (RectorConfig $rectorConfig): void {
    $rectorConfig->import(SymfonySetList::SYMFONY_28);
    // take it 1 set at a time to so next set works with output of the previous set; I do 1 set per pull-request
    // $rectorConfig->import(SymfonySetList::SYMFONY_30);
    // $rectorConfig->import(SymfonySetList::SYMFONY_31);
    // $rectorConfig->import(SymfonySetList::SYMFONY_32);
    // $rectorConfig->import(SymfonySetList::SYMFONY_33);
    // $rectorConfig->import(SymfonySetList::SYMFONY_34);

    // set paths to directories with your code
    $parameters = $containerConfigurator->parameters();
    $parameters->set(Option::PATHS, [
        __DIR__ . '/app',
        __DIR__ . '/src',
        __DIR__ . '/tests',
    ]);
};

Are you stuck on old PHP 5.3? Rector got you covered:

use Rector\Set\ValueObject\SetList;
use Rector\Config\RectorConfig;

return function (RectorConfig $rectorConfig): void {
    $rectorConfig->import(SetList::PHP_53);
    // again 1 set at a time
    // $rectorConfig->import(SetList::PHP_54);
    // $rectorConfig->import(SetList::PHP_55);
    // $rectorConfig->import(SetList::PHP_56);
    // ...
};

Awesome Symfony 3.3+ Dependency Injection

Upgrade to Symfony 3.3 shrunk my configs to 1/5 of its original size. That's the #1 reason you want to upgrade. If you don't know what I'm talking about, check the diff post about those features. But wait, don't do it manually! This tool converts it all for you.


And that's how we upgrade in 2019 :)


Happy coding!




Do you learn from my contents or use open-souce packages like Rector every day?
Consider supporting it on GitHub Sponsors. I'd really appreciate it!