Skip to content

Commit

Permalink
TASK: Optimize introspection of old formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
sabbelasichon committed Dec 6, 2022
1 parent 37f4fed commit 011c30f
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 75 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ indent_size = 4
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[tabs.txt]
indent_style = tab
2 changes: 0 additions & 2 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,4 @@
]]);

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

$parameters->set(Typo3Option::TYPOSCRIPT_INDENT_SIZE, 4);
};
20 changes: 10 additions & 10 deletions src/FileProcessor/TypoScript/TypoScriptFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Helmich\TypoScriptParser\Parser\Traverser\Traverser;
use Helmich\TypoScriptParser\Parser\Traverser\Visitor;
use Helmich\TypoScriptParser\Tokenizer\TokenizerException;
use Nette\Utils\Strings;
use Rector\ChangesReporting\ValueObjectFactory\FileDiffFactory;
use Rector\Core\Application\FileSystem\RemovedAndAddedFilesCollector;
use Rector\Core\Configuration\Parameter\ParameterProvider;
Expand All @@ -32,6 +31,7 @@
use Ssch\TYPO3Rector\Contract\Processor\ConfigurableProcessorInterface;
use Ssch\TYPO3Rector\FileProcessor\TypoScript\Collector\RemoveTypoScriptStatementCollector;
use Ssch\TYPO3Rector\FileProcessor\TypoScript\Rector\AbstractTypoScriptRector;
use Ssch\TYPO3Rector\ValueObject\Indent;
use Symfony\Component\Console\Output\BufferedOutput;
use Symplify\SmartFileSystem\SmartFileInfo;
use Webmozart\Assert\Assert;
Expand Down Expand Up @@ -209,20 +209,20 @@ private function processFile(File $file): void
return;
}

// keep original json format
$tabMatches = Strings::match($file->getFileContent(), "#^\n#");
$indentStyle = $tabMatches ? 'tab' : 'space';
// keep original TypoScript format
$indent = Indent::fromFile($file);

$prettyPrinterConfiguration = PrettyPrinterConfiguration::create();
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withEmptyLineBreaks();

if ('tab' === $indentStyle) {
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withTabs();
} else {
if ($indent->isSpace()) {
// default indent
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withSpaceIndentation(
$this->parameterProvider->provideParameter(Typo3Option::TYPOSCRIPT_INDENT_SIZE)
);
$indentation = $this->parameterProvider->provideParameter(
Typo3Option::TYPOSCRIPT_INDENT_SIZE
) ?? $indent->length();
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withSpaceIndentation($indentation);
} else {
$prettyPrinterConfiguration = $prettyPrinterConfiguration->withTabs();
}

$prettyPrinterConfiguration = $prettyPrinterConfiguration->withClosingGlobalStatement();
Expand Down
15 changes: 4 additions & 11 deletions src/FileProcessor/Yaml/Form/FormYamlFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Rector\Core\ValueObject\Reporting\FileDiff;
use Rector\Parallel\ValueObject\Bridge;
use Ssch\TYPO3Rector\Contract\FileProcessor\Yaml\Form\FormYamlRectorInterface;
use Ssch\TYPO3Rector\FileProcessor\Yaml\YamlIndentResolver;
use Ssch\TYPO3Rector\ValueObject\Indent;
use Symfony\Component\Yaml\Yaml;
use Symplify\SmartFileSystem\SmartFileInfo;

Expand All @@ -34,11 +34,6 @@ final class FormYamlFileProcessor implements FileProcessorInterface
*/
private FileDiffFactory $fileDiffFactory;

/**
* @readonly
*/
private YamlIndentResolver $yamlIndentResolver;

/**
* @var FormYamlRectorInterface[]
* @readonly
Expand All @@ -51,12 +46,10 @@ final class FormYamlFileProcessor implements FileProcessorInterface
public function __construct(
CurrentFileProvider $currentFileProvider,
FileDiffFactory $fileDiffFactory,
YamlIndentResolver $yamlIndentResolver,
array $transformer
) {
$this->currentFileProvider = $currentFileProvider;
$this->fileDiffFactory = $fileDiffFactory;
$this->yamlIndentResolver = $yamlIndentResolver;
$this->transformer = $transformer;
}

Expand All @@ -72,6 +65,8 @@ public function process(File $file, Configuration $configuration): array

$this->currentFileProvider->setFile($file);

$indent = Indent::fromFile($file);

$smartFileInfo = new SmartFileInfo($file->getFilePath());
$oldYamlContent = $smartFileInfo->getContents();
$yaml = Yaml::parse($oldYamlContent, Yaml::PARSE_CUSTOM_TAGS);
Expand All @@ -91,9 +86,7 @@ public function process(File $file, Configuration $configuration): array
return $systemErrorsAndFileDiffs;
}

$spaceCount = $this->yamlIndentResolver->resolveIndentSpaceCount($oldYamlContent);

$newFileContent = Yaml::dump($newYaml, 99, $spaceCount);
$newFileContent = Yaml::dump($newYaml, 99, $indent->length());
$file->changeFileContent($newFileContent);

$fileDiff = $this->fileDiffFactory->createFileDiff($file, $oldYamlContent, $newFileContent);
Expand Down
38 changes: 0 additions & 38 deletions src/FileProcessor/Yaml/YamlIndentResolver.php

This file was deleted.

67 changes: 67 additions & 0 deletions src/ValueObject/Indent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\ValueObject;

use Rector\Core\ValueObject\Application\File;
use Webmozart\Assert\Assert;

/**
* @see https://github.com/ergebnis/json-normalizer/blob/main/src/Format/Indent.php
*/
final class Indent
{
public const CHARACTERS = [
'space' => ' ',
'tab' => "\t",
];

private string $value;

private function __construct(string $value)
{
$this->value = $value;
}

public static function fromFile(File $file): self
{
if (1 === \preg_match('/^(?P<indent>( +|\t+)).*/m', $file->getFileContent(), $match)) {
return self::fromString($match['indent']);
}

return self::fromSizeAndStyle(4, 'space',);
}

public function toString(): string
{
return $this->value;
}

public function isSpace(): bool
{
return 1 === \preg_match('/^( +).*/', $this->value);
}

public function length(): int
{
return strlen($this->value);
}

private static function fromSizeAndStyle(int $size, string $style): self
{
Assert::greaterThanEq($size, 1);
Assert::keyExists(self::CHARACTERS, $style);

$value = \str_repeat(self::CHARACTERS[$style], $size);

return new self($value);
}

private static function fromString(string $value): self
{
Assert::regex($value, '/^( *|\t+)$/');

return new self($value);
}
}
2 changes: 0 additions & 2 deletions tests/FileProcessor/TypoScript/config/configured_rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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 @@ -48,7 +47,6 @@
$services->set(VersionConditionMatcher::class);

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

$rectorConfig->rule(AdditionalHeadersToArrayTypoScriptRector::class);
$rectorConfig->rule(LibFluidContentToLibContentElementRector::class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
config.metaCharset = utf-8

config {
metaCharset = utf-8
dummy = 1
metaCharset = utf-8
dummy = 1
}

config.foo = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ mod.web_layout.disableNewContentElementWizard = 1
mod.newContentElementWizard.override = 1

mod {
web_layout.disableNewContentElementWizard = 0
newContentElementWizard.override = 0
dummy = 1
web_layout.disableNewContentElementWizard = 0
newContentElementWizard.override = 0
dummy = 1
}

config.foo = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
config.sendCacheHeaders_onlyWhenLoginDeniedInBranch = 1

config {
sendCacheHeaders_onlyWhenLoginDeniedInBranch = 0
dummy = 1
sendCacheHeaders_onlyWhenLoginDeniedInBranch = 0
dummy = 1
}

config.foo = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ options.workspaces.changeStageMode = any
options.workspaces.changeStageMode = page

options {
workspaces.swapMode = any
workspaces.swapMode = page
workspaces.changeStageMode = any
workspaces.changeStageMode = page
dummy = 1
workspaces.swapMode = any
workspaces.swapMode = page
workspaces.changeStageMode = any
workspaces.changeStageMode = page
dummy = 1
}

config.foo = true
Expand Down
10 changes: 10 additions & 0 deletions tests/ValueObject/Fixtures/file-with-spaces.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

plugin.tx_news {
view {
templateRootPaths {
0 = EXT:news/Resources/Private/Templates/
1 = EXT:news/Resources/Private/Templates/Styles/Twb/Templates
2 = {$plugin.tx_news.view.twb.templateRootPath}
}
}
}
10 changes: 10 additions & 0 deletions tests/ValueObject/Fixtures/tabs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

plugin.tx_news {
view {
templateRootPaths {
0 = EXT:news/Resources/Private/Templates/
1 = EXT:news/Resources/Private/Templates/Styles/Twb/Templates
2 = {$plugin.tx_news.view.twb.templateRootPath}
}
}
}
64 changes: 64 additions & 0 deletions tests/ValueObject/IndentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Tests\ValueObject;

use PHPUnit\Framework\TestCase;
use Rector\Core\ValueObject\Application\File;
use Ssch\TYPO3Rector\ValueObject\Indent;

final class IndentTest extends TestCase
{
/**
* @dataProvider provideValidFiles
*/
public function testFromFile(string $expected, File $file): void
{
$indent = Indent::fromFile($file);
self::assertSame($expected, $indent->toString());
}

public function testIsSpaceReturnsTrue(): void
{
self::assertTrue(Indent::fromFile($this->fileWithSpaces())->isSpace());
}

public function testLengthReturnsCorrectValue(): void
{
self::assertSame(2, Indent::fromFile($this->fileWithSpaces())->length());
}

public function testIsSpaceReturnsFalse(): void
{
self::assertFalse(Indent::fromFile($this->fileWithTabs())->isSpace());
}

/**
* @return \Generator<array<string>>
*/
public function provideValidStringValues(): \Generator
{
yield 'Tabs' => ["\t", "\t"];
yield 'Spaces' => [' ', ' '];
}

/**
* @return \Generator<array<int, File|string>>
*/
public function provideValidFiles(): \Generator
{
yield 'File with tab content' => ["\t", $this->fileWithTabs()];
yield 'File with two spaces content' => [' ', $this->fileWithSpaces()];
}

public function fileWithSpaces(): File
{
return new File('foobar.txt', (string) file_get_contents(__DIR__ . '/Fixtures/file-with-spaces.txt'));
}

public function fileWithTabs(): File
{
return new File('foobar.txt', (string) file_get_contents(__DIR__ . '/Fixtures/tabs.txt'));
}
}

0 comments on commit 011c30f

Please sign in to comment.