Skip to content

Commit

Permalink
[TASK] Add init command to generate configuration file (#1673)
Browse files Browse the repository at this point in the history
Resolves: #1672
  • Loading branch information
sabbelasichon authored Nov 26, 2020
1 parent cc20ad3 commit 6d55943
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 52 deletions.
63 changes: 11 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ Apply automatic fixes on your TYPO3 code.

[![Coverage Status](https://img.shields.io/coveralls/sabbelasichon/typo3-rector/master.svg?style=flat-square)](https://coveralls.io/github/sabbelasichon/typo3-rector?branch=master)

[Rector](https://getrector.org/) aims to provide instant upgrades and instant refactoring of any PHP 5.3+ code. This project adds rectors specific to TYPO3 to help you migrate between TYPO3 releases.
[Rector](https://getrector.org/) aims to provide instant upgrades and instant refactoring of any PHP 5.3+ code. This project adds rectors specific to TYPO3 to help you migrate between TYPO3 releases or keep your code deprecation free.

## Let´s see some examples in action
Let´s see some "rules" in action. Let´s say you have a Fluid ViewHelper looking like this:
Let´s say you have a Fluid ViewHelper looking like this:

```php
class InArrayViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
Expand All @@ -36,7 +36,7 @@ class InArrayViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHel

What´s "wrong" with this code? Well, it depends on the context. But, if we assume you would like to have this code ready for TYPO3 version 10 you should move the render method arguments to the method initializeArguments and you should rename the namespace \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper to \TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper.

And we are not talking about the superfluous else statement and not having Type Declarations if we would like to use modern PHP. That´s a different story.
And we are not talking about the superfluous else statement, not having Type Declarations or in_array could be used. That´s a different story.

Do you like to do these changes manually on a codebase with let´s say 40-100 ViewHelpers? We don´t. So let Rector do the heavy work for us and apply the "rules" MoveRenderArgumentsToInitializeArgumentsMethodRector and RenameClassMapAliasRector for Version 9.5.

Expand Down Expand Up @@ -200,60 +200,19 @@ php ~/.composer/vendor/bin/typo3-rector process public/typo3conf/ext/your_extens

## Configuration and Processing

This library ships already with a bunch of configuration files organized by TYPO3 version. [show config](/config/)
Let´s say want to migrate from version 8.x to 9.x, you could import the config sets for v8 and v9.
So create your own configuration file called rector.php in the root of your project the following way:
This library ships already with a bunch of configuration files organized by TYPO3 version.
To get you started quickly run the following command inside the root directory of your project:

```php
<?php

use Rector\Core\Configuration\Option;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Ssch\TYPO3Rector\Set\Typo3SetList;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::SETS, [
Typo3SetList::TYPO3_87,
Typo3SetList::TYPO3_95,
]
);

// FQN classes are not imported by default. If you don't do it manually after every Rector run, enable it by:
$parameters->set(Option::AUTO_IMPORT_NAMES, true);

// this will not import root namespace classes, like \DateTime or \Exception
$parameters->set(Option::IMPORT_SHORT_CLASSES, false);

// this will not import classes used in PHP DocBlocks, like in /** @var \Some\Class */
$parameters->set(Option::IMPORT_DOC_BLOCKS, false);

$parameters->set(Option::PHP_VERSION_FEATURES, '7.2');

// If you set option Option::AUTO_IMPORT_NAMES to true, you should consider excluding some TYPO3 files.
$parameters->set(Option::EXCLUDE_PATHS, [
'ClassAliasMap.php',
'class.ext_update.php',
'ext_localconf.php',
'ext_emconf.php',
'ext_tables.php',
__DIR__ . '/packages/my_package/Configuration/*'
]);

// If you have trouble that rector cannot run because some TYPO3 constants are not defined add an additional constants file
// Have a look at https://github.com/sabbelasichon/typo3-rector/typo3.constants.php
$parameters->set(Option::AUTOLOAD_PATHS, [
__DIR__ . '/typo3.constants.php'
]);
};
```bash
./vendor/bin/typo3-rector typo3-init
```

See [Rector README](https://github.com/rectorphp/rector#configuration) for full configuration options.
The command generates a basic configuration skeleton which you can adapt to your needs.
The file is fully of comments so you can follow along what is going on.

This configuration applies all available rectors defined in the different PHP files for version 8.x to 9.x with PHP 7.2 capabilities.
Additionally we auto import the FQCN except for PHP core classes and within DocBlocks.
For more configuration options see [Rector README](https://github.com/rectorphp/rector#configuration).

After the configuration run typo3-rector to simulate (hence the option -n) the code fixes:
After your adopt the configuration to your needs, run typo3-rector to simulate (hence the option -n) the future code fixes:

```bash
./vendor/bin/typo3-rector process packages/my_custom_extension --dry-run
Expand Down
54 changes: 54 additions & 0 deletions src/Console/Command/Typo3InitCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Console\Command;

use Rector\Core\Console\Command\AbstractCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symplify\PackageBuilder\Console\Command\CommandNaming;
use Symplify\PackageBuilder\Console\ShellCode;
use Symplify\SmartFileSystem\SmartFileSystem;

final class Typo3InitCommand extends AbstractCommand
{
/**
* @var SmartFileSystem
*/
private $smartFileSystem;

/**
* @var SymfonyStyle
*/
private $symfonyStyle;

public function __construct(SmartFileSystem $smartFileSystem, SymfonyStyle $symfonyStyle)
{
parent::__construct();

$this->smartFileSystem = $smartFileSystem;
$this->symfonyStyle = $symfonyStyle;
}

protected function configure(): void
{
$this->setName(CommandNaming::classToName(self::class));
$this->setDescription('Generate rector.php configuration file specific for TYPO3');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$rectorConfigFiles = $this->smartFileSystem->exists(getcwd() . '/rector.php');

if (! $rectorConfigFiles) {
$this->smartFileSystem->copy(__DIR__ . '/../../../templates/rector.php.dist', getcwd() . '/rector.php');
$this->symfonyStyle->success('"rector.php" config file has been generated successfully!');
} else {
$this->symfonyStyle->error('Config file not generated. A "rector.php" configuration file already exists');
}

return ShellCode::SUCCESS;
}
}
52 changes: 52 additions & 0 deletions templates/rector.php.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Ssch\TYPO3Rector\Set;
use Ssch\TYPO3Rector\Rector\v9\v0\InjectAnnotationRector;

return static function (ContainerConfigurator $containerConfigurator): void {
// get parameters
$parameters = $containerConfigurator->parameters();

// Define what rule sets will be applied
$parameters->set(Option::SETS, [
Typo3SetList::TYPO3_95,
]);

// FQN classes are not imported by default. If you don't do it manually after every Rector run, enable it by:
$parameters->set(Option::AUTO_IMPORT_NAMES, true);

// this will not import root namespace classes, like \DateTime or \Exception
$parameters->set(Option::IMPORT_SHORT_CLASSES, false);

// this will not import classes used in PHP DocBlocks, like in /** @var \Some\Class */
$parameters->set(Option::IMPORT_DOC_BLOCKS, false);

// Define your target version which you want to support
$parameters->set(Option::PHP_VERSION_FEATURES, '7.2');

// If you set option Option::AUTO_IMPORT_NAMES to true, you should consider excluding some TYPO3 files.
$parameters->set(Option::EXCLUDE_PATHS, [
'ClassAliasMap.php',
'class.ext_update.php',
'ext_localconf.php',
'ext_emconf.php',
'ext_tables.php',
__DIR__ . '/packages/my_package/Configuration/*'
]);

// If you have trouble that rector cannot run because some TYPO3 constants are not defined add an additional constants file
// Have a look at https://github.com/sabbelasichon/typo3-rector/typo3.constants.php
$parameters->set(Option::AUTOLOAD_PATHS, [
__DIR__ . '/typo3.constants.php'
]);

// get services (needed for register a single rule)
// $services = $containerConfigurator->services();

// register a single rule
// $services->set(InjectAnnotationRector::class);
};

0 comments on commit 6d55943

Please sign in to comment.