[DX] Remove non-PHP file formatting based on editorconfig, rather let external coding standard tools handle the file format (#2378)

This commit is contained in:
Tomas Votruba 2022-05-27 18:40:49 +02:00 committed by GitHub
parent 30b5b53c0e
commit 37df55a2fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
39 changed files with 0 additions and 1190 deletions

View File

@ -14,9 +14,6 @@
"composer/semver": "^3.3",
"composer/xdebug-handler": "^3.0",
"doctrine/inflector": "^2.0",
"ergebnis/json-printer": "^3.2",
"helmich/typo3-typoscript-parser": "^2.4.1",
"idiosyncratic/editorconfig": "^0.1.3",
"myclabs/php-enum": "^1.8",
"nette/utils": "^3.2.7",
"nikic/php-parser": "^4.13.2",
@ -24,7 +21,6 @@
"phpstan/phpdoc-parser": "^1.5.1",
"phpstan/phpstan": "^1.7.1",
"phpstan/phpstan-phpunit": "^1.0",
"psr/log": "^2.0",
"react/child-process": "^0.6.4",
"react/event-loop": "^1.3",
"react/socket": "^1.11",

View File

@ -5,9 +5,6 @@ declare(strict_types=1);
use Composer\Semver\VersionParser;
use Doctrine\Inflector\Inflector;
use Doctrine\Inflector\Rules\English\InflectorFactory;
use Ergebnis\Json\Printer\Printer;
use Ergebnis\Json\Printer\PrinterInterface;
use Idiosyncratic\EditorConfig\EditorConfig;
use OndraM\CiDetector\CiDetector;
use PhpParser\BuilderFactory;
use PhpParser\Lexer;
@ -217,9 +214,4 @@ return static function (RectorConfig $rectorConfig): void {
$services->set(DynamicSourceLocatorProvider::class)
->factory([service(PHPStanServicesFactory::class), 'createDynamicSourceLocatorProvider']);
$services->set(EditorConfig::class);
$services->set(Printer::class);
$services->alias(PrinterInterface::class, Printer::class);
};

View File

@ -12,7 +12,6 @@ use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\NodeManipulator\MethodCallManipulator;
use Rector\Defluent\NodeAnalyzer\SameClassMethodCallAnalyzer;
use Rector\DependencyInjection\NodeManipulator\PropertyConstructorInjectionManipulator;
use Rector\FileFormatter\Contract\Formatter\FileFormatterInterface;
use Rector\FileSystemRector\Parser\FileInfoParser;
use Rector\Naming\Contract\AssignVariableNameResolverInterface;
use Rector\Naming\Contract\Guard\ConflictingNameGuardInterface;
@ -64,7 +63,6 @@ return static function (ContainerConfigurator $containerConfigurator): void {
AbstractTestCase::class,
PHPStanServicesFactory::class,
OutputStyleInterface::class,
FileFormatterInterface::class,
MethodCallManipulator::class,
AssignVariableNameResolverInterface::class,
// fix later - rector-symfony

View File

@ -1,3 +0,0 @@
[composer.json]
indent_size = 1
indent_style = tab

View File

@ -1,41 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\FileFormatter\EditorConfig\EditorConfigParser;
use Rector\Core\ValueObject\Application\File;
use Rector\FileFormatter\EditorConfig\EditorConfigParser;
use Rector\FileFormatter\ValueObject\Indent;
use Rector\FileFormatter\ValueObjectFactory\EditorConfigConfigurationBuilder;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class EditorConfigParserTest extends AbstractTestCase
{
private EditorConfigParser $editorConfigParser;
protected function setUp(): void
{
$this->boot();
$this->editorConfigParser = $this->getService(EditorConfigParser::class);
}
public function testComposerJsonFile(): void
{
$editorConfigConfigurationBuilder = new EditorConfigConfigurationBuilder();
$editorConfigConfigurationBuilder->withIndent(Indent::createSpaceWithSize(20));
$composerJsonFile = new SmartFileInfo(__DIR__ . '/Fixture/composer.json');
$file = new File($composerJsonFile, $composerJsonFile->getContents());
$editorConfigConfiguration = $this->editorConfigParser->extractConfigurationForFile(
$file,
$editorConfigConfigurationBuilder
);
$this->assertSame('tab', $editorConfigConfiguration->getIndentStyle());
$this->assertSame(1, $editorConfigConfiguration->getIndentSize());
}
}

View File

@ -1,5 +0,0 @@
{
"name": "foo/bar",
"description": "A foo bar baz extension",
"license": "GPL-2.0-or-later"
}

View File

@ -1,11 +0,0 @@
{
"name": "foo/bar",
"description": "A foo bar baz extension",
"license": "GPL-2.0-or-later"
}
-----
{
"name": "foo/bar",
"description": "A foo bar baz extension",
"license": "GPL-2.0-or-later"
}

View File

@ -1,57 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\FileFormatter\Formatter\JsonFileFormatter;
use Iterator;
use Rector\Core\ValueObject\Application\File;
use Rector\FileFormatter\Formatter\JsonFileFormatter;
use Rector\FileFormatter\ValueObject\Indent;
use Rector\FileFormatter\ValueObjectFactory\EditorConfigConfigurationBuilder;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder;
use Symplify\EasyTesting\StaticFixtureSplitter;
use Symplify\SmartFileSystem\SmartFileInfo;
final class JsonFileFormatterTest extends AbstractTestCase
{
private JsonFileFormatter $jsonFileFormatter;
protected function setUp(): void
{
$this->boot();
$this->jsonFileFormatter = $this->getService(JsonFileFormatter::class);
}
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<array<int, SmartFileInfo>>
*/
public function provideData(): Iterator
{
return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture', '*.json');
}
private function doTestFileInfo(SmartFileInfo $smartFileInfo): void
{
$inputFileInfoAndExpected = StaticFixtureSplitter::splitFileInfoToLocalInputAndExpected($smartFileInfo);
$inputFileInfo = $inputFileInfoAndExpected->getInputFileInfo();
$file = new File($inputFileInfo, $inputFileInfo->getContents());
$editorConfigConfigurationBuilder = new EditorConfigConfigurationBuilder();
$editorConfigConfigurationBuilder->withIndent(Indent::createTab());
$this->jsonFileFormatter->format($file, $editorConfigConfigurationBuilder->build());
$this->assertSame($inputFileInfoAndExpected->getExpected(), $file->getFileContent());
}
}

View File

@ -1,25 +0,0 @@
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
</catalog>
-----
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
</catalog>

View File

@ -1,9 +0,0 @@
services:
Vendor\Namespace\MyService:
arguments: [ !tagged_iterator { tag: 'namespace_mytag' } ]
-----
services:
Vendor\Namespace\MyService:
arguments:
- !tagged_iterator
tag: namespace_mytag

View File

@ -1,15 +0,0 @@
martin:
name: Martin
job: Developer
skills:
- python
- perl
- pascal
-----
martin:
name: Martin
job: Developer
skills:
- python
- perl
- pascal

View File

@ -1,51 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\FileFormatter\ValueObject;
use PHPUnit\Framework\TestCase;
use Rector\FileFormatter\ValueObject\Indent;
use Rector\FileFormatter\ValueObjectFactory\EditorConfigConfigurationBuilder;
use Symplify\PackageBuilder\Configuration\StaticEolConfiguration;
final class EditorConfigConfigurationTest extends TestCase
{
public function testWithFinalNewline(): void
{
$editorConfigConfigurationBuilder = new EditorConfigConfigurationBuilder();
$editorConfigConfiguration = $editorConfigConfigurationBuilder->build();
$this->assertSame(StaticEolConfiguration::getEolChar(), $editorConfigConfiguration->getFinalNewline());
}
public function testWithoutFinalNewline(): void
{
$editorConfigConfigurationBuilder = new EditorConfigConfigurationBuilder();
$editorConfigConfigurationBuilder->withInsertFinalNewline(false);
$editorConfigConfiguration = $editorConfigConfigurationBuilder->build();
$this->assertSame('', $editorConfigConfiguration->getFinalNewline());
}
public function testIndentForTab(): void
{
$editorConfigConfigurationBuilder = new EditorConfigConfigurationBuilder();
$editorConfigConfigurationBuilder->withIndent(Indent::createTab());
$editorConfigConfiguration = $editorConfigConfigurationBuilder->build();
$this->assertSame(' ', $editorConfigConfiguration->getIndent());
}
public function testIndentForSpace(): void
{
$editorConfigConfigurationBuilder = new EditorConfigConfigurationBuilder();
$editorConfigConfigurationBuilder->withIndent(Indent::createSpaceWithSize(10));
$editorConfigConfiguration = $editorConfigConfigurationBuilder->build();
$this->assertSame(' ', $editorConfigConfiguration->getIndent());
}
}

View File

@ -1 +0,0 @@
{ "name": "foo/bar", "description": "A foo bar baz extension", "license": "GPL-2.0-or-later" }

View File

@ -1,5 +0,0 @@
{
"name": "foo/bar",
"description": "A foo bar baz extension",
"license": "GPL-2.0-or-later"
}

View File

@ -1,5 +0,0 @@
{
"name": "foo/bar",
"description": "A foo bar baz extension",
"license": "GPL-2.0-or-later"
}

View File

@ -1,5 +0,0 @@
{
"name": "foo/bar",
"description": "A foo bar baz extension",
"license": "GPL-2.0-or-later"
}

View File

@ -1,5 +0,0 @@
{
"name": "foo/bar",
"description": "A foo bar baz extension",
"license": "GPL-2.0-or-later"
}

View File

@ -1,12 +0,0 @@
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
</catalog>

View File

@ -1 +0,0 @@
martin: name: Martin job: Developer skills: - python - perl - pascal

View File

@ -1,7 +0,0 @@
martin:
name: Martin
job: Developer
skills:
- python
- perl
- pascal

View File

@ -1,7 +0,0 @@
martin:
name: Martin
job: Developer
skills:
- python
- perl
- pascal

View File

@ -1,107 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\FileFormatter\ValueObject;
use Generator;
use Iterator;
use PHPUnit\Framework\TestCase;
use Rector\FileFormatter\Exception\InvalidIndentSizeException;
use Rector\FileFormatter\Exception\InvalidIndentStyleException;
use Rector\FileFormatter\Exception\ParseIndentException;
use Rector\FileFormatter\ValueObject\Indent;
use Symplify\SmartFileSystem\SmartFileInfo;
final class IndentTest extends TestCase
{
/**
* @dataProvider extractFromFiles
*/
public function testFromFiles(SmartFileInfo $smartFileInfo, string $expectedIndent): void
{
$indent = Indent::fromContent($smartFileInfo->getContents());
$this->assertSame($expectedIndent, $indent->__toString());
}
/**
* @dataProvider provideSizeStyleAndIndentString
*/
public function testFromSizeAndStyle(int $size, string $style, string $string): void
{
$indent = Indent::fromSizeAndStyle($size, $style);
$this->assertSame($string, $indent->__toString());
$this->assertSame($size, $indent->getIndentSize());
$this->assertSame($style, $indent->getIndentStyle());
}
public function testFromSizeAndStyleWithInvalidSizeThrowsException(): void
{
$this->expectException(InvalidIndentSizeException::class);
Indent::fromSizeAndStyle(0, 'invalid');
}
public function testFromSizeAndStyleWithInvalidStyleThrowsException(): void
{
$this->expectException(InvalidIndentStyleException::class);
Indent::fromSizeAndStyle(1, 'invalid');
}
public function testFromInvalidContentThrowsException(): void
{
$this->expectException(ParseIndentException::class);
Indent::fromContent('This is invalid content');
}
/**
* @return Iterator<array<string>>
*/
public function extractFromFiles(): Iterator
{
yield 'Yaml file with space indentation of size 4' => [
new SmartFileInfo(__DIR__ . '/Fixture/yaml_indentation_space_four.yaml'),
' ',
];
yield 'Yaml file with space indentation of size 2' => [
new SmartFileInfo(__DIR__ . '/Fixture/yaml_indentation_space_two.yaml'),
' ',
];
yield 'Json file with tab indentation of size 2' => [
new SmartFileInfo(__DIR__ . '/Fixture/composer_indentation_tab_two.json'),
' ',
];
yield 'Json file with space indentation of size 6' => [
new SmartFileInfo(__DIR__ . '/Fixture/composer_indentation_space_six.json'),
' ',
];
}
/**
* @return Generator<array{0: int, 1: string, 2: string}>
*/
public function provideSizeStyleAndIndentString(): Iterator
{
foreach ($this->sizes() as $size) {
foreach (Indent::CHARACTERS as $style => $character) {
$string = str_repeat($character, $size);
yield [$size, $style, $string];
}
}
}
/**
* @return array{int-one: int, int-greater-than-one: int}
*/
private static function sizes(): array
{
return [
'int-one' => 1,
'int-greater-than-one' => 5,
];
}
}

View File

@ -1,112 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\FileFormatter\ValueObject;
use Iterator;
use Rector\FileFormatter\Exception\InvalidNewLineStringException;
use Rector\FileFormatter\ValueObject\NewLine;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class NewLineTest extends AbstractTestCase
{
/**
* @dataProvider extractFromFiles
*/
public function testFromFiles(SmartFileInfo $smartFileInfo, string $expectedNewLine): void
{
$newLine = NewLine::fromContent($smartFileInfo->getContents());
$this->assertSame($expectedNewLine, $newLine->__toString());
}
/**
* @dataProvider provideInvalidNewLineString
*/
public function testFromStringRejectsInvalidNewLineString(string $string): void
{
$this->expectException(InvalidNewLineStringException::class);
NewLine::fromSingleCharacter($string);
}
/**
* @dataProvider provideValidNewLineString
*/
public function testFromStringReturnsNewLine(string $string): void
{
$newLine = NewLine::fromSingleCharacter($string);
$this->assertSame($string, $newLine->__toString());
}
/**
* @dataProvider provideValidNewLineStringFromEditorConfig
*/
public function testFromEditorConfigReturnsNewLine(string $string, string $expected): void
{
$newLine = NewLine::fromEditorConfig($string);
$this->assertSame($expected, $newLine->__toString());
}
/**
* @return Iterator<array<string>>
*/
public function provideValidNewLineString(): Iterator
{
foreach (["\n", "\r", "\r\n"] as $string) {
yield [$string];
}
}
/**
* @return Iterator<array<string>>
*/
public function provideInvalidNewLineString(): Iterator
{
foreach (["\t", " \r ", " \r\n ", " \n ", ' ', "\f", "\x0b", "\x85"] as $string) {
yield [$string];
}
}
/**
* @return Iterator<array<string>>
*/
public function extractFromFiles(): Iterator
{
yield 'Yaml file with carriage return' => [
new SmartFileInfo(__DIR__ . '/Fixture/yaml_carriage_return.yaml'),
"\r",
];
yield 'Xml file with line feed' => [new SmartFileInfo(__DIR__ . '/Fixture/xml_line_feed.xml'), "\n"];
yield 'Json file with line feed' => [new SmartFileInfo(__DIR__ . '/Fixture/composer_line_feed.json'), "\n"];
yield 'Json file with carriage return' => [
new SmartFileInfo(__DIR__ . '/Fixture/composer_carriage_return.json'),
"\r",
];
yield 'Json file with carriage return and line feed' => [
new SmartFileInfo(__DIR__ . '/Fixture/composer_carriage_return_line_feed.json'),
"\r\n",
];
}
/**
* @return Iterator<array<string>>
*/
public function provideValidNewLineStringFromEditorConfig(): Iterator
{
foreach ([
'lf' => "\n",
'cr' => "\r",
'crlf' => "\r\n",
] as $editorConfig => $string) {
yield [$editorConfig, $string];
}
}
}

View File

@ -1,15 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\FileFormatter\Contract\Formatter;
use Rector\Core\ValueObject\Application\File;
use Rector\FileFormatter\ValueObject\EditorConfigConfiguration;
interface FileFormatterInterface
{
public function supports(File $file): bool;
public function format(File $file, EditorConfigConfiguration $editorConfigConfiguration): void;
}

View File

@ -1,59 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\FileFormatter\EditorConfig;
use Idiosyncratic\EditorConfig\Declaration\Declaration;
use Idiosyncratic\EditorConfig\EditorConfig;
use Rector\Core\ValueObject\Application\File;
use Rector\FileFormatter\ValueObject\EditorConfigConfiguration;
use Rector\FileFormatter\ValueObject\EditorConfigOption;
use Rector\FileFormatter\ValueObjectFactory\EditorConfigConfigurationBuilder;
/**
* @see \Rector\Tests\FileFormatter\EditorConfig\EditorConfigParser\EditorConfigParserTest
*/
final class EditorConfigParser
{
public function __construct(
private readonly EditorConfig $editorConfig
) {
}
public function extractConfigurationForFile(
File $file,
EditorConfigConfigurationBuilder $editorConfigConfigurationBuilder
): EditorConfigConfiguration {
$smartFileInfo = $file->getSmartFileInfo();
/** @var Declaration[] $configuration */
$configuration = $this->editorConfig->getConfigForPath($smartFileInfo->getRealPath());
if (array_key_exists(EditorConfigOption::INDENT_STYLE, $configuration)) {
$indentStyle = (string) $configuration[EditorConfigOption::INDENT_STYLE]->getValue();
$editorConfigConfigurationBuilder->withIndentStyle($indentStyle);
}
if (array_key_exists(EditorConfigOption::INDENT_SIZE, $configuration)) {
$indentSize = (int) $configuration[EditorConfigOption::INDENT_SIZE]->getValue();
$editorConfigConfigurationBuilder->withIndentSize($indentSize);
}
if (array_key_exists(EditorConfigOption::END_OF_LINE, $configuration)) {
$endOfLine = (string) $configuration[EditorConfigOption::END_OF_LINE]->getValue();
$editorConfigConfigurationBuilder->withEndOfLineFromEditorConfig($endOfLine);
}
if (array_key_exists(EditorConfigOption::INSERT_FINAL_NEWLINE, $configuration)) {
$insertFinalNewline = (bool) $configuration[EditorConfigOption::INSERT_FINAL_NEWLINE]->getValue();
$editorConfigConfigurationBuilder->withInsertFinalNewline($insertFinalNewline);
}
return $editorConfigConfigurationBuilder->build();
}
}

View File

@ -1,18 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\FileFormatter\Enum;
final class IndentType
{
/**
* @var string
*/
public const SPACE = 'space';
/**
* @var string
*/
public const TAB = 'tab';
}

View File

@ -1,16 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\FileFormatter\Exception;
use InvalidArgumentException;
final class InvalidIndentSizeException extends InvalidArgumentException
{
public function __construct(int $size, int $minimumSize)
{
$message = sprintf('Size %d must be greater than %d', $size, $minimumSize);
parent::__construct($message);
}
}

View File

@ -1,16 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\FileFormatter\Exception;
use InvalidArgumentException;
final class InvalidIndentStringException extends InvalidArgumentException
{
public function __construct(string $value)
{
$message = sprintf('The "%s" is not valid indentation', $value);
parent::__construct($message);
}
}

View File

@ -1,19 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\FileFormatter\Exception;
use InvalidArgumentException;
use Rector\FileFormatter\Enum\IndentType;
final class InvalidIndentStyleException extends InvalidArgumentException
{
public function __construct(string $style)
{
$allowedIndentTypesString = implode('", "', [IndentType::SPACE, IndentType::TAB]);
$message = sprintf('Given style "%s" is not allowed. Allowed are "%s"', $style, $allowedIndentTypesString);
parent::__construct($message);
}
}

View File

@ -1,20 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\FileFormatter\Exception;
use InvalidArgumentException;
final class InvalidNewLineStringException extends InvalidArgumentException
{
public static function fromString(string $string): self
{
return new self(sprintf('"%s" is not a valid new-line character sequence.', $string));
}
public static function create(string $message): self
{
return new self($message);
}
}

View File

@ -1,16 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\FileFormatter\Exception;
use UnexpectedValueException;
final class ParseIndentException extends UnexpectedValueException
{
public function __construct(string $string)
{
$message = sprintf('The content "%s" could not be parsed', $string);
parent::__construct($message);
}
}

View File

@ -1,83 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\FileFormatter;
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\Application\File;
use Rector\FileFormatter\Contract\Formatter\FileFormatterInterface;
use Rector\FileFormatter\EditorConfig\EditorConfigParser;
use Rector\FileFormatter\Exception\InvalidNewLineStringException;
use Rector\FileFormatter\Exception\ParseIndentException;
use Rector\FileFormatter\ValueObject\EditorConfigConfiguration;
use Rector\FileFormatter\ValueObject\Indent;
use Rector\FileFormatter\ValueObject\NewLine;
use Rector\FileFormatter\ValueObjectFactory\EditorConfigConfigurationBuilder;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
final class FileFormatter
{
/**
* @param FileFormatterInterface[] $fileFormatters
*/
public function __construct(
private readonly EditorConfigParser $editorConfigParser,
private readonly ParameterProvider $parameterProvider,
private readonly array $fileFormatters = []
) {
}
/**
* @param File[] $files
*/
public function format(array $files): void
{
foreach ($files as $file) {
if (! $file->hasChanged()) {
continue;
}
foreach ($this->fileFormatters as $fileFormatter) {
if (! $fileFormatter->supports($file)) {
continue;
}
$editorConfigConfigurationBuilder = new EditorConfigConfigurationBuilder();
$this->sniffOriginalFileContent($file, $editorConfigConfigurationBuilder);
$editorConfiguration = $this->createEditorConfiguration($file, $editorConfigConfigurationBuilder);
$fileFormatter->format($file, $editorConfiguration);
}
}
}
private function sniffOriginalFileContent(
File $file,
EditorConfigConfigurationBuilder $editorConfigConfigurationBuilder
): void {
// Try to sniff into the original content to get the indentation and new line
try {
$indent = Indent::fromContent($file->getOriginalFileContent());
$editorConfigConfigurationBuilder->withIndent($indent);
} catch (ParseIndentException) {
}
try {
$newLine = NewLine::fromContent($file->getOriginalFileContent());
$editorConfigConfigurationBuilder->withNewLine($newLine);
} catch (InvalidNewLineStringException) {
}
}
private function createEditorConfiguration(
File $file,
EditorConfigConfigurationBuilder $editorConfigConfigurationBuilder
): EditorConfigConfiguration {
if (! $this->parameterProvider->provideBoolParameter(Option::ENABLE_EDITORCONFIG)) {
return $editorConfigConfigurationBuilder->build();
}
return $this->editorConfigParser->extractConfigurationForFile($file, $editorConfigConfigurationBuilder);
}
}

View File

@ -1,41 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\FileFormatter\Formatter;
use Ergebnis\Json\Printer\PrinterInterface;
use Rector\Core\ValueObject\Application\File;
use Rector\FileFormatter\Contract\Formatter\FileFormatterInterface;
use Rector\FileFormatter\ValueObject\EditorConfigConfiguration;
/**
* @see \Rector\Tests\FileFormatter\Formatter\JsonFileFormatter\JsonFileFormatterTest
*/
final class JsonFileFormatter implements FileFormatterInterface
{
public function __construct(
private readonly PrinterInterface $jsonPrinter
) {
}
public function supports(File $file): bool
{
$smartFileInfo = $file->getSmartFileInfo();
return $smartFileInfo->getExtension() === 'json';
}
public function format(File $file, EditorConfigConfiguration $editorConfigConfiguration): void
{
$newFileContent = $this->jsonPrinter->print(
$file->getFileContent(),
$editorConfigConfiguration->getIndent(),
$editorConfigConfiguration->getNewLine()
);
$newFileContent .= $editorConfigConfiguration->getFinalNewline();
$file->changeFileContent($newFileContent);
}
}

View File

@ -1,48 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\FileFormatter\ValueObject;
/**
* @see \Rector\Tests\FileFormatter\ValueObject\EditorConfigConfigurationTest
*/
final class EditorConfigConfiguration
{
public function __construct(
private readonly Indent $indent,
private readonly NewLine $newLine,
private readonly bool $insertFinalNewline
) {
}
public function getNewLine(): string
{
return $this->newLine->__toString();
}
public function getFinalNewline(): string
{
return $this->insertFinalNewline ? $this->getNewLine() : '';
}
public function getIndent(): string
{
return $this->indent->__toString();
}
public function getIndentStyleCharacter(): string
{
return $this->indent->getIndentStyleCharacter();
}
public function getIndentStyle(): string
{
return $this->indent->getIndentStyle();
}
public function getIndentSize(): int
{
return $this->indent->getIndentSize();
}
}

View File

@ -1,33 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\FileFormatter\ValueObject;
final class EditorConfigOption
{
/**
* @var string
*/
public const INDENT_STYLE = 'indent_style';
/**
* @var string
*/
public const INDENT_SIZE = 'indent_size';
/**
* @var string
*/
public const END_OF_LINE = 'end_of_line';
/**
* @var string
*/
public const INSERT_FINAL_NEWLINE = 'insert_final_newline';
/**
* @var string
*/
public const TAB_WIDTH = 'tab_width';
}

View File

@ -1,125 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\FileFormatter\ValueObject;
use Nette\Utils\Strings;
use Rector\FileFormatter\Enum\IndentType;
use Rector\FileFormatter\Exception\InvalidIndentSizeException;
use Rector\FileFormatter\Exception\InvalidIndentStringException;
use Rector\FileFormatter\Exception\InvalidIndentStyleException;
use Rector\FileFormatter\Exception\ParseIndentException;
use Stringable;
/**
* @see \Rector\Tests\FileFormatter\ValueObject\IndentTest
*/
final class Indent implements Stringable
{
/**
* @var array<string, string>
*/
public const CHARACTERS = [
IndentType::SPACE => ' ',
IndentType::TAB => "\t",
];
/**
* @see https://regex101.com/r/A2XiaF/1
* @var string
*/
private const VALID_INDENT_REGEX = '#^( *|\t+)$#';
/**
* @var int
*/
private const MINIMUM_SIZE = 1;
/**
* @see https://regex101.com/r/3HFEjX/1
* @var string
*/
private const PARSE_INDENT_REGEX = '/^(?P<indent>( +|\t+)).*/m';
private function __construct(
private readonly string $string
) {
}
public function __toString(): string
{
return $this->string;
}
public static function fromString(string $content): self
{
$match = Strings::match($content, self::VALID_INDENT_REGEX);
if ($match === null) {
throw new InvalidIndentStringException($content);
}
return new self($content);
}
public static function createSpaceWithSize(int $size): self
{
return self::fromSizeAndStyle($size, IndentType::SPACE);
}
public static function createTab(): self
{
return self::fromSizeAndStyle(1, IndentType::TAB);
}
/**
* @param IndentType::* $style
*/
public static function fromSizeAndStyle(int $size, string $style): self
{
if ($size < self::MINIMUM_SIZE) {
throw new InvalidIndentSizeException($size, self::MINIMUM_SIZE);
}
if (! array_key_exists($style, self::CHARACTERS)) {
throw new InvalidIndentStyleException($style);
}
$value = str_repeat(self::CHARACTERS[$style], $size);
return new self($value);
}
public static function fromContent(string $content): self
{
$match = Strings::match($content, self::PARSE_INDENT_REGEX);
if (isset($match['indent'])) {
return self::fromString($match['indent']);
}
throw new ParseIndentException($content);
}
public function getIndentSize(): int
{
return strlen($this->string);
}
/**
* @return IndentType::*
*/
public function getIndentStyle(): string
{
return $this->startsWithSpace() ? IndentType::SPACE : IndentType::TAB;
}
public function getIndentStyleCharacter(): string
{
return $this->startsWithSpace() ? self::CHARACTERS[IndentType::SPACE] : self::CHARACTERS[IndentType::TAB];
}
private function startsWithSpace(): bool
{
return \str_starts_with($this->string, ' ');
}
}

View File

@ -1,97 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\FileFormatter\ValueObject;
use Nette\Utils\Strings;
use const PHP_EOL;
use Rector\FileFormatter\Exception\InvalidNewLineStringException;
use Stringable;
/**
* @see \Rector\Tests\FileFormatter\ValueObject\NewLineTest
*/
final class NewLine implements Stringable
{
/**
* @var string
*/
public const LINE_FEED = 'lf';
/**
* @var string
*/
public const CARRIAGE_RETURN = 'cr';
/**
* @var string
*/
public const CARRIAGE_RETURN_LINE_FEED = 'crlf';
/**
* @var array<string, string>
*/
private const ALLOWED_END_OF_LINE = [
self::LINE_FEED => "\n",
self::CARRIAGE_RETURN => "\r",
self::CARRIAGE_RETURN_LINE_FEED => "\r\n",
];
/**
* @see https://regex101.com/r/icaBBp/1
* @var string
*/
private const NEWLINE_REGEX = '#(?P<newLine>\r\n|\n|\r)#';
/**
* @see https://regex101.com/r/WrY9ZW/1/
* @var string
*/
private const VALID_NEWLINE_REGEX = '#^(?>\r\n|\n|\r)$#';
private function __construct(
private readonly string $string
) {
}
public function __toString(): string
{
return $this->string;
}
public static function fromSingleCharacter(string $content): self
{
$matches = Strings::match($content, self::VALID_NEWLINE_REGEX);
if ($matches === null) {
throw InvalidNewLineStringException::fromString($content);
}
return new self($content);
}
public static function fromContent(string $content): self
{
$match = Strings::match($content, self::NEWLINE_REGEX);
if (isset($match['newLine'])) {
return self::fromSingleCharacter($match['newLine']);
}
return self::fromSingleCharacter(PHP_EOL);
}
public static function fromEditorConfig(string $endOfLine): self
{
if (! array_key_exists($endOfLine, self::ALLOWED_END_OF_LINE)) {
$allowedEndOfLineValues = array_keys(self::ALLOWED_END_OF_LINE);
$message = sprintf(
'The endOfLine "%s" is not allowed. Allowed are "%s"',
$endOfLine,
implode(',', $allowedEndOfLineValues)
);
throw InvalidNewLineStringException::create($message);
}
return self::fromSingleCharacter(self::ALLOWED_END_OF_LINE[$endOfLine]);
}
}

View File

@ -1,83 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\FileFormatter\ValueObjectFactory;
use Rector\FileFormatter\Enum\IndentType;
use Rector\FileFormatter\ValueObject\EditorConfigConfiguration;
use Rector\FileFormatter\ValueObject\Indent;
use Rector\FileFormatter\ValueObject\NewLine;
final class EditorConfigConfigurationBuilder
{
private NewLine $newLine;
/**
* @param IndentType::* $indentStyle
*/
public function __construct(
private string $indentStyle = IndentType::SPACE,
private int $indentSize = 4,
private bool $insertFinalNewline = true
) {
$this->newLine = NewLine::fromEditorConfig('lf');
}
public function withNewLine(NewLine $newLine): self
{
$this->newLine = $newLine;
return $this;
}
public function withIndent(Indent $indent): self
{
$this->indentSize = $indent->getIndentSize();
$this->indentStyle = $indent->getIndentStyle();
return $this;
}
/**
* @param IndentType::* $indentStyle
*/
public function withIndentStyle(string $indentStyle): self
{
$this->indentStyle = $indentStyle;
return $this;
}
public function withIndentSize(int $indentSize): self
{
$this->indentSize = $indentSize;
return $this;
}
public function withInsertFinalNewline(bool $insertFinalNewline): self
{
$this->insertFinalNewline = $insertFinalNewline;
return $this;
}
public function withEndOfLineFromEditorConfig(string $endOfLine): self
{
$this->newLine = NewLine::fromEditorConfig($endOfLine);
return $this;
}
public function build(): EditorConfigConfiguration
{
$newLine = $this->newLine;
return new EditorConfigConfiguration(
Indent::fromSizeAndStyle($this->indentSize, $this->indentStyle),
$newLine,
$this->insertFinalNewline
);
}
}

View File

@ -15,7 +15,6 @@ use Rector\Core\ValueObject\Configuration;
use Rector\Core\ValueObject\Error\SystemError;
use Rector\Core\ValueObject\Reporting\FileDiff;
use Rector\Core\ValueObjectFactory\Application\FileFactory;
use Rector\FileFormatter\FileFormatter;
use Rector\Parallel\Application\ParallelFileProcessor;
use Rector\Parallel\ValueObject\Bridge;
use Symfony\Component\Console\Input\InputInterface;
@ -47,7 +46,6 @@ final class ApplicationFileProcessor
public function __construct(
private readonly SmartFileSystem $smartFileSystem,
private readonly FileDiffFileDecorator $fileDiffFileDecorator,
private readonly FileFormatter $fileFormatter,
private readonly RemovedAndAddedFilesProcessor $removedAndAddedFilesProcessor,
private readonly OutputStyleInterface $rectorOutputStyle,
private readonly FileFactory $fileFactory,
@ -89,7 +87,6 @@ final class ApplicationFileProcessor
$this->configurePHPStanNodeScopeResolver($files);
$systemErrorsAndFileDiffs = $this->processFiles($files, $configuration);
$this->fileFormatter->format($files);
$this->fileDiffFileDecorator->decorate($files);
$this->printFiles($files, $configuration);