Skip to content

Commit

Permalink
TASK: Add paramter to define number of spaces in TS files (#3208)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabbelasichon authored Nov 8, 2022
1 parent c8ea130 commit 2ae5497
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
3 changes: 3 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
$services->defaults()
->public()
->autowire();
$parameters = $rectorConfig->parameters();

$services->set(Filesystem::class);

Expand Down Expand Up @@ -80,4 +81,6 @@
]]);

$services->set(\PhpParser\PrettyPrinter\Standard::class);

$parameters->set(Typo3Option::TYPOSCRIPT_INDENT_SIZE, 4);
};
5 changes: 5 additions & 0 deletions src/Configuration/Typo3Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ final class Typo3Option
* @var string
*/
public const PHPSTAN_FOR_RECTOR_PATH = __DIR__ . '/../../utils/phpstan/config/extension.neon';

/**
* @var string
*/
public const TYPOSCRIPT_INDENT_SIZE = 'typoscript-indent-size';
}
14 changes: 11 additions & 3 deletions src/FileProcessor/TypoScript/TypoScriptFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Nette\Utils\Strings;
use Rector\ChangesReporting\ValueObjectFactory\FileDiffFactory;
use Rector\Core\Application\FileSystem\RemovedAndAddedFilesCollector;
use Rector\Core\Configuration\Parameter\ParameterProvider;
use Rector\Core\Console\Output\RectorOutputStyle;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\ValueObject\Application\File;
Expand All @@ -24,6 +25,7 @@
use Rector\Core\ValueObject\Reporting\FileDiff;
use Rector\FileSystemRector\ValueObject\AddedFileWithContent;
use Rector\Parallel\ValueObject\Bridge;
use Ssch\TYPO3Rector\Configuration\Typo3Option;
use Ssch\TYPO3Rector\Contract\FileProcessor\TypoScript\ConvertToPhpFileInterface;
use Ssch\TYPO3Rector\Contract\FileProcessor\TypoScript\TypoScriptPostRectorInterface;
use Ssch\TYPO3Rector\Contract\FileProcessor\TypoScript\TypoScriptRectorInterface;
Expand Down Expand Up @@ -98,13 +100,15 @@ final class TypoScriptFileProcessor implements ConfigurableProcessorInterface
* @var TypoScriptRectorInterface[]
* @readonly
*/
private array $typoScriptRectors = [];
private array $typoScriptRectors;

/**
* @var TypoScriptPostRectorInterface[]
* @readonly
*/
private array $typoScriptPostRectors = [];
private array $typoScriptPostRectors;

private ParameterProvider $parameterProvider;

/**
* @param TypoScriptRectorInterface[] $typoScriptRectors
Expand All @@ -119,6 +123,7 @@ public function __construct(
RectorOutputStyle $rectorOutputStyle,
FileDiffFactory $fileDiffFactory,
RemoveTypoScriptStatementCollector $removeTypoScriptStatementCollector,
ParameterProvider $parameterProvider,
array $typoScriptRectors = [],
array $typoScriptPostRectors = []
) {
Expand All @@ -132,6 +137,7 @@ public function __construct(
$this->removeTypoScriptStatementCollector = $removeTypoScriptStatementCollector;
$this->typoScriptRectors = $typoScriptRectors;
$this->typoScriptPostRectors = $typoScriptPostRectors;
$this->parameterProvider = $parameterProvider;
}

public function supports(File $file, Configuration $configuration): bool
Expand Down Expand Up @@ -214,7 +220,9 @@ private function processFile(File $file): void
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withTabs();
} else {
// default indent
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withSpaceIndentation(4);
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withSpaceIndentation(
$this->parameterProvider->provideParameter(Typo3Option::TYPOSCRIPT_INDENT_SIZE)
);
}

$prettyPrinterConfiguration = $prettyPrinterConfiguration->withClosingGlobalStatement();
Expand Down
4 changes: 4 additions & 0 deletions templates/rector.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ use Ssch\TYPO3Rector\Set\Typo3LevelSetList;

return static function (RectorConfig $rectorConfig): void {

// If you want to override the number of spaces for your typoscript files you can define it here, the default value is 4
// $parameters = $rectorConfig->parameters();
// $parameters->set(Typo3Option::TYPOSCRIPT_INDENT_SIZE, 2);

$rectorConfig->sets([
Typo3LevelSetList::UP_TO_TYPO3_11,
]);
Expand Down
4 changes: 4 additions & 0 deletions tests/FileProcessor/TypoScript/config/configured_rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Ssch\TYPO3Rector\Configuration\Typo3Option;
use Ssch\TYPO3Rector\FileProcessor\TypoScript\Conditions\ApplicationContextConditionMatcher;
use Ssch\TYPO3Rector\FileProcessor\TypoScript\Conditions\BrowserConditionMatcher;
use Ssch\TYPO3Rector\FileProcessor\TypoScript\Conditions\CompatVersionConditionMatcher;
Expand Down Expand Up @@ -46,6 +47,9 @@
$services->set(UsergroupConditionMatcherMatcher::class);
$services->set(VersionConditionMatcher::class);

$parameters = $rectorConfig->parameters();
$parameters->set(Typo3Option::TYPOSCRIPT_INDENT_SIZE, 4);

$rectorConfig->rule(AdditionalHeadersToArrayTypoScriptRector::class);
$rectorConfig->rule(LibFluidContentToLibContentElementRector::class);
$rectorConfig->rule(LibFluidContentToContentElementTypoScriptPostRector::class);
Expand Down

0 comments on commit 2ae5497

Please sign in to comment.