Upgrade to Symfony 7
Symfony 7.0 was a cleanup: it removed everything Symfony 6 had deprecated, such as the @Route
annotation, the annotations of SensioFrameworkExtraBundle, MessageHandlerInterface, renderForm() and the
property $defaultName of a command. Symfony 6.4 already has all their replacements, so you do most of the upgrade
from Symfony 6 to 7 while still on 6.4: DressCode with the package dresscode/upgrade-symfony rewrites your code and
reports what it cannot rewrite.
According to UPGRADE-7.0.md, version 7.0 has the same features as 6.4, only without the deprecated ones. But 7.0 needs PHP 8.2, while 6.4 also runs on PHP 8.1 (Symfony 7.0).
What It Looks Like
This controller was written for Symfony 6.4, and composer.json has the Symfony packages at 6.4.*. It
uses three things that Symfony 7.0 removed:
use Symfony\Component\Routing\Annotation\Route;
class OrderController extends AbstractController
{
/** @Route("/orders/{id}", name="order_show") */
public function show(Request $request, int $id): Response
{
$form = $this->createForm(OrderType::class);
return $this->renderForm('order/show.html.twig', [
'form' => $form,
'format' => $request->getContentType(),
]);
}
}
After dresscode upgrade today, still on Symfony 6.4:
use Symfony\Component\Routing\Attribute\Route;
class OrderController extends AbstractController
{
#[Route('/orders/{id}', name: 'order_show')]
public function show(Request $request, int $id): Response
{
$form = $this->createForm(OrderType::class);
return $this->render('order/show.html.twig', [
'form' => $form,
'format' => $request->getContentTypeFormat(),
]);
}
}
The annotation became an attribute of the same name, imported from the namespace Attribute instead of
Annotation. renderForm() became render(), which takes the form as it is, and
getContentType() became getContentTypeFormat(). The code still runs on 6.4. After the versions in
composer.json were raised to 7.0.*, a second run found nothing more to rewrite in this controller.
Step by Step
The Symfony packages are raised all at once, so you raise them in composer.json yourself and
dresscode upgrade today rewrites the code. DressCode needs PHP 8.4. If your application runs on an older PHP,
install DressCode globally, with PHPStan and the data for Symfony:
composer global require dresscode/dresscode dresscode/upgrade-symfony phpstan/phpstan
On PHP 8.4 or newer, it can be a development dependency of the project, as Installation shows.
- Start from Symfony 6.4 with a clean working tree. Run
dresscode upgrade today, and once more if it advises you to. It rewrites what Symfony 6.1 to 6.4 deprecated, because the replacements already exist in 6.4. - Go through what it reports, run your tests and PHPStan, and commit.
- Raise the Symfony packages in
composer.jsonto7.0.*, or to a later 7.x, andrequire.phpto at least 8.2. With Symfony Flex, raiseextra.symfony.requiretoo. Then runcomposer update "symfony/*" -W --prefer-lowest. - Run
dresscode upgrade todayagain. It finds what is left after raising the versions, and on a later 7.x also what that version deprecated. Then run the tests, and commit.
DressCode reads the version of each package from its constraint in composer.json. If a Symfony
package has only * there and leaves the version to extra.symfony.require, DressCode does not see the
lower bound and only reports the changes of that package. Write the version into the constraint, such as 6.4.*.
Upgrading Code explains why
--prefer-lowest and why one major version at a time.
SensioFrameworkExtraBundle Annotations
SensioFrameworkExtraBundle is abandoned, and its last version 6.2.10 allows symfony/framework-bundle only up to
6.x, as Packagist shows. Since Symfony 6.2, the
framework has its own attributes for these annotations, and DressCode writes them in place of the annotations:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class ProductController extends AbstractController
{
/**
* @IsGranted("ROLE_EDITOR")
* @Cache(maxage=3600, public=true)
* @Template("product/list.html.twig")
*/
public function list(): array
{
return ['products' => []];
}
}
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class ProductController extends AbstractController
{
#[\Symfony\Component\Security\Http\Attribute\IsGranted('ROLE_EDITOR')]
#[\Symfony\Component\HttpKernel\Attribute\Cache(maxage: 3600, public: true)]
#[\Symfony\Bridge\Twig\Attribute\Template('product/list.html.twig')]
public function list(): array
{
return ['products' => []];
}
}
The imports of the bundle stay in the file, so the new attributes are written with their fully qualified names, which cannot
clash with them. Delete the imports when you remove the bundle. The annotation @Security, whose expression only calls
is_granted(), becomes #[IsGranted] too, as the page Symfony shows.
What Gets Rewritten
The data of the package dresscode/upgrade-symfony hold 135 changes that Symfony deprecated in versions 6.1 to
6.4 and removed in 7.0. DressCode rewrites 98 of them by itself and reports the other 37. The table shows examples, each
verified by a run of DressCode. The complete list is in the data of the package, one file per component, for example console.neon.
| old API | new API | since |
|---|---|---|
protected static $defaultName = 'app:import'; |
#[AsCommand(name: 'app:import')] on the class |
symfony/console 6.1 |
/** @Route("/orders/{id}") */ from Routing\Annotation |
#[Route('/orders/{id}')] from Routing\Attribute |
symfony/routing 6.4 |
class OrderPlacedHandler implements MessageHandlerInterface |
#[AsMessageHandler] class OrderPlacedHandler |
symfony/messenger 6.2 |
Symfony\Component\Security\Core\Security |
Symfony\Bundle\SecurityBundle\Security |
symfony/security-bundle 6.2 |
$this->renderForm($view, ['form' => $form]) |
$this->render($view, ['form' => $form]) |
symfony/framework-bundle 6.2 |
$request->getContentType() |
$request->getContentTypeFormat() |
symfony/http-foundation 6.2 |
$response->setEtag() |
$response->setEtag(null) |
symfony/http-foundation 6.2 |
/** @required */ |
#[Required] |
symfony/dependency-injection 6.3 |
@Assert\Regex("/^\w+$/") |
#[Assert\Regex('/^\w+$/')] |
symfony/validator 6.4 |
$e->getNestedExceptions() |
\array_values($e->getWrappedExceptions()) |
symfony/messenger 6.4 |
Symfony\Component\HttpKernel\UriSigner |
Symfony\Component\HttpFoundation\UriSigner |
symfony/http-kernel 6.4 |
SendinblueApiTransport |
BrevoApiTransport |
symfony/brevo-mailer 6.4 |
The data have no section for 7.0, because 7.0 brought the same features as 6.4 and only removed what was deprecated. What
Symfony 6.0 changed, such as MASTER_REQUEST renamed to MAIN_REQUEST, belongs to the move from Symfony
5 to 6, and the package covers it as well.
What Gets Reported
Where the replacement is used differently, or there is none, DressCode reports the place, leaves the code alone and says what to write instead. A class that takes services from the container is a typical case:
Class `Symfony\Component\DependencyInjection\ContainerAwareInterface` is deprecated: pass the services the class needs to its constructor, or implement `Symfony\Contracts\Service\ServiceSubscriberInterface` to fetch them lazily, since symfony/dependency-injection 6.4.
| old API | what to write instead | since |
|---|---|---|
ContainerAwareInterface, ContainerAwareTrait |
the services in the constructor, or ServiceSubscriberInterface to fetch them lazily |
symfony/dependency-injection 6.4 |
CacheableSupportsMethodInterface |
getSupportedTypes() of the normalizer, which says for each type whether supports*() may
be cached |
symfony/serializer 6.3 |
PostSubmitEvent::setData() |
setData() of PreSubmitEvent or SubmitEvent, in a listener that runs before the form is
submitted |
symfony/form 6.4 |
Symfony\Bridge\Doctrine\Logger\DbalLogger |
a DBAL middleware such as Doctrine\DBAL\Logging\Middleware |
symfony/doctrine-bridge 6.4 |
StopWorkerOnSignalsListener |
nothing, messenger:consume stops the worker on SIGTERM and SIGINT by itself; remove the
listener |
symfony/messenger 6.4 |
Kernel::stripComments() |
no replacement; copy the method into the project | symfony/http-kernel 6.4 |
Confirm such a place with the comment // dresscode:ignore <decision> -- <reason>, or rewrite it; Manual Tasks Along the Way has the details.
The data do not capture changes in YAML configuration, in Twig templates or in behavior. Symfony 7.0 also added native return types to many methods (Symfony 7.0 type declarations), and a method of yours that overrides one of them needs the same type; the page Symfony says how DressCode writes such signatures.
Where to Go Next
- Symfony, all components the package covers, and the move from Symfony 5 to 6.
- Upgrading code,
upgrade todayin detail, and the upgrade plan with tests and commits. - Upgrading to a newer PHP, because Symfony 7 needs PHP 8.2.