You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: resources/posts/2024/2024-12-17-off-the-beaten-path-to-upgrade-symfony-28-to-72.md
+40-40Lines changed: 40 additions & 40 deletions
Original file line number
Diff line number
Diff line change
@@ -2,25 +2,25 @@
2
2
id: 421
3
3
title: "Off the Beaten Path to Upgrade Symfony 2.8 to 7.2"
4
4
perex: |
5
-
There are 2 types of upgrades - one follows only `UPGRADE.md` files on every release, replace what has been removed with new alternatives. It works and we could say that codebase will be "up-to-date".
5
+
There are two types of upgrades. One follows only `UPGRADE.md` files on every release, replacing what has been removed with new alternatives. It works, and we could say that the codebase will be "up-to-date."
6
6
7
-
The other upgrade doesn't stop at required minimum, but **makes use of all modern features the framework provides**. It will be faster, easier to understand and easier to upgrade to the next versions. I [wrote a post](/blog/two-kinds-of-legacy-code-upgrade) that explains why latter is better.
7
+
The other upgrade doesn't stop at the required minimum but **makes use of all modern features the framework provides**. It will be faster, easier to understand, and easier to upgrade to the next version. I [wrote a post](/blog/two-kinds-of-legacy-code-upgrade) that explains why the latter is better.
8
8
9
-
There are no sources about Symfony upgrade spanning multiple major versions. Time to fix that.
9
+
There are no sources about Symfony upgrades spanning multiple major versions—time to fix that.
10
10
---
11
11
12
-
This was a reply to [question on Reddit](https://www.reddit.com/r/PHP/comments/1hfjn89/good_strategy_when_upgrading_php_symfony_apps/) that grew into a post. Today we look at what lessspoken steps, that bring the real value of modern Symfony. If you do them, a new Symfony develop that joins your team will have good times onboarding and working with your code.
12
+
This was a reply to [question on Reddit](https://www.reddit.com/r/PHP/comments/1hfjn89/good_strategy_when_upgrading_php_symfony_apps/) that grew into a post. Today, we look at the less-spoken steps that bring the real value of modern Symfony. If you do them, a new Symfony developer who joins your team will have a good time onboarding and working with your code.
13
13
14
14
## Happy Path
15
15
16
16
For the happy path upgrade:
17
17
18
18
* see [Rector Symfony upgrade sets](https://getrector.com/find-rule?activeRectorSetGroup=symfony)
19
-
*an`UPGRADE.md` files in Symfony Github repository
19
+
*See`UPGRADE.md` files in the Symfony Github repository
20
20
21
21
<br>
22
22
23
-
Here is a full list of `UPGRADE.md` files: [2.8](https://github.com/symfony/symfony/blob/2.8/UPGRADE-2.8.md), [3.0](https://github.com/symfony/symfony/blob/3.0/UPGRADE-3.0.md),
23
+
Here is a complete list of `UPGRADE.md` files: [2.8](https://github.com/symfony/symfony/blob/2.8/UPGRADE-2.8.md), [3.0](https://github.com/symfony/symfony/blob/3.0/UPGRADE-3.0.md),
@@ -45,46 +45,46 @@ Here is a full list of `UPGRADE.md` files: [2.8](https://github.com/symfony/symf
45
45
46
46
<br>
47
47
48
-
## Can I skip first steps, when we're on Symfony 5?
48
+
## Can I skip the first steps when we're on Symfony 5?
49
49
50
-
In case you're using later version like Symfony 4, 5 or 6, **still check previous steps**. Some projects have Symfony 7 in their `composer.json`, but their syntax and architecture is stuck on Symfony 3 times. Imagine the surprise when they're hiring for a modern Symfony 7 codebase, but then devs sees a code fossil.
50
+
If you're using a later version like Symfony 4, 5, or 6, **still check previous steps**. Some projects have Symfony 7 in their `composer.json`, but their syntax and architecture are stuck on Symfony 3 times. Imagine the surprise when they're hiring for a modern Symfony 7 codebase, but then the developers see a code fossil.
51
51
52
52
## PHP or Symfony first?
53
53
54
54
Should we upgrade first to PHP 8, then start with Symfony 3 to 4 to 5, or vise versa? This is a legit question and one path might lead you to serious turmoil if PHP bugs (don't always trust composer requirements in the past).
55
55
56
-
Here is a simple table of minimal PHP version required by various Symfony versions:
56
+
Here is a simple table of minimal PHP versions required by various Symfony versions:
Symfony 4 or 5 was not tested on PHP 8.0. It should work, but I've experience few bugs when it crashes on invalid internal type or return value. Those are unfixable, as we'd have to change PHP itself.
60
+
Symfony 4 or 5 was not tested on PHP 8.0. It should work, but I've experienced a few bugs when it crashes on an invalid internal type or return value. Those are unfixable, as we'd have to change PHP itself.
61
61
62
-
In my experience it's best to reach the highest Symfony version possible, and if our PHP version blocks us from going further - then upgrade PHP version by single minor step.
62
+
In my experience, it's best to reach the highest Symfony version possible. If our PHP version blocks us from going further, then upgrade the PHP version by a single minor step.
63
63
64
-
E.g. let's say we're upgrading project from Symfony 4 to 7 and we're running PHP 7.4. First we upgrade 4 to 5.0, then to 5.4. Why? Because **Symfony 5.4 is the last version** that runs on PHP 7.4. Symfony 6.0 require us to upgrade the PHP to 8.0. Only then we upgrade PHP 7.4 to 8.0.
64
+
For example, let's say we're upgrading the project from Symfony 4 to 7 and running PHP 7.4. First, we upgrade 4 to 5.0, then to 5.4. Why? Because Symfony 5.4 is the last version** that runs on PHP 7.4. Symfony 6.0 requires us to upgrade PHP to 8.0. Only then do we upgrade PHP 7.4 to 8.0.
65
65
66
66
67
67
## Symfony 3
68
68
69
69
### Leaner Directory Structure
70
70
71
-
Main change in Symfony 3 was a directory structure. In short, everything used to be placed in `/app` and `/Resources`directory. Now everything is directly in the root directory.
71
+
The main change in Symfony 3 was the directory structure. In short, everything used to be placed in the `/app` and `/Resources`directories. Now, everything is directly in the root directory.
Give the `/Resources` directory a good care, as most of templates, translations, tests, configs etc. are nested there. It will make working with Symfony project easier - not just for you, but also to linters, static analyser and Rector.
75
+
Give the `/Resources` directory some love, as most of the templates, translations, tests, configs, etc., are nested there. It will make working with the Symfony project easier—not just for you but also for linters, static analyzers, and Rector.
76
76
77
77
<br>
78
78
79
79
### From named Services to Constructor Injection
80
80
81
-
The 2nd important change is moving from string-named service and global container:
81
+
The 2nd important change is moving from a string-named service to and global container...
82
82
83
83
```php
84
84
$someService = $this->get('some_service');
85
85
```
86
86
87
-
to a typed constructor injection:
87
+
...to a typed constructor injection:
88
88
89
89
```php
90
90
/**
@@ -98,28 +98,28 @@ public function __construct(SomeService $someService)
98
98
}
99
99
```
100
100
101
-
This is mostly spread in:
101
+
This is mainly spread in:
102
102
103
103
* controllers
104
104
* commands
105
105
* even subscribers
106
-
* anywhere where container is available
106
+
* anywhere where a container is available
107
107
108
-
This sole change consumes **~40 % of all upgrade time**. But the value increase is hundred-fold. Constructor injection is one of the best patterns that lead to clean and adaptable architecture.
108
+
This sole change consumes **~40 % of all upgrade time**, but the value increase is a hundredfold. Constructor injection is one of the best patterns for creating clean and adaptable architecture.
109
109
110
-
There is a [special Rector set](https://github.com/rectorphp/rector-symfony/blob/main/config/sets/symfony/symfony-constructor-injection.php) to help with upgrade. Use one rule at a time, refactor your code, send pull-request, merge then add the next rule.
110
+
There is a [special Rector set](https://github.com/rectorphp/rector-symfony/blob/main/config/sets/symfony/symfony-constructor-injection.php) to help with the upgrade. Use one rule at a time, refactor your code, send a pullrequest, merge, and then add the next rule.
111
111
112
112
<br>
113
113
114
114
In short, the change is:
115
115
116
-
* to use constructor injection, service must be explicitly registered in `services.yml`
117
-
*the controller class must extend `Symfony\Bundle\FrameworkBundle\Controller\AbstractController` class
118
-
* all the injected services must be explicitly registered in `services.yml` too
116
+
* to use constructor injection, the service must be explicitly registered in `services.yml`
117
+
*The controller class must extend the`Symfony\Bundle\FrameworkBundle\Controller\AbstractController` class
118
+
* all the injected services must be explicitly registered in `services.yml`, too
119
119
120
120
### From Explicit Service Arguments to Autowire
121
121
122
-
Also, you can cleanup your service configs:
122
+
Also, you can clean your service configs:
123
123
124
124
```diff
125
125
# services.yml
@@ -133,11 +133,11 @@ Also, you can cleanup your service configs:
133
133
- $someArgument: '@AnotherType'
134
134
```
135
135
136
-
I wrote a [dedicated post about configs upgrade](/blog/2017/05/07/how-to-refactor-to-new-dependency-injection-features-in-symfony-3-3/), so you wont miss any line you can remove.
136
+
I wrote a [dedicated post about configs upgrade](/blog/2017/05/07/how-to-refactor-to-new-dependency-injection-features-in-symfony-3-3/), so you won't miss any line you can remove.
137
137
138
138
### Do the Monorepo Split
139
139
140
-
Is your project still using follwing dependency in `composer.json`?
140
+
Is your project still using the following dependency in `composer.json`?
141
141
142
142
```json
143
143
{
@@ -147,7 +147,7 @@ Is your project still using follwing dependency in `composer.json`?
147
147
}
148
148
```
149
149
150
-
Flip this to a monorepo split. Instead require each package separatelly:
150
+
Flip this to a monorepo split. Instead, require each package separately:
151
151
152
152
```json
153
153
{
@@ -197,11 +197,11 @@ Packages that are hard to bump and should go the last:
197
197
198
198
## Symfony 4
199
199
200
-
You can upgrade to Symfony 4 while handling named services upgrade, but Symfony 4.4 is a last one that allow it. Symfony 5 would crash.
200
+
You can upgrade to Symfony 4 while handling named services upgrades, but Symfony 4.4 is the last one to allow it. Symfony 5 would crash.
201
201
202
202
### PSR-4 Autodiscovery
203
203
204
-
Symfony 3.3 introduced [PSR-4based service discovery](https://symfony.com/blog/new-in-symfony-3-3-psr-4-based-service-discovery). It was slightly buggy till Symfony 4.0, so I would first upgrade to Symfony 4 before starting using it.
204
+
Symfony 3.3 introduced [PSR-4-based service discovery](https://symfony.com/blog/new-in-symfony-3-3-psr-4-based-service-discovery). It was slightly buggy until Symfony 4.0, so I would first upgrade to Symfony 4 before using it.
205
205
206
206
What is it? Before, we had to register each service manually - one by one:
207
207
@@ -228,17 +228,17 @@ If we add a new `*Repository` class, Symfony will automatically pick it up.
228
228
229
229
### From YAML to PHP configs
230
230
231
-
You've noticed we're not using the YAML syntax anymore. Why? Symfony 3.4 has added a [PHP fluent syntax](https://symfony.com/blog/new-in-symfony-3-4-php-based-configuration-for-services-and-routes) for configs. Again, better wait for Symfony 4 to make it work well.
231
+
You've noticed we're not using the YAML syntax anymore. Why? Symfony 3.4 has added a [PHP fluent syntax](https://symfony.com/blog/new-in-symfony-3-4-php-based-configuration-for-services-and-routes) for configs. Again, we better wait for Symfony 4 to make it reliable.
232
232
233
-
First, let me give you [10 reasons, why PHP beats YAML](/blog/2020/07/16/10-cool-features-you-get-after-switching-from-yaml-to-php-configs/).
233
+
First, let me give you [10 reasons why PHP beats YAML](/blog/2020/07/16/10-cool-features-you-get-after-switching-from-yaml-to-php-configs/).
234
234
235
235
"But we have over 50 configs", you say.
236
236
237
237
No worries, **there is a [migration tool that automates 99 % of the process](/blog/2020/07/27/how-to-switch-from-yaml-xml-configs-to-php-today-with-migrify/)**.
238
238
239
239
<br>
240
240
241
-
Last but not least, I wrote a [dedicated post about benefits modern PHP Symfony configs](https://getrector.com/blog/modernize-symfony-configs), how works perfectly with PHPStan deprecation rules to **get warnings about deprecated config methods**. This is not possible with YAML.
241
+
Last, but not least, I wrote a [dedicated post about the benefits of modern PHP Symfony configs](https://getrector.com/blog/modernize-symfony-configs), how it works perfectly with PHPStan deprecation rules to **get warnings about deprecated config methods**. This is not possible with YAML.
Symfony 5.2 added [`#[Route]` and `#[Required]` attributes](https://symfony.com/blog/new-in-symfony-5-2-php-8-attributes). As I've said above, first we should upgrade to Symfony 5.4 while still on PHP 7.4. Only then we should upgrade to PHP 8.0.
282
+
Symfony 5.2 added [# [Route] and # [Required] attributes](https://symfony.com/blog/new-in-symfony-5-2-php-8-attributes). As I've said above, we should first upgrade to Symfony 5.4 while still on PHP 7.4 and then to PHP 8.0.
The `->withAttributesSets()` method enables all attributes sets that are relevant to your project. But don't worry, it will not start upgrading until we're on PHP 8.0. Once we're on PHP 8.0, it will only upgrade those attributes that really exists.
295
+
The `->withAttributesSets()` method enables all relevant attribute sets in your project. But don't worry; it will only start upgrading once we're on PHP 8.0. Once we're on PHP 8.0, it will only upgrade those attributes that really exist.
296
296
297
297
298
298
<br>
@@ -301,9 +301,9 @@ The `->withAttributesSets()` method enables all attributes sets that are relevan
301
301
302
302
Symfony 3 introduced a new way to handle authentication - Guard. It got [further improved](https://symfony.com/blog/new-in-symfony-3-4-guard-authentication-improvements) and promoted. Then [deprecated in Symfony 5.3](https://symfony.com/blog/new-in-symfony-5-3-guard-component-deprecation) to be replaced with [new authentication system](https://symfony.com/blog/new-in-symfony-5-1-updated-security-system).
303
303
304
-
This is the most dynamic component in Symfony that keeps changing every major version. It's like the oposite of Form component.
304
+
This is the most dynamic component in Symfony, changing with every major version. It's like the opposite of the Form component.
305
305
306
-
I've had the fortune to work with projects that were not coupled to Symfony security. If that was the case, we **slowly decoupled authentication logic from Symfony security to our own**. It made and will our migration much easier.
306
+
I've had the fortune to work with projects that were not coupled with Symfony security. If that was the case, we **slowly decoupled authentication logic from Symfony security to our own**. It made and will make our migration much easier.
307
307
308
308
Security upgrade depends on each specific project, but it's worth skipping the Security Guard completely. It's a dead end.
309
309
@@ -313,11 +313,11 @@ Security upgrade depends on each specific project, but it's worth skipping the S
313
313
314
314
### Attributes Everywhere
315
315
316
-
When we reach PHP 8.0 and Symfony 6.0, **95 % work is already behind us**. Symfony 6 and 7 are stabilazing and relaxing releases. They're mostly about syntax sugar and more attributes. I would not recommend using them all blindly, just because it's PHP 8.0 syntax though.
316
+
When we reach PHP 8.0 and Symfony 6.0, **95 % of the work is already behind us**. Symfony 6 and 7 are stabilizing and relaxing releases. They're mostly about syntax sugar and more attributes. I would not recommend using them all blindly just because it's PHP 8.0 syntax, though.
317
317
318
318
<br>
319
319
320
-
But there are few worth mentioning that bring a value:
320
+
But there are a few worth mentioning that bring a value:
@@ -355,7 +355,7 @@ They both allow to drop even more config blurbs and make them more leaner.
355
355
356
356
<br>
357
357
358
-
This would be the offthebeaten path to Symfony 2.8 to 7.2 upgrade. It's a lot of work, but with the right tools and mindset, it can be done in a months. Final result is worth it! Good luck and have fun.
358
+
This would be the off-the-beaten path to upgrading Symfony 2.8 to 7.2. It's a lot of work, but with the right tools and mindset, it can be done in months. The final result is worth it! Good luck and have fun.
0 commit comments