Switch from deprecated --set
option to rector.php
config.
Symfony recently announced a new version of Twig with namespaces as we know it. Before PHP 5.2 there was Underscored_Namespace
- I remember because that was the first version I used.
Today I'll show you how to upgrade from _
to \\
in few minutes seconds.
This set would not be possible and as good as it is without you, open-source PHP community. I'd like to thank 👏:
<ul>
<li><strong><a href="https://github.com/greg0ire">greg0ire</a></strong> <a href="https://github.com/rectorphp/rector/commit/493e418f4691f3a4beadf901bd54ea7406380891">for contributing</a> to this set</li>
<li>and <strong><a href="https://github.com/enumag">enumag</a></strong> for <a href="https://github.com/rectorphp/rector/search?q=twig+is%3Aissue+author%3Aenumag&unscoped_q=twig+is%3Aissue+author%3Aenumag&type=Issues">battle testing and reported issues</a></li>
</ul>
So all we need to do is replace Twig_
with Twig\
?
-Twig_Function
+Twig\Function
This would fail since Twig\Function
class doesn't exist. Twig\TwigFunction
does. There 150 more cases where find and replace fails.
We need to replace both docblocks:
/**
- * @throws \Twig_Error_Loader
+ * @throws \Twig\Error\LoaderError
*/
public function render(): void
{
- /** @var \Twig_Environment $env */
+ /** @var \Twig\Environment $env */
$env = $this->getTwigEnv();
// ...
}
And the code:
-$safeTwigEnvironment = new \Twig_Environment(
+$safeTwigEnvironment = new \Twig\Environment(
- new \Twig_Loader_Array([])
+ new \Twig\Loader\ArrayLoader([])
);
In a reaction to the Symfony blog post, I see many developers do upgrades manually. In case of 50 changes, it's ok, but private code bases will go 1000+ use cases.
For Rector it just 1 pattern to refactor. Just tell him to process your files src
:
composer require rector/rector --dev
rector.php
use Rector\Symfony\Set\TwigSetList;
use Rector\Config\RectorConfig;
return function (RectorConfig $rectorConfig): void {
$rectorConfig->import(TwigSetList::TWIG_UNDERSCORE_TO_NAMESPACE);
};
vendor/bin/rector process src
Happy coding!
Do you learn from my contents or use open-souce packages like Rector every day?
Consider supporting it on GitHub Sponsors.
I'd really appreciate it!