Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Take .editorconfig into account #6272

Merged
merged 7 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ phpstan-for-rector.neon export-ignore

# testing Windows spaces - https://help.github.com/en/github/using-git/configuring-git-to-handle-line-endings
packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfoPrinter/FixtureBasic/with_spac.txt text eol=crlf
packages-tests/FileFormatter/ValueObject/Fixture/composer_carriage_return_line_feed.json json eol=crlf
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"composer/xdebug-handler": "^2.0",
"danielstjules/stringy": "^3.1",
"doctrine/inflector": "^2.0",
"ergebnis/json-printer": "^3.1",
sabbelasichon marked this conversation as resolved.
Show resolved Hide resolved
"idiosyncratic/editorconfig": "^0.1.0",
"jean85/pretty-package-versions": "^1.6",
"nette/caching": "^3.1",
"nette/robot-loader": "^3.4",
Expand All @@ -48,6 +50,7 @@
"rector/rector-phpunit": "^0.10.8",
"rector/rector-symfony": "^0.10.5",
"sebastian/diff": "^4.0.4",
"shanethehat/pretty-xml": "^1.0",
"symfony/console": "^4.4.8|^5.1",
"symfony/dependency-injection": "^5.1",
"symfony/finder": "^4.4.8|^5.1",
Expand Down Expand Up @@ -156,7 +159,5 @@
"config": {
"sort-packages": true,
"platform-check": false
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
15 changes: 15 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
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 Nette\Caching\Cache;
use PhpParser\BuilderFactory;
use PhpParser\Lexer;
Expand All @@ -21,13 +24,16 @@
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TypeParser;
use PHPStan\Reflection\ReflectionProvider;
use PrettyXml\Formatter;
use Rector\BetterPhpDocParser\PhpDocParser\BetterPhpDocParser;
use Rector\BetterPhpDocParser\PhpDocParser\BetterTypeParser;
use Rector\Caching\Cache\NetteCacheFactory;
use Rector\Core\Console\ConsoleApplication;
use Rector\Core\NonPhpFile\Rector\RenameClassNonPhpRector;
use Rector\Core\PhpParser\Parser\NikicPhpParserFactory;
use Rector\Core\PhpParser\Parser\PhpParserLexerFactory;
use Rector\FileFormatter\Contract\EditorConfig\EditorConfigParserInterface;
use Rector\FileFormatter\EditorConfig\EditorConfigIdiosyncraticParser;
use Rector\NodeTypeResolver\DependencyInjection\PHPStanServicesFactory;
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocator\IntermediateSourceLocator;
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
Expand Down Expand Up @@ -146,4 +152,13 @@
->factory([service(PHPStanServicesFactory::class), 'createTypeNodeResolver']);
$services->set(DynamicSourceLocatorProvider::class)
->factory([service(PHPStanServicesFactory::class), 'createDynamicSourceLocatorProvider']);

$services->set(Printer::class);
$services->alias(PrinterInterface::class, Printer::class);

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

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

$services->alias(EditorConfigParserInterface::class, EditorConfigIdiosyncraticParser::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[composer.json]
indent_size = 1
indent_style = tab
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Rector\Tests\FileFormatter\EditorConfig\EditorConfigIdiosyncraticParser;

use Rector\Core\ValueObject\Application\File;
use Rector\FileFormatter\Contract\EditorConfig\EditorConfigParserInterface;
use Rector\FileFormatter\ValueObject\Indent;
use Rector\FileFormatter\ValueObjectFactory\EditorConfigConfigurationBuilder;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class EditorConfigIdiosyncraticParserTest extends AbstractTestCase
{
/**
* @var EditorConfigParserInterface
*/
private $editorConfigParser;

protected function setUp(): void
{
$this->boot();
$this->editorConfigParser = $this->getService(EditorConfigParserInterface::class);
}

public function testComposerJsonFile(): void
{
$editorConfigConfigurationBuilder = EditorConfigConfigurationBuilder::anEditorConfigConfiguration();
$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());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "foo/bar",
"description": "A foo bar baz extension",
"license": "GPL-2.0-or-later"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"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"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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
{
/**
* @var JsonFileFormatter
*/
private $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 = EditorConfigConfigurationBuilder::anEditorConfigConfiguration();
$editorConfigConfigurationBuilder->withIndent(Indent::createTabWithSize(1));

$this->jsonFileFormatter->format($file, $editorConfigConfigurationBuilder->build());

$this->assertSame($inputFileInfoAndExpected->getExpected(), $file->getFileContent());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
sabbelasichon marked this conversation as resolved.
Show resolved Hide resolved
<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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\FileFormatter\Formatter\XmlFileFormatter;

use Iterator;
use Rector\Core\ValueObject\Application\File;
use Rector\FileFormatter\Formatter\XmlFileFormatter;
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 XmlFileFormatterTest extends AbstractTestCase
{
/**
* @var XmlFileFormatter
*/
private $xmlFileFormatter;

protected function setUp(): void
{
$this->boot();
$this->xmlFileFormatter = $this->getService(XmlFileFormatter::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', '*.xml');
}

private function doTestFileInfo(SmartFileInfo $smartFileInfo): void
{
$inputFileInfoAndExpected = StaticFixtureSplitter::splitFileInfoToLocalInputAndExpected($smartFileInfo);

$inputFileInfo = $inputFileInfoAndExpected->getInputFileInfo();
$file = new File($inputFileInfo, $inputFileInfo->getContents());

$editorConfigConfigurationBuilder = EditorConfigConfigurationBuilder::anEditorConfigConfiguration();
$editorConfigConfigurationBuilder->withIndent(Indent::createTabWithSize(1));

$this->xmlFileFormatter->format($file, $editorConfigConfigurationBuilder->build());

$this->assertSame($inputFileInfoAndExpected->getExpected(), $file->getFileContent());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
martin:
name: Martin
job: Developer
skills:
- python
- perl
- pascal
-----
martin:
name: Martin
job: Developer
skills:
- python
- perl
- pascal
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\FileFormatter\Formatter\YamlFileFormatter;

use Iterator;
use Rector\Core\ValueObject\Application\File;
use Rector\FileFormatter\Formatter\YamlFileFormatter;
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 YamlFileFormatterTest extends AbstractTestCase
{
/**
* @var YamlFileFormatter
*/
private $yamlFileFormatter;

protected function setUp(): void
{
$this->boot();
$this->yamlFileFormatter = $this->getService(YamlFileFormatter::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', '*.yaml');
}

private function doTestFileInfo(SmartFileInfo $smartFileInfo): void
{
$inputFileInfoAndExpected = StaticFixtureSplitter::splitFileInfoToLocalInputAndExpected($smartFileInfo);

$inputFileInfo = $inputFileInfoAndExpected->getInputFileInfo();
$file = new File($inputFileInfo, $inputFileInfo->getContents());

$editorConfigConfigurationBuilder = EditorConfigConfigurationBuilder::anEditorConfigConfiguration();
$editorConfigConfigurationBuilder->withIndent(Indent::createSpaceWithSize(4));
$editorConfigConfigurationBuilder->withInsertFinalNewline(false);

$this->yamlFileFormatter->format($file, $editorConfigConfigurationBuilder->build());

$this->assertSame($inputFileInfoAndExpected->getExpected(), $file->getFileContent());
}
}