From f8b0ac301fe9a2c8db3aa5718770ce228550dd2a Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 11 May 2021 17:48:41 +0200 Subject: [PATCH] update to new phpstan rules, add static fixes (#18) * update to new phpstan rules, add static fixes * [ci-review] Rector Rectify Co-authored-by: kaizen-ci --- composer.json | 2 +- config/services.php | 4 -- .../.editorconfig | 0 .../EditorConfigParserTest.php} | 12 +++--- .../Fixture/composer.json | 0 .../EditorConfigParserInterface.php | 40 ------------------- ...raticParser.php => EditorConfigParser.php} | 28 +++++++------ packages/FileFormatter/FileFormatter.php | 8 ++-- .../ValueObject/EditorConfigOption.php | 33 +++++++++++++++ phpstan.neon | 10 ++++- .../TypeInferer/SplArrayFixedTypeNarrower.php | 2 +- stubs/PhpCsFixer/Tokenizer/Tokens.php | 14 +++++++ 12 files changed, 83 insertions(+), 70 deletions(-) rename packages-tests/FileFormatter/EditorConfig/{EditorConfigIdiosyncraticParser => EditorConfigParser}/.editorconfig (100%) rename packages-tests/FileFormatter/EditorConfig/{EditorConfigIdiosyncraticParser/EditorConfigIdiosyncraticParserTest.php => EditorConfigParser/EditorConfigParserTest.php} (80%) rename packages-tests/FileFormatter/EditorConfig/{EditorConfigIdiosyncraticParser => EditorConfigParser}/Fixture/composer.json (100%) delete mode 100644 packages/FileFormatter/Contract/EditorConfig/EditorConfigParserInterface.php rename packages/FileFormatter/EditorConfig/{EditorConfigIdiosyncraticParser.php => EditorConfigParser.php} (51%) create mode 100644 packages/FileFormatter/ValueObject/EditorConfigOption.php create mode 100644 stubs/PhpCsFixer/Tokenizer/Tokens.php diff --git a/composer.json b/composer.json index 240a48938ad4..d470d8b50d59 100644 --- a/composer.json +++ b/composer.json @@ -56,7 +56,7 @@ "phpstan/phpstan-nette": "^0.12.16", "phpunit/phpunit": "^9.5", "rector/rector-generator": "^0.1.7", - "rector/rector-phpstan-rules": "^0.1", + "rector/rector-phpstan-rules": "^0.2.3", "symplify/coding-standard": "^9.3.5", "symplify/easy-ci": "^9.3.5", "symplify/easy-coding-standard": "^9.3.5", diff --git a/config/services.php b/config/services.php index cdff9644df11..0e7a3c5118c1 100644 --- a/config/services.php +++ b/config/services.php @@ -31,8 +31,6 @@ use Rector\Core\Console\ConsoleApplication; 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; @@ -155,6 +153,4 @@ $services->set(Formatter::class); $services->set(EditorConfig::class); - - $services->alias(EditorConfigParserInterface::class, EditorConfigIdiosyncraticParser::class); }; diff --git a/packages-tests/FileFormatter/EditorConfig/EditorConfigIdiosyncraticParser/.editorconfig b/packages-tests/FileFormatter/EditorConfig/EditorConfigParser/.editorconfig similarity index 100% rename from packages-tests/FileFormatter/EditorConfig/EditorConfigIdiosyncraticParser/.editorconfig rename to packages-tests/FileFormatter/EditorConfig/EditorConfigParser/.editorconfig diff --git a/packages-tests/FileFormatter/EditorConfig/EditorConfigIdiosyncraticParser/EditorConfigIdiosyncraticParserTest.php b/packages-tests/FileFormatter/EditorConfig/EditorConfigParser/EditorConfigParserTest.php similarity index 80% rename from packages-tests/FileFormatter/EditorConfig/EditorConfigIdiosyncraticParser/EditorConfigIdiosyncraticParserTest.php rename to packages-tests/FileFormatter/EditorConfig/EditorConfigParser/EditorConfigParserTest.php index cecf58ae4bf0..2880161e6109 100644 --- a/packages-tests/FileFormatter/EditorConfig/EditorConfigIdiosyncraticParser/EditorConfigIdiosyncraticParserTest.php +++ b/packages-tests/FileFormatter/EditorConfig/EditorConfigParser/EditorConfigParserTest.php @@ -1,25 +1,27 @@ boot(); - $this->editorConfigParser = $this->getService(EditorConfigParserInterface::class); + $this->editorConfigParser = $this->getService(EditorConfigParser::class); } public function testComposerJsonFile(): void diff --git a/packages-tests/FileFormatter/EditorConfig/EditorConfigIdiosyncraticParser/Fixture/composer.json b/packages-tests/FileFormatter/EditorConfig/EditorConfigParser/Fixture/composer.json similarity index 100% rename from packages-tests/FileFormatter/EditorConfig/EditorConfigIdiosyncraticParser/Fixture/composer.json rename to packages-tests/FileFormatter/EditorConfig/EditorConfigParser/Fixture/composer.json diff --git a/packages/FileFormatter/Contract/EditorConfig/EditorConfigParserInterface.php b/packages/FileFormatter/Contract/EditorConfig/EditorConfigParserInterface.php deleted file mode 100644 index 6d6f383bd45a..000000000000 --- a/packages/FileFormatter/Contract/EditorConfig/EditorConfigParserInterface.php +++ /dev/null @@ -1,40 +0,0 @@ -getSmartFileInfo(); $configuration = $this->editorConfig->getConfigForPath($smartFileInfo->getRealPath()); - if (array_key_exists(self::INDENT_STYLE, $configuration)) { - $indentStyle = (string) $configuration[self::INDENT_STYLE]->getValue(); + if (array_key_exists(EditorConfigOption::INDENT_STYLE, $configuration)) { + $indentStyle = (string) $configuration[EditorConfigOption::INDENT_STYLE]->getValue(); $editorConfigConfigurationBuilder->withIndentStyle($indentStyle); } - if (array_key_exists(self::INDENT_SIZE, $configuration)) { - $indentSize = (int) $configuration[self::INDENT_SIZE]->getValue(); + if (array_key_exists(EditorConfigOption::INDENT_SIZE, $configuration)) { + $indentSize = (int) $configuration[EditorConfigOption::INDENT_SIZE]->getValue(); $editorConfigConfigurationBuilder->withIndentSize($indentSize); } - if (array_key_exists(self::END_OF_LINE, $configuration)) { - $endOfLine = (string) $configuration[self::END_OF_LINE]->getValue(); + if (array_key_exists(EditorConfigOption::END_OF_LINE, $configuration)) { + $endOfLine = (string) $configuration[EditorConfigOption::END_OF_LINE]->getValue(); $editorConfigConfigurationBuilder->withEndOfLineFromEditorConfig($endOfLine); } - if (array_key_exists(self::INSERT_FINAL_NEWLINE, $configuration)) { - $insertFinalNewline = (bool) $configuration[self::INSERT_FINAL_NEWLINE]->getValue(); + if (array_key_exists(EditorConfigOption::INSERT_FINAL_NEWLINE, $configuration)) { + $insertFinalNewline = (bool) $configuration[EditorConfigOption::INSERT_FINAL_NEWLINE]->getValue(); $editorConfigConfigurationBuilder->withInsertFinalNewline($insertFinalNewline); } - if (array_key_exists(self::TAB_WIDTH, $configuration)) { - $editorConfigConfigurationBuilder->withIndentSize($configuration[self::TAB_WIDTH]->getValue()); + if (array_key_exists(EditorConfigOption::TAB_WIDTH, $configuration)) { + $editorConfigConfigurationBuilder->withIndentSize( + $configuration[EditorConfigOption::TAB_WIDTH]->getValue() + ); } return $editorConfigConfigurationBuilder->build(); diff --git a/packages/FileFormatter/FileFormatter.php b/packages/FileFormatter/FileFormatter.php index 0cefce8e5724..2c0c77a86d39 100644 --- a/packages/FileFormatter/FileFormatter.php +++ b/packages/FileFormatter/FileFormatter.php @@ -6,8 +6,8 @@ use Rector\Core\Configuration\Option; use Rector\Core\ValueObject\Application\File; -use Rector\FileFormatter\Contract\EditorConfig\EditorConfigParserInterface; 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; @@ -22,7 +22,7 @@ final class FileFormatter * @param FileFormatterInterface[] $fileFormatters */ public function __construct( - private EditorConfigParserInterface $editorConfigParser, + private EditorConfigParser $editorConfigParser, private ParameterProvider $parameterProvider, private array $fileFormatters = [] ) { @@ -62,13 +62,13 @@ private function sniffOriginalFileContent( try { $indent = Indent::fromContent($file->getOriginalFileContent()); $editorConfigConfigurationBuilder->withIndent($indent); - } catch (ParseIndentException $parseIndentException) { + } catch (ParseIndentException) { } try { $newLine = NewLine::fromContent($file->getOriginalFileContent()); $editorConfigConfigurationBuilder->withNewLine($newLine); - } catch (InvalidNewLineStringException $invalidNewLineStringException) { + } catch (InvalidNewLineStringException) { } } diff --git a/packages/FileFormatter/ValueObject/EditorConfigOption.php b/packages/FileFormatter/ValueObject/EditorConfigOption.php new file mode 100644 index 000000000000..907e134c4311 --- /dev/null +++ b/packages/FileFormatter/ValueObject/EditorConfigOption.php @@ -0,0 +1,33 @@ +isSuperTypeOf(new ObjectType('SplArrayFixed'))->no()) { + if ($paramType->isSuperTypeOf(new ObjectType('SplFixedArray'))->no()) { return $paramType; } diff --git a/stubs/PhpCsFixer/Tokenizer/Tokens.php b/stubs/PhpCsFixer/Tokenizer/Tokens.php new file mode 100644 index 000000000000..32430592b429 --- /dev/null +++ b/stubs/PhpCsFixer/Tokenizer/Tokens.php @@ -0,0 +1,14 @@ +