How to Upgrade Symfony 2.8 to 3.4

Found a typo? Edit me
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!

Would you like to learn more about Rector? Step by step, from zero to hero?

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\Core\Configuration\Option;
use Rector\Symfony\Set\SymfonySetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return function (ContainerConfigurator $containerConfigurator): void {
    $containerConfigurator->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
    // $containerConfigurator->import(SetList::SYMFONY_30);
    // $containerConfigurator->import(SetList::SYMFONY_31);
    // $containerConfigurator->import(SetList::SYMFONY_32);
    // $containerConfigurator->import(SetList::SYMFONY_33);
    // $containerConfigurator->import(SetList::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\Core\Configuration\Option;
use Rector\Set\ValueObject\SetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return function (ContainerConfigurator $containerConfigurator): void {
    $containerConfigurator->import(SetList::PHP_53);
    // again 1 set at a time
    // $containerConfigurator->import(SetList::PHP_54);
    // $containerConfigurator->import(SetList::PHP_55);
    // $containerConfigurator->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!


Have you find this post useful? Do you want more?

Follow me on Twitter, RSS or support me on GitHub Sponsors.