-
-
Notifications
You must be signed in to change notification settings - Fork 670
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Take .editorconfig into account
Resolves: #6215
- Loading branch information
1 parent
37e0776
commit 9695171
Showing
18 changed files
with
794 additions
and
3 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 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,56 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\Application; | ||
|
||
use Rector\Core\Contract\EditorConfig\EditorConfigParserInterface; | ||
use Rector\Core\Contract\Formatter\FormatterInterface; | ||
use Rector\Core\ValueObject\Application\File; | ||
|
||
final class FileFormatter | ||
{ | ||
/** | ||
* @var EditorConfigParserInterface | ||
*/ | ||
private $editorConfigParser; | ||
|
||
/** | ||
* @var FormatterInterface[] | ||
*/ | ||
private $fileFormatters; | ||
|
||
/** | ||
* @param FormatterInterface[] $fileFormatters | ||
*/ | ||
public function __construct(EditorConfigParserInterface $editorConfigParser, array $fileFormatters = []) | ||
{ | ||
$this->editorConfigParser = $editorConfigParser; | ||
$this->fileFormatters = $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; | ||
} | ||
|
||
$editorConfiguration = $this->editorConfigParser->extractConfigurationForFile( | ||
$file, | ||
$fileFormatter->createEditorConfigConfigurationBuilder() | ||
); | ||
|
||
$fileFormatter->format($file, $editorConfiguration); | ||
} | ||
} | ||
} | ||
} |
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,40 @@ | ||
<?php | ||
|
||
namespace Rector\Core\Contract\EditorConfig; | ||
|
||
use Rector\Core\ValueObject\Application\File; | ||
use Rector\Core\ValueObject\EditorConfigConfiguration; | ||
use Rector\Core\ValueObjectFactory\EditorConfigConfigurationBuilder; | ||
|
||
interface EditorConfigParserInterface | ||
{ | ||
/** | ||
* @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'; | ||
|
||
public function extractConfigurationForFile( | ||
File $file, | ||
EditorConfigConfigurationBuilder $editorConfigConfigurationBuilder | ||
): EditorConfigConfiguration; | ||
} |
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,16 @@ | ||
<?php | ||
|
||
namespace Rector\Core\Contract\Formatter; | ||
|
||
use Rector\Core\ValueObject\Application\File; | ||
use Rector\Core\ValueObject\EditorConfigConfiguration; | ||
use Rector\Core\ValueObjectFactory\EditorConfigConfigurationBuilder; | ||
|
||
interface FormatterInterface | ||
{ | ||
public function supports(File $file): bool; | ||
|
||
public function format(File $file, EditorConfigConfiguration $editorConfigConfiguration): void; | ||
|
||
public function createEditorConfigConfigurationBuilder(): EditorConfigConfigurationBuilder; | ||
} |
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,58 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\EditorConfig; | ||
|
||
use Idiosyncratic\EditorConfig\EditorConfig; | ||
use Rector\Core\Contract\EditorConfig\EditorConfigParserInterface; | ||
use Rector\Core\ValueObject\Application\File; | ||
use Rector\Core\ValueObject\EditorConfigConfiguration; | ||
use Rector\Core\ValueObjectFactory\EditorConfigConfigurationBuilder; | ||
|
||
/** | ||
* @see \Rector\Core\Tests\EditorConfig\EditorConfigIdiosyncraticParser\EditorConfigIdiosyncraticParserTest | ||
*/ | ||
final class EditorConfigIdiosyncraticParser implements EditorConfigParserInterface | ||
{ | ||
/** | ||
* @var EditorConfig | ||
*/ | ||
private $editorConfig; | ||
|
||
public function __construct(EditorConfig $editorConfig) | ||
{ | ||
$this->editorConfig = $editorConfig; | ||
} | ||
|
||
public function extractConfigurationForFile( | ||
File $file, | ||
EditorConfigConfigurationBuilder $editorConfigConfigurationBuilder | ||
): EditorConfigConfiguration { | ||
$smartFileInfo = $file->getSmartFileInfo(); | ||
$configuration = $this->editorConfig->getConfigForPath($smartFileInfo->getRealPath()); | ||
|
||
if (array_key_exists(self::INDENT_STYLE, $configuration)) { | ||
$editorConfigConfigurationBuilder->withIndentStyle($configuration[self::INDENT_STYLE]->getValue()); | ||
} | ||
|
||
if (array_key_exists(self::INDENT_SIZE, $configuration)) { | ||
$editorConfigConfigurationBuilder->withIndentSize($configuration[self::INDENT_SIZE]->getValue()); | ||
} | ||
|
||
if (array_key_exists(self::END_OF_LINE, $configuration)) { | ||
$editorConfigConfigurationBuilder->withEndOfLine($configuration[self::END_OF_LINE]->getValue()); | ||
} | ||
|
||
if (array_key_exists(self::INSERT_FINAL_NEWLINE, $configuration)) { | ||
$editorConfigConfigurationBuilder->withInsertFinalNewline( | ||
$configuration[self::INSERT_FINAL_NEWLINE]->getValue() | ||
); | ||
} | ||
|
||
if (array_key_exists(self::TAB_WIDTH, $configuration)) { | ||
$editorConfigConfigurationBuilder->withTabWidth($configuration[self::TAB_WIDTH]->getValue()); | ||
} | ||
|
||
return $editorConfigConfigurationBuilder->build(); | ||
} | ||
} |
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 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\Exception; | ||
|
||
use InvalidArgumentException; | ||
|
||
final class EditorConfigConfigurationException extends InvalidArgumentException | ||
{ | ||
} |
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,57 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\Formatter; | ||
|
||
use Ergebnis\Json\Printer\Printer; | ||
use Rector\Core\Contract\Formatter\FormatterInterface; | ||
use Rector\Core\ValueObject\Application\File; | ||
use Rector\Core\ValueObject\EditorConfigConfiguration; | ||
use Rector\Core\ValueObjectFactory\EditorConfigConfigurationBuilder; | ||
|
||
/** | ||
* @see \Rector\Core\Tests\Formatter\JsonFormatter\JsonFormatterTest | ||
*/ | ||
final class JsonFormatter implements FormatterInterface | ||
{ | ||
/** | ||
* @var Printer | ||
*/ | ||
private $jsonPrinter; | ||
|
||
public function __construct(Printer $jsonPrinter) | ||
{ | ||
$this->jsonPrinter = $jsonPrinter; | ||
} | ||
|
||
public function supports(File $file): bool | ||
{ | ||
$smartFileInfo = $file->getSmartFileInfo(); | ||
|
||
return in_array($smartFileInfo->getExtension(), ['json'], true); | ||
} | ||
|
||
public function format(File $file, EditorConfigConfiguration $editorConfigConfiguration): void | ||
{ | ||
$newContent = $this->jsonPrinter->print( | ||
$file->getFileContent(), | ||
$editorConfigConfiguration->getIndent(), | ||
$editorConfigConfiguration->getEndOfLine() | ||
); | ||
|
||
$newContent .= $editorConfigConfiguration->getFinalNewline(); | ||
|
||
$file->changeFileContent($newContent); | ||
} | ||
|
||
public function createEditorConfigConfigurationBuilder(): EditorConfigConfigurationBuilder | ||
{ | ||
$editorConfigConfigurationBuilder = EditorConfigConfigurationBuilder::anEditorConfigConfiguration(); | ||
$editorConfigConfigurationBuilder->withLineFeed(); | ||
$editorConfigConfigurationBuilder->withSpace(); | ||
$editorConfigConfigurationBuilder->withIndentSize(4); | ||
$editorConfigConfigurationBuilder->withFinalNewline(); | ||
|
||
return $editorConfigConfigurationBuilder; | ||
} | ||
} |
Oops, something went wrong.