For coders TYPO3 Tech Corner

[TYPO3] Use rector to automatically update old extensions

[TYPO3] Use rector to automatically update old extensions

I think that many of us have already faced the hurdle of getting an old extension that is no longer maintained for a new TYPO3 version to work again. Sometimes this is a lot of manual work. There is an automated solution for this with the PHP package rector/rector, which can make changes to the code of extensions directly.

Rector: Installation and configuration

You can simply install rector via composer:

 

composer require --dev rector/rector

 

 

Then you can have a configuration file for TYPO3 created automatically:

 

./vendor/bin/rector init --template-type=typo3

 

 

This file (rector.php) is now in your project root directory. Here you can still set which changes in the code of your extension should be checked for. The set lists and the individual services that can be loaded are particularly important. The following is a greatly simplified example of a configuration of a TYPO3 extension that was created for TYPO3 8, but is now required in TYPO3 10:

 

<?php ... return static function (ContainerConfigurator $containerConfigurator): void { // get parameters $parameters = $containerConfigurator->parameters(); $containerConfigurator->import(Typo3SetList::TYPO3_87); $containerConfigurator->import(Typo3SetList::TYPO3_95); $containerConfigurator->import(Typo3SetList::TYPO3_104); $containerConfigurator->import(Typo3SetList::TYPOSCRIPT_CONDITIONS_95); $containerConfigurator->import(Typo3SetList::TYPOSCRIPT_CONDITIONS_104); $containerConfigurator->import(Typo3SetList::COMPOSER_PACKAGES_87_EXTENSIONS); $containerConfigurator->import(Typo3SetList::COMPOSER_PACKAGES_95_CORE); $containerConfigurator->import(Typo3SetList::COMPOSER_PACKAGES_95_EXTENSIONS); $containerConfigurator->import(Typo3SetList::COMPOSER_PACKAGES_104_CORE); $containerConfigurator->import(Typo3SetList::COMPOSER_PACKAGES_104_EXTENSIONS); $containerConfigurator->import(Typo3SetList::COMPOSER_PACKAGES_TER_TO_PACKAGIST); $containerConfigurator->import(Typo3SetList::DATABASE_TO_DBAL); $containerConfigurator->import(\Ssch\TYPO3Rector\Set\Extension\NimutTestingFrameworkSetList::NIMUT_TESTING_FRAMEWORK_TO_TYPO3_TESTING_FRAMEWORK); $containerConfigurator->import(Typo3SetList::EXTBASE_COMMAND_CONTROLLERS_TO_SYMFONY_COMMANDS); ... $services = $containerConfigurator->services(); $services->set(\Ssch\TYPO3Rector\Rector\General\ConvertTypo3ConfVarsRector::class); $services->set(\Ssch\TYPO3Rector\FileProcessor\Fluid\Rector\DefaultSwitchFluidRector::class); $services->set(\Ssch\TYPO3Rector\FileProcessor\Yaml\Form\Rector\EmailFinisherRector::class); $services->set(\Ssch\TYPO3Rector\Rector\General\ExtEmConfRector::class); $services->set(\Ssch\TYPO3Rector\FileProcessor\TypoScript\Visitors\ExtbasePersistenceVisitor::class); $services->set(\Ssch\TYPO3Rector\FileProcessor\Composer\Rector\ExtensionComposerRector::class); $services->set(\Ssch\TYPO3Rector\FileProcessor\Resources\Icons\Rector\IconsRector::class); $services->set(\Ssch\TYPO3Rector\Rector\Experimental\OptionalConstructorToHardRequirementRector::class); $services->set(\Ssch\TYPO3Rector\FileProcessor\Composer\Rector\RemoveCmsPackageDirFromExtraComposerRector::class); $services->set(\Ssch\TYPO3Rector\Rector\Migrations\RenameClassMapAliasRector::class); $services->set(\Ssch\TYPO3Rector\FileProcessor\FlexForms\Rector\RenderTypeFlexFormRector::class); $services->set(\Ssch\TYPO3Rector\FileProcessor\TypoScript\Visitors\FileIncludeToImportStatementVisitor::class); ...

 

 

And you can have your files updated automatically:

 

# Test which files can be changed ./vendor/bin/rector process private/typo3conf/ext/extensionkey --dry-run # Change files now ./vendor/bin/rector process private/typo3conf/ext/extensionkey

 

 

Rector: Best practice

Without a doubt, Rector is a great piece of software that is being improved every day. Using it can save you hours of tinkering, research and, above all, trial and error.

Nevertheless, it is possible that tomorrow a new rule will be added that is still missing today. In addition, you may have to live with the fact that not every line of code can be converted automatically. For some methods, there may be several ways in which manual intervention is required. Other substitutions may not be as perfect as you would like them to be.

We therefore suggest a combination of automated and manual revision. With the help of GIT you can, for example, commit your changes in the extension. So if automatic adjustments are made afterwards, you can control the changes in your IDE and accept or roll back. For example, PhpStorm shows you exactly what has been changed.

Tip: When comparing you should check "Ignore whitespace" if, for example, tabs have been changed to Spaces, so you don't miss an important change.

By the way: In addition to the rules specifically for TYPO3, there are also a number of general PHP settings. Especially if the PHP version of your TYPO3 instance changes, it is worth taking a look here.

Back

"Code faster, look at the time" - does this sound familiar to you?

How about time and respect for code quality? Working in a team? Automated tests?

Join us