Skip to content

Commit

Permalink
[FEATURE][WIP] Add rector
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszuznanski committed Nov 16, 2023
1 parent 0557cc0 commit 769e591
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@
"phpstan/phpstan-phpunit": "^1.3.15",
"phpstan/phpstan-strict-rules": "^1.5.2",
"phpunit/phpunit": "^9.6.13",
"rector/rector": "^0.18.10",
"saschaegerer/phpstan-typo3": "^1.9.0",
"scssphp/scssphp": "^1.12",
"seld/jsonlint": "^1.10.0",
"squizlabs/php_codesniffer": "^3.7.2",
"ssch/typo3-rector": "*",
"symfony/yaml": "^5.3.6 || ^6.2.0",
"tomasvotruba/type-coverage": "^0.2.1",
"typo3/cms-fluid-styled-content": "^11.5.4 || ^12.4.0",
Expand Down
119 changes: 119 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\PostRector\Rector\NameImportingPostRector;
use Ssch\TYPO3Rector\Configuration\Typo3Option;
use Ssch\TYPO3Rector\FileProcessor\Composer\Rector\ExtensionComposerRector;
use Ssch\TYPO3Rector\FileProcessor\TypoScript\Rector\FileIncludeToImportStatementTypoScriptRector;
use Ssch\TYPO3Rector\Rector\v9\v0\InjectAnnotationRector;
use Ssch\TYPO3Rector\Rector\General\ConvertImplicitVariablesToExplicitGlobalsRector;
use Ssch\TYPO3Rector\Rector\General\ExtEmConfRector;
use Ssch\TYPO3Rector\Set\Typo3LevelSetList;
use Ssch\TYPO3Rector\FileProcessor\TypoScript\Rector\ExtbasePersistenceTypoScriptRector;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([
Typo3LevelSetList::UP_TO_TYPO3_11,
]);

// In order to have a better analysis from phpstan we teach it here some more things
$rectorConfig->phpstanConfig(Typo3Option::PHPSTAN_FOR_RECTOR_PATH);

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

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

// this prevents infinite loop issues due to symlinks in e.g. ".Build/" folders within single extensions
$parameters->set(Option::FOLLOW_SYMLINKS, false);

// Define your target version which you want to support
$rectorConfig->phpVersion(PhpVersion::PHP_83);

// If you have an editorconfig and changed files should keep their format enable it here
// $parameters->set(Option::ENABLE_EDITORCONFIG, true);

// If you only want to process one/some TYPO3 extension(s), you can specify its path(s) here.
// If you use the option --config change __DIR__ to getcwd()
// $rectorConfig->paths([
// __DIR__ . '/packages/acme_demo/',
// ]);

// If you use the option --config change __DIR__ to getcwd()
$rectorConfig->skip([
// @see https://github.com/sabbelasichon/typo3-rector/issues/2536
__DIR__ . '/**/Configuration/ExtensionBuilder/*',
// We skip those directories on purpose as there might be node_modules or similar
// that include typescript which would result in false positive processing
__DIR__ . '/**/Resources/**/node_modules/*',
__DIR__ . '/**/Resources/**/NodeModules/*',
__DIR__ . '/**/Resources/**/BowerComponents/*',
__DIR__ . '/**/Resources/**/bower_components/*',
__DIR__ . '/**/Resources/**/build/*',
__DIR__ . '/vendor/*',
__DIR__ . '/Build/*',
__DIR__ . '/public/*',
__DIR__ . '/.github/*',
__DIR__ . '/.Build/*',
]);

// This is used by the class \Ssch\TYPO3Rector\Rector\PostRector\FullQualifiedNamePostRector to force FQN in this paths and files
$parameters->set(Typo3Option::PATHS_FULL_QUALIFIED_NAMESPACES, [
# If you are targeting TYPO3 Version 11 use can now use Short namespace
# @see namespace https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ExtensionArchitecture/ConfigurationFiles/Index.html
'ext_localconf.php',
'ext_tables.php',
'ClassAliasMap.php',
__DIR__ . '/**/Configuration/*.php',
__DIR__ . '/**/Configuration/**/*.php',
]);

// If you have trouble that rector cannot run because some TYPO3 constants are not defined add an additional constants file
// @see https://github.com/sabbelasichon/typo3-rector/blob/master/typo3.constants.php
// @see https://github.com/rectorphp/rector/blob/main/docs/static_reflection_and_autoload.md#include-files
// $parameters->set(Option::BOOTSTRAP_FILES, [
// __DIR__ . '/typo3.constants.php'
// ]);

// register a single rule
// $rectorConfig->rule(InjectAnnotationRector::class);

/**
* Useful rule from RectorPHP itself to transform i.e. GeneralUtility::makeInstance('TYPO3\CMS\Core\Log\LogManager')
* to GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\LogManager::class) calls.
* But be warned, sometimes it produces false positives (edge cases), so watch out
*/
// $rectorConfig->rule(StringClassNameToClassConstantRector::class);

// Optional non-php file functionalities:
// @see https://github.com/sabbelasichon/typo3-rector/blob/main/docs/beyond_php_file_processors.md

// Adapt your composer.json dependencies to the latest available version for the defined SetList
// $rectorConfig->sets([
// Typo3SetList::COMPOSER_PACKAGES_104_CORE
// Typo3SetList::COMPOSER_PACKAGES_104_EXTENSIONS,
// );

// Rewrite your extbase persistence class mapping from typoscript into php according to official docs.
// This processor will create a summarized file with all of the typoscript rewrites combined into a single file.
// The filename can be passed as argument, "Configuration_Extbase_Persistence_Classes.php" is default.
// $rectorConfig->ruleWithConfiguration(ExtbasePersistenceTypoScriptRector::class, []);
// Add some general TYPO3 rules
$rectorConfig->rule(ConvertImplicitVariablesToExplicitGlobalsRector::class);
$rectorConfig->ruleWithConfiguration(ExtEmConfRector::class, [
ExtEmConfRector::ADDITIONAL_VALUES_TO_BE_REMOVED => []
]);
$rectorConfig->ruleWithConfiguration(ExtensionComposerRector::class, [
ExtensionComposerRector::TYPO3_VERSION_CONSTRAINT => ''
]);

// Do you want to modernize your TypoScript include statements for files and move from <INCLUDE /> to @import use the FileIncludeToImportStatementVisitor
// $rectorConfig->rule(FileIncludeToImportStatementTypoScriptRector::class);
};

0 comments on commit 769e591

Please sign in to comment.