gstreamer0.10-ffmpeg
gstreamer0.10-plugins-good
packages.
Rector just automated several changes to our app that are needed to remove deprecations on Symfony 5.4. Plus it did some bonus refactoring, like adding the optional Response
return type on our controllers.
But as nice as that is, it's not perfect. All the class names are inlined, instead of having a use
statement. And even though it renamed some interfaces, it didn't rename the methods that we call on those objects to reflect the change. No need to worry, though. Rector gave us a great start and helped highlight several changes that we need to make. Now, let's finish the job.
First, for these long class names with no use
statement and, in general for coding styles, Rector doesn't know what coding style we prefer, so it doesn't even try to format things correctly. The official recommendation is to use a tool in your project like PHP CS Fixer to reformat the code after running Rector. PHP CS Fixer is a great tool anyway... so let's get it installed so it can help us along our journey.
You can install PHP CS Fixer a few different ways, but oddly enough, the recommended way - and the way that I like - is to install it via Composer into its own directory. Run:
mkdir -p tools/php-cs-fixer
There's nothing special here: just a new tools/
directory with php-cs-fixer/
inside. Now install it into that directory by running composer require --working-dir=tools/php-cs-fixer
- that tells Composer to behave like I'm running it from inside of tools/php-cs-fixer
- and then friendsofphp/php-cs-fixer
.
If you're wondering why we're not just installing this directly into our main composer.json
dependencies, well... that's a bit tricky. PHP CS Fixer is a standalone executable tool. If I install it into our app's dependencies, then it could cause problems if some of its dependencies don't match versions that we have already in our app. Well, really, this is a potential problem whenever you install any library. But since all we need is a standalone binary... there's no reason to mix it into our app. We could have done the same thing with Rector.
This gives us, in that directory, composer.json
and composer.lock
files. And in its vendor/bin
directory... yes: php-cs-fixer
. That's the executable.
And because we have a new vendor/
directory, open up the root .gitignore
file and, at the bottom, ignore that: /tools/php-cs-fixer/vendor
. And while we're here, let's also ignore /.php-cs-fixer.cache
. That's a cache file that PHP CS Fixer will create when it does its work.
... lines 1 - 23 | |
/tools/php-cs-fixer/vendor/ | |
/.php-cs-fixer.cache |
The last thing we need to do is add a config file. Up here, create a new file called .php-cs-fixer.php
. Inside, I'm going to paste about 10 lines of code. This is pretty simple. It tells PHP CS Fixer where to find our src/
files... then, below, which rules to apply. I'm using a pretty standard Symfony set of rules.
... lines 1 - 2 | |
$finder = PhpCsFixer\Finder::create() | |
->in(__DIR__.'/src') | |
; | |
$config = new PhpCsFixer\Config(); | |
return $config->setRules([ | |
'@Symfony' => true, | |
'yoda_style' => false, | |
]) | |
->setFinder($finder) | |
; |
And... we're ready to run this! To see what it does, over at the command line, add all the changes to git with:
git add .
Then check on them:
git status
But don't commit them yet. I still want to be able to review the changes that Rector made before we finally commit. But at least, now, we'll be able to see what PHP CS Fixer does.
Let's run it:
./tools/php-cs-fixer/vendor/bin/php-cs-fixer fix
And... nice! It modified 6 files. Let's check them out!
git diff
Awesome! It removed the long class names for Response
across our entire codebase! It also deleted a few old use
statements that we don't need. So the code from Rector still isn't perfect, but that was a nice step towards making it better!
For the final fixes, we'll do them manually by digging into the changes that Rector made, one by one. I'll help out by zooming us into the places that need updates.
The first is RegistrationController
: src/Controller/RegistrationController.php
. This is one of the places where it changed UserPasswordEncoderInterface
to UserPasswordHasherInterface
. Notice that PHP CS Fixer did fix a lot of the long, inlined class names... but not all of them. It depends on if there was already a use
statement for that class or not.
So let's fix this by hand. Hover over the class, hit "alt" + "enter" and then go to "Simplify FQN". That shortens it and adds the use
statement on top.
... lines 1 - 11 | |
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; | |
... lines 13 - 16 | |
class RegistrationController extends AbstractController | |
{ | |
... lines 19 - 21 | |
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, VerifyEmailHelperInterface $verifyEmailHelper): Response | |
... lines 23 - 99 | |
} |
But there's another problem. If we trace down to where this is used, previously we were calling ->encodePassword()
. But... that method doesn't exist on the new interface! We need to call ->hashPassword()
.
I'm also going to rename the argument. Go to "Refactor" then "Rename" and call it $userPasswordHasher
... just because that's a more fitting name.
... lines 1 - 21 | |
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, VerifyEmailHelperInterface $verifyEmailHelper): Response | |
{ | |
... lines 24 - 27 | |
if ($form->isSubmitted() && $form->isValid()) { | |
... line 29 | |
$user->setPassword( | |
$userPasswordHasher->hashPassword( | |
... lines 32 - 34 | |
); | |
... lines 36 - 55 | |
} | |
... lines 57 - 60 | |
} | |
... lines 62 - 101 |
Next up is src/Factory/UserFactory.php
for the same change. Scroll down and... once again, we have a long class name. Hit "alt" + "enter" and go to "Simplify FQN" to add that use
statement. Then... let's "Refactor" and "Rename" the argument to $passwordHasher
... good... and "Refactor", "Rename" the property also to $passwordHasher
.
... lines 1 - 6 | |
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; | |
... lines 8 - 29 | |
final class UserFactory extends ModelFactory | |
{ | |
private UserPasswordHasherInterface $userPasswordHasher; | |
public function __construct(UserPasswordHasherInterface $userPasswordHasher) | |
{ | |
... lines 36 - 37 | |
$this->userPasswordHasher = $userPasswordHasher; | |
} | |
... lines 40 - 68 | |
} |
Finally, below, we need to call ->hashPassword()
instead of ->encodePassword()
.
... lines 1 - 50 | |
protected function initialize(): self | |
{ | |
... line 53 | |
return $this | |
->afterInstantiate(function (User $user) { | |
if ($user->getPlainPassword()) { | |
$user->setPassword( | |
$this->userPasswordHasher->hashPassword($user, $user->getPlainPassword()) | |
); | |
} | |
}) | |
; | |
} | |
... lines 64 - 70 |
Done!
Just one more spot where we need this same change: src/Security/LoginFormAuthenticator.php
. We're going to refactor this class later to use the new security system... but let's at least get it working. Find the UserPasswordHasherInterface
argument, shorten that with "Simplify FQN"... then rename the argument to $passwordHasher
... and rename the property to $passwordHasher
.
... lines 1 - 8 | |
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; | |
... lines 10 - 24 | |
class LoginFormAuthenticator extends AbstractFormLoginAuthenticator implements PasswordAuthenticatedInterface | |
{ | |
... lines 27 - 34 | |
private $userPasswordHasher; | |
public function __construct(SessionInterface $session, EntityManagerInterface $entityManager, UrlGeneratorInterface $urlGenerator, CsrfTokenManagerInterface $csrfTokenManager, UserPasswordHasherInterface $userPasswordHasher) | |
{ | |
... lines 39 - 42 | |
$this->userPasswordHasher = $userPasswordHasher; | |
} | |
... lines 45 - 119 | |
} |
Then we check to see where this is used... I'll search for "hasher"... there we go! Down on line 84, the ->isPasswordValid()
actually does exist on the new interface, so this is one case where we don't need to change anything else.
... lines 1 - 14 | |
use Symfony\Component\Security\Core\Exception\UserNotFoundException; | |
... lines 16 - 24 | |
class LoginFormAuthenticator extends AbstractFormLoginAuthenticator implements PasswordAuthenticatedInterface | |
{ | |
... lines 27 - 66 | |
public function getUser($credentials, UserProviderInterface $userProvider) | |
{ | |
... lines 69 - 75 | |
if (!$user) { | |
throw new UserNotFoundException('Email could not be found.'); | |
} | |
... lines 79 - 80 | |
} | |
... lines 82 - 119 | |
} |
Oh, but while we're in here, the UserNotFoundException
is another long class name. Hit "Simplify FQN" again.
Beautiful! That should be everything.
The big question now is: does our app work? If we go back to the Homepage... it doesn't. We're back on the Welcome to Symfony page? That's weird...
Spin back over to your terminal and run:
php bin/console debug:router
Wow. In fact, all of our routes are gone. This is due to one other change that Rector made that we need to pay close attention to. It's inside of our Kernel
class. We're going to talk more about this class later when we upgrade our recipes. Rector changed the argument to RoutingConfigurator
, but it didn't update the code below.
... lines 1 - 10 | |
class Kernel extends BaseKernel | |
{ | |
... lines 13 - 44 | |
protected function configureRoutes(\Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator $routes): void | |
{ | |
$confDir = $this->getProjectDir().'/config'; | |
$routes->import($confDir.'/{routes}/'.$this->environment.'/*'.self::CONFIG_EXTS, '/', 'glob'); | |
$routes->import($confDir.'/{routes}/*'.self::CONFIG_EXTS, '/', 'glob'); | |
$routes->import($confDir.'/{routes}'.self::CONFIG_EXTS, '/', 'glob'); | |
} | |
} |
So again, Rector is really good for finding some of these changes, but you should always double-check the final result.
Fortunately, the entire configureRoutes()
method has been moved into this MicroKernelTrait
- a fact I'll talk about more soon. So we don't need this method in our class at all anymore. As soon as we delete it, the correct version from the trait is used... our routes are back.... and the page works! Woohoo!
And hopefully we have a few less deprecations than before. I now see 58. Progress!
So what's next? We've upgraded our dependencies and automated some of the changes we need with Rector. Well, there's still one more thing we can do before we start going through each deprecation manually: updating our recipes. And this has gotten a whole heck of a lot easier than the last time you upgraded. Let's find out how next.
Hey Thomas!
Apologies for the super slow reply! And... hmm. I might have a guess... or maybe it's too simple ;). Here's what the .php-cs-fixer.php
looks like from this project:
$finder = PhpCsFixer\Finder::create()
->in(__DIR__.'/src')
;
$config = new PhpCsFixer\Config();
return $config->setRules([
'@Symfony' => true,
'yoda_style' => false,
'class_attributes_separation' => [
'elements' => ['method' => 'one', 'property' => 'one', 'trait_import' => 'one']
]
])
->setFinder($finder)
;
If you comment-out/remove the ->in()
line, you'll get the error. Is it possible you're just missing that part?
Cheers!
// composer.json
{
"require": {
"php": "^8.0.2",
"ext-ctype": "*",
"ext-iconv": "*",
"babdev/pagerfanta-bundle": "^3.6", // v3.6.1
"composer/package-versions-deprecated": "^1.11", // 1.11.99.5
"doctrine/annotations": "^1.13", // 1.13.2
"doctrine/dbal": "^3.3", // 3.3.5
"doctrine/doctrine-bundle": "^2.0", // 2.6.2
"doctrine/doctrine-migrations-bundle": "^3.2", // 3.2.2
"doctrine/orm": "^2.0", // 2.11.2
"knplabs/knp-markdown-bundle": "^1.8", // 1.10.0
"knplabs/knp-time-bundle": "^1.18", // v1.18.0
"pagerfanta/doctrine-orm-adapter": "^3.6", // v3.6.1
"pagerfanta/twig": "^3.6", // v3.6.1
"sensio/framework-extra-bundle": "^6.0", // v6.2.6
"sentry/sentry-symfony": "^4.0", // 4.2.8
"stof/doctrine-extensions-bundle": "^1.5", // v1.7.0
"symfony/asset": "6.0.*", // v6.0.7
"symfony/console": "6.0.*", // v6.0.7
"symfony/dotenv": "6.0.*", // v6.0.5
"symfony/flex": "^2.1", // v2.1.7
"symfony/form": "6.0.*", // v6.0.7
"symfony/framework-bundle": "6.0.*", // v6.0.7
"symfony/mailer": "6.0.*", // v6.0.5
"symfony/monolog-bundle": "^3.0", // v3.7.1
"symfony/property-access": "6.0.*", // v6.0.7
"symfony/property-info": "6.0.*", // v6.0.7
"symfony/proxy-manager-bridge": "6.0.*", // v6.0.6
"symfony/routing": "6.0.*", // v6.0.5
"symfony/runtime": "6.0.*", // v6.0.7
"symfony/security-bundle": "6.0.*", // v6.0.5
"symfony/serializer": "6.0.*", // v6.0.7
"symfony/stopwatch": "6.0.*", // v6.0.5
"symfony/twig-bundle": "6.0.*", // v6.0.3
"symfony/ux-chartjs": "^2.0", // v2.1.0
"symfony/validator": "6.0.*", // v6.0.7
"symfony/webpack-encore-bundle": "^1.7", // v1.14.0
"symfony/yaml": "6.0.*", // v6.0.3
"symfonycasts/verify-email-bundle": "^1.7", // v1.10.0
"twig/extra-bundle": "^2.12|^3.0", // v3.3.8
"twig/string-extra": "^3.3", // v3.3.5
"twig/twig": "^2.12|^3.0" // v3.3.10
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.4", // 3.4.1
"phpunit/phpunit": "^9.5", // 9.5.20
"rector/rector": "^0.12.17", // 0.12.20
"symfony/debug-bundle": "6.0.*", // v6.0.3
"symfony/maker-bundle": "^1.15", // v1.38.0
"symfony/var-dumper": "6.0.*", // v6.0.6
"symfony/web-profiler-bundle": "6.0.*", // v6.0.6
"zenstruck/foundry": "^1.16" // v1.18.0
}
}
Hi, when running "./tools/php-cs-fixer/vendor/bin/php-cs-fixer fix" I get the error "You must call one of in() or append() methods before iterating over a Finder."
I followed the video and on the first app this worked fine. Does anyone have any thoughts on what may be causing this?
Thanks in advance