Upgrading Symfony
Since Symfony 6.0, over 650 things have changed in fifty components and bridges: MASTER_REQUEST
became MAIN_REQUEST, attributes of the framework replaced the SensioFrameworkExtraBundle annotations, and the name of
a command moved into #[AsCommand]. The package dresscode/upgrade-symfony describes all of them.
DressCode fixes roughly 440 right away and reports the rest, together with what to write instead.
The upgrades from Symfony 6 to Symfony 7 and from Symfony 7.4 to Symfony 8 have pages of their own, with an example and the steps to follow.
Installation
To upgrade an application that still runs on PHP older than 8.4, install DressCode globally, because DressCode itself needs PHP 8.4 (see Where DressCode Runs):
composer global require dresscode/dresscode dresscode/upgrade-symfony phpstan/phpstan
dresscode upgrade today
You need no configuration for it: upgrade today finds the data and the rewrites of the package on its own and
leaves the style of the code as it is. The data of each component applies only when the project uses that component, and only for
the versions the project has reached. Upgrading Code describes how to go one major version at a time and what to do with
what is left. A project that already runs on PHP 8.4 to 8.6 can also add the package as a development dependency,
composer require --dev dresscode/upgrade-symfony phpstan/phpstan, and then run
vendor/bin/dresscode upgrade today.
A project that requires the whole symfony/symfony has all the components that package replaces, in
the version of symfony/symfony.
What Gets Fixed
Here are a command and a controller written for Symfony 5.4, in a project that now uses the current versions:
class ReportCommand extends Command
{
protected static $defaultName = 'app:report'; // Property `Symfony\Component\Console\Command\Command::$defaultName` is replaced by the attribute `#[Symfony\Component\Console\Attribute\AsCommand]`.
protected static $defaultDescription = 'Sends the daily report'; // Property `Symfony\Component\Console\Command\Command::$defaultDescription` is replaced by the attribute `#[Symfony\Component\Console\Attribute\AsCommand]`.
protected function execute(InputInterface $input, OutputInterface $output): int
{
$request = $this->requestStack->getMasterRequest(); // Method `Symfony\Component\HttpFoundation\RequestStack::getMasterRequest()` is replaced by `RequestStack::getMainRequest()`.
$output->writeln($request->get('day')); // Method `Symfony\Component\HttpFoundation\Request::get()` is forbidden: read the bag the value comes from, the `attributes`, `query` or `request` property, such as `query->get()`.
return Command::SUCCESS;
}
}
class ReportController extends AbstractController
{
/** @Security("is_granted('ROLE_ADMIN')") */ // Annotation `@Security` is replaced by the attribute `#[Symfony\Component\Security\Http\Attribute\IsGranted]`.
public function show(): Response
{
return new Response($this->getUser()->getUsername()); // Method `Symfony\Component\Security\Core\User\UserInterface::getUsername()` is replaced by `getUserIdentifier()`.
}
}
After dresscode upgrade today:
#[AsCommand(name: 'app:report', description: 'Sends the daily report')]
class ReportCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
$request = $this->requestStack->getMainRequest();
$output->writeln($request->get('day'));
return Command::SUCCESS;
}
}
class ReportController extends AbstractController
{
#[IsGranted('ROLE_ADMIN')]
public function show(): Response
{
return new Response($this->getUser()->getUserIdentifier());
}
}
The two static properties merged into one attribute, and the expression of the annotation @Security became the
role of the attribute #[IsGranted], both with their imports. The call $request->get() was left alone:
Request::get() looks for the value in the attributes, in the query and in the body of the request, and you have to
decide which of them your code reads it from.
What the package fixes:
- renamed classes and interfaces:
Symfony\Component\Security\Core\SecuritytoSymfony\Bundle\SecurityBundle\Security,TaggedIteratortoAutowireIterator, the Sendinblue bridge to Brevo and others; the import, the type,new,instanceofand the attribute are all rewritten, - renamed members:
getMasterRequest()togetMainRequest(),HttpKernelInterface::MASTER_REQUESTtoMAIN_REQUEST,getUsername()togetUserIdentifier(),Application::add()toaddCommand(), - calls of a different shape: a parameter that got a different name (
new UriSigner($secret, parameter: ...)tohashParameter:), a key of an options array renamed in place (mirror()withcopy_on_windowstofollow_symlinks), the container configuration functionref()toservice(), - SensioFrameworkExtraBundle annotations to attributes of the framework:
@Route,@Cache,@IsGrantedand@Template, and@Security, whose expression only callsis_granted(), to#[IsGranted]; routing and serializer annotations from the namespaceAnnotationto attributes fromAttribute; validator constraint annotations and@UniqueEntityto the attributes of the same name, except@ExpressionLanguageSyntax, which becomes#[ExpressionSyntax]; and@requiredto#[Required], - a class member to an attribute:
$defaultNameand$defaultDescriptionof a command, and an overriddengetDefaultName()too, to#[AsCommand], and the interfaceMessageHandlerInterfaceto#[AsMessageHandler], - a controller argument resolver written against
ArgumentValueResolverInterfacetoValueResolverInterface: itssupports()becomes a condition at the start ofresolve(), - signatures of methods in child classes of framework classes: the return types and parameters a component added
(
execute(): intof a command), even without data, from the declarations of the installed version. Adding a return type is a risky fix, because the body of the method may return something else.
What Is Left to You
Where there is no replacement, or the replacement is used differently, DressCode reports the place and says what to write. This covers, for example, services a controller used to take from the container, or an interface that should be replaced by attributes on individual methods:
Method `Symfony\Bundle\FrameworkBundle\Controller\AbstractController::getDoctrine()` is forbidden: use a constructor or action argument typed with `Doctrine\Persistence\ManagerRegistry`.
Class `Symfony\Component\Messenger\Handler\MessageSubscriberInterface` is forbidden: write the attribute `AsMessageHandler` on each method that handles a message, with what `getHandledMessages()` yields for it as its arguments.
What a component only quietly marks as deprecated is not in the data. With the types, DressCode reports it anyway, and where
the annotation names the replacement, it rewrites the code. The data does not capture changes in behavior, in YAML configuration
or in Twig templates at all; you will find those in the UPGRADE-*.md guides of the Symfony repository.
Keeping the Old API from Coming Back
Every check can watch for the old API too, for example in continuous integration. For that, the configuration needs three
things: the types, the set deprecations, and the name of the package, which enables its rewrites in the section
symfony (see Upgrading
Code):
typeAnalysis: phpstan
use:
- symfony # your standard
- deprecations
- upgrade-symfony
The decisions symfony.isGrantedAttribute from symfony/security-http 6.2 and
symfony.valueResolver from symfony/http-kernel 6.2 rewrite the annotation @Security, also
in the form of the attribute #[Security] of the same bundle, and the argument resolver. The data itself turns them
on, so you do not have to add them to the configuration, and upgrade today runs them too. If you do not want one of
them, turn it off in the section symfony with the value keep.
What the Package Covers
| component | versions |
|---|---|
symfony/asset, symfony/asset-mapper |
6.0 to 7.1 |
symfony/browser-kit, symfony/dom-crawler |
6.0 to 7.4 |
symfony/cache |
6.0 to 7.1 |
symfony/config |
6.0 to 7.4 |
symfony/console |
6.0 to 7.4 |
symfony/dependency-injection |
6.0 to 8.1 |
symfony/doctrine-bridge |
6.0 to 8.2 |
symfony/event-dispatcher |
6.0 |
symfony/expression-language |
7.1 |
symfony/filesystem, symfony/finder |
6.0 to 8.2 |
symfony/form |
6.0 to 8.1 |
symfony/framework-bundle |
6.0 to 7.4 |
symfony/html-sanitizer |
7.4 |
symfony/http-client |
6.0 to 8.1 |
symfony/http-foundation |
6.0 to 8.1 |
symfony/http-kernel |
6.0 to 8.2 |
symfony/intl |
6.0 |
symfony/json-streamer, symfony/type-info |
7.2 to 8.1 |
symfony/lock |
6.0 to 6.3 |
symfony/mailer, symfony/mime, symfony/notifier |
6.0 to 7.4 |
symfony/messenger |
6.0 to 6.4 |
symfony/monolog-bridge |
6.0 to 7.4 |
symfony/options-resolver |
6.0 |
symfony/property-access, symfony/property-info |
6.0 to 7.3 |
symfony/routing |
6.0 to 7.4 |
symfony/security-bundle, symfony/security-core, symfony/security-csrf |
6.0 to 7.4 |
symfony/security-http |
6.0 to 8.1 |
symfony/serializer |
6.0 to 7.4 |
symfony/string |
7.4 |
symfony/translation |
6.2 to 7.4 |
symfony/twig-bridge |
6.2 to 7.2 |
symfony/validator |
6.0 to 8.2 |
symfony/var-dumper, symfony/var-exporter |
6.2 to 7.3 |
symfony/workflow |
6.0 to 7.3 |
bridges brevo-mailer, brevo-notifier, mail-pace-mailer, sevenio-notifier,
google-chat-notifier, slack-notifier |
6.0 to 7.3 |
The versions show the releases in which a component changed something the data captures. The data of a retired bridge is listed
under the bridge that replaced it: the rename of Sendinblue to Brevo applies in a project that already has
symfony/brevo-mailer. An application on Symfony 5.4 is upgraded the same way, with version 6.0 as the first step;
Upgrading Code describes how to proceed one
major version at a time.