-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TASK: Optimize introspection of old formatting
- Loading branch information
1 parent
37f4fed
commit 011c30f
Showing
14 changed files
with
180 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
tests/Rector/v12/v0/typoscript/RemoveMetaCharSetRector/Fixture/fixture.typoscript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...tor/v12/v0/typoscript/RemoveSendCacheHeadersConfigOptionRector/Fixture/fixture.typoscript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |