-
-
Notifications
You must be signed in to change notification settings - Fork 671
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
5b386ab
commit b8ccbd6
Showing
17 changed files
with
571 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,55 @@ | ||
<?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->getDefaultEditorConfiguration() | ||
); | ||
|
||
$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,29 @@ | ||
<?php | ||
|
||
namespace Rector\Core\Contract\EditorConfig; | ||
|
||
use Rector\Core\ValueObject\Application\File; | ||
use Rector\Core\ValueObject\EditorConfigConfiguration; | ||
|
||
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'; | ||
|
||
public function extractConfigurationForFile( | ||
File $file, | ||
EditorConfigConfiguration $defaultEditorConfiguration | ||
): 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,15 @@ | ||
<?php | ||
|
||
namespace Rector\Core\Contract\Formatter; | ||
|
||
use Rector\Core\ValueObject\Application\File; | ||
use Rector\Core\ValueObject\EditorConfigConfiguration; | ||
|
||
interface FormatterInterface | ||
{ | ||
public function supports(File $file): bool; | ||
|
||
public function format(File $file, EditorConfigConfiguration $editorConfigConfiguration): void; | ||
|
||
public function getDefaultEditorConfiguration(): 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,51 @@ | ||
<?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; | ||
|
||
/** | ||
* @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, | ||
EditorConfigConfiguration $defaultEditorConfiguration | ||
): EditorConfigConfiguration { | ||
$smartFileInfo = $file->getSmartFileInfo(); | ||
$configuration = $this->editorConfig->getConfigForPath($smartFileInfo->getRealPath()); | ||
|
||
$identStyle = $defaultEditorConfiguration->getIndentStyle(); | ||
$identSize = $defaultEditorConfiguration->getIndentSize(); | ||
$endOfLine = $defaultEditorConfiguration->getEndOfLine(); | ||
|
||
if (array_key_exists(self::INDENT_STYLE, $configuration)) { | ||
$identStyle = $configuration[self::INDENT_STYLE]->getValue(); | ||
} | ||
|
||
if (array_key_exists(self::INDENT_SIZE, $configuration)) { | ||
$identSize = $configuration[self::INDENT_SIZE]->getValue(); | ||
} | ||
|
||
if (array_key_exists(self::END_OF_LINE, $configuration)) { | ||
$endOfLine = $configuration[self::END_OF_LINE]->getValue(); | ||
} | ||
|
||
return new EditorConfigConfiguration($identStyle, $identSize, $endOfLine); | ||
} | ||
} |
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,53 @@ | ||
<?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; | ||
|
||
/** | ||
* @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 | ||
{ | ||
$indent = str_pad('', $editorConfigConfiguration->getIndentSize()); | ||
if ($editorConfigConfiguration->getIsTab()) { | ||
$indent = "\t"; | ||
} | ||
|
||
$newContent = $this->jsonPrinter->print( | ||
$file->getFileContent(), | ||
$indent, | ||
$editorConfigConfiguration->getEndOfLine() | ||
); | ||
|
||
$file->changeFileContent($newContent); | ||
} | ||
|
||
public function getDefaultEditorConfiguration(): EditorConfigConfiguration | ||
{ | ||
return new EditorConfigConfiguration(EditorConfigConfiguration::SPACE, 2, EditorConfigConfiguration::LINE_FEED); | ||
} | ||
} |
Oops, something went wrong.