-
-
Notifications
You must be signed in to change notification settings - Fork 86
New Feature: Add .editorconfig Support #259
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cba0580
fix(phpcs): respect sniffers requesting to be disabled during file pr…
Kenneth-Sills 5cc551a
fix: correct bad syntax from improperly patched commit
Kenneth-Sills 409bf0f
test: add a test case for the new bug fix
Kenneth-Sills dd18797
style: fix styling after the wrong auto formatter ran on File.php
Kenneth-Sills cb18f30
fix: add more duplicate/conflicting checker relationships
Kenneth-Sills 2140c44
feat: add initial support for `.editorconfig`
Kenneth-Sills d8eb232
rebase
TomasVotruba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\EasyCodingStandard\Configuration\EditorConfig; | ||
|
||
/** | ||
* @see https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties | ||
*/ | ||
class EditorConfig | ||
{ | ||
public function __construct( | ||
public readonly ?IndentStyle $indentStyle, | ||
public readonly ?EndOfLine $endOfLine, | ||
public readonly ?bool $trimTrailingWhitespace, | ||
public readonly ?bool $insertFinalNewline, | ||
public readonly ?int $maxLineLength, | ||
public readonly ?QuoteType $quoteType | ||
) { | ||
} | ||
} |
This file contains hidden or 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,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\EasyCodingStandard\Configuration\EditorConfig; | ||
|
||
use Exception; | ||
|
||
class EditorConfigFactory | ||
{ | ||
public function load(): EditorConfig | ||
{ | ||
// By default, composer executes scripts from within the project root. | ||
$projectRoot = getcwd(); | ||
$editorConfigPath = $projectRoot . '/.editorconfig'; | ||
|
||
if (! file_exists($editorConfigPath)) { | ||
throw new Exception('No .editorconfig found.'); | ||
} | ||
|
||
$configFileContent = file_get_contents($editorConfigPath); | ||
|
||
if ($configFileContent === false) { | ||
throw new Exception('Unable to load .editorconfig.'); | ||
} | ||
|
||
return $this->parse($configFileContent); | ||
} | ||
|
||
public function parse(string $editorConfigFileContents): EditorConfig | ||
{ | ||
$fullConfig = parse_ini_string($editorConfigFileContents, true, INI_SCANNER_TYPED); | ||
|
||
if ($fullConfig === false) { | ||
throw new Exception('Unable to parse .editorconfig.'); | ||
} | ||
|
||
$config = [...$fullConfig['*'] ?? [], ...$fullConfig['*.php'] ?? []]; | ||
|
||
// Just letting "validation" happen with PHP's type hints. | ||
return new EditorConfig( | ||
indentStyle: $this->field($config, 'indent_style', IndentStyle::tryFrom(...)), | ||
endOfLine: $this->field($config, 'end_of_line', EndOfLine::tryFrom(...)), | ||
trimTrailingWhitespace: $this->field($config, 'trim_trailing_whitespace', $this->id(...)), | ||
insertFinalNewline: $this->field($config, 'insert_final_newline', $this->id(...)), | ||
maxLineLength: $this->field($config, 'max_line_length', $this->id(...)), | ||
quoteType: $this->field($config, 'quote_type', QuoteType::tryFrom(...)), | ||
); | ||
} | ||
|
||
/** | ||
* @template From | ||
* @template To | ||
* @param mixed[] $config | ||
* @param callable(From): To $transform | ||
* @return To|null | ||
*/ | ||
private function field(array $config, string $field, callable $transform): mixed | ||
{ | ||
if (! isset($config[$field])) { | ||
return null; | ||
} | ||
|
||
return $transform($config[$field]); | ||
} | ||
|
||
/** | ||
* @template T | ||
* @param T $value | ||
* @return T | ||
*/ | ||
private function id(mixed $value): mixed | ||
{ | ||
return $value; | ||
} | ||
} |
This file contains hidden or 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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\EasyCodingStandard\Configuration\EditorConfig; | ||
|
||
enum EndOfLine: string | ||
{ | ||
case Posix = 'lf'; | ||
case Legacy = 'cr'; | ||
case Windows = 'crlf'; | ||
} |
This file contains hidden or 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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\EasyCodingStandard\Configuration\EditorConfig; | ||
|
||
enum IndentStyle: string | ||
{ | ||
case Space = 'space'; | ||
case Tab = 'tab'; | ||
} |
This file contains hidden or 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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\EasyCodingStandard\Configuration\EditorConfig; | ||
|
||
/** | ||
* @see https://github.com/jednano/codepainter#quote_type-single-double-auto | ||
*/ | ||
enum QuoteType: string | ||
{ | ||
case Single = 'single'; | ||
case Double = 'double'; | ||
case Auto = 'auto'; | ||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems cause downgrade build error, see
https://github.com/easy-coding-standard/easy-coding-standard/actions/runs/13073439464/job/36479848211#step:12:51
I will look into it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems due to enum usage
I think we can just use class and constant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created new PR to fix it
by use class and constant instead.