From 4c5bf755c61c602e02471724043f7899c7c7b2f0 Mon Sep 17 00:00:00 2001 From: Krzysztof Rewak Date: Mon, 12 Feb 2024 15:55:23 +0100 Subject: [PATCH] - importing FQCNs (#109) --- composer.json | 4 ++-- src/Config.php | 10 +++++++++- src/Configuration/Defaults/CommonRules.php | 22 +++++++++++++--------- tests/codestyle/CommonRulesetTest.php | 1 + tests/fixtures/classesImport/actual.php | 11 +++++++++++ tests/fixtures/classesImport/expected.php | 16 ++++++++++++++++ 6 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 tests/fixtures/classesImport/actual.php create mode 100644 tests/fixtures/classesImport/expected.php diff --git a/composer.json b/composer.json index 8e92467..606228d 100644 --- a/composer.json +++ b/composer.json @@ -5,8 +5,8 @@ "type": "library", "require": { "php": "^8.1", - "friendsofphp/php-cs-fixer": "^3.23", - "kubawerlos/php-cs-fixer-custom-fixers": "^3.16" + "friendsofphp/php-cs-fixer": "^3.49", + "kubawerlos/php-cs-fixer-custom-fixers": "^3.20" }, "require-dev": { "jetbrains/phpstorm-attributes": "^1.0", diff --git a/src/Config.php b/src/Config.php index 64f1992..2398fb3 100644 --- a/src/Config.php +++ b/src/Config.php @@ -23,6 +23,7 @@ class Config protected Paths $paths; protected Rules $rules; protected string $rootPath; + protected bool $withRiskyFixers = true; public function __construct( ?Paths $paths = null, @@ -52,7 +53,7 @@ public function config(): PhpCsFixerConfig ->setUsingCache(false) ->registerCustomFixers(new PhpCsFixerCustomFixers()) ->registerCustomFixers($this->getCustomFixers()) - ->setRiskyAllowed(true) + ->setRiskyAllowed($this->withRiskyFixers) ->setRules($rules); } @@ -72,6 +73,13 @@ public function purgeMode(): static return $this; } + public function withoutRiskyFixers(): static + { + $this->withRiskyFixers = false; + + return $this; + } + protected function getAllFiles(array &$paths, string $path): void { if (is_file($path) || !is_dir($path)) { diff --git a/src/Configuration/Defaults/CommonRules.php b/src/Configuration/Defaults/CommonRules.php index 2244d3d..1a7499a 100644 --- a/src/Configuration/Defaults/CommonRules.php +++ b/src/Configuration/Defaults/CommonRules.php @@ -12,7 +12,7 @@ use PhpCsFixer\Fixer\ArrayNotation\NoWhitespaceBeforeCommaInArrayFixer; use PhpCsFixer\Fixer\ArrayNotation\TrimArraySpacesFixer; use PhpCsFixer\Fixer\ArrayNotation\WhitespaceAfterCommaInArrayFixer; -use PhpCsFixer\Fixer\Basic\CurlyBracesPositionFixer; +use PhpCsFixer\Fixer\Basic\BracesPositionFixer; use PhpCsFixer\Fixer\Basic\NoMultipleStatementsPerLineFixer; use PhpCsFixer\Fixer\Basic\NoTrailingCommaInSinglelineFixer; use PhpCsFixer\Fixer\Basic\PsrAutoloadingFixer; @@ -37,8 +37,8 @@ use PhpCsFixer\Fixer\Comment\SingleLineCommentSpacingFixer; use PhpCsFixer\Fixer\ControlStructure\ControlStructureBracesFixer; use PhpCsFixer\Fixer\ControlStructure\ControlStructureContinuationPositionFixer; +use PhpCsFixer\Fixer\ControlStructure\NoUnneededBracesFixer; use PhpCsFixer\Fixer\ControlStructure\NoUnneededControlParenthesesFixer; -use PhpCsFixer\Fixer\ControlStructure\NoUnneededCurlyBracesFixer; use PhpCsFixer\Fixer\ControlStructure\NoUselessElseFixer; use PhpCsFixer\Fixer\ControlStructure\TrailingCommaInMultilineFixer; use PhpCsFixer\Fixer\ControlStructure\YodaStyleFixer; @@ -54,6 +54,7 @@ use PhpCsFixer\Fixer\Import\NoUnusedImportsFixer; use PhpCsFixer\Fixer\Import\OrderedImportsFixer; use PhpCsFixer\Fixer\Import\SingleLineAfterImportsFixer; +use PhpCsFixer\Fixer\LanguageConstruct\ClassKeywordFixer; use PhpCsFixer\Fixer\LanguageConstruct\DeclareEqualNormalizeFixer; use PhpCsFixer\Fixer\LanguageConstruct\ExplicitIndirectVariableFixer; use PhpCsFixer\Fixer\LanguageConstruct\FunctionToConstantFixer; @@ -66,7 +67,7 @@ use PhpCsFixer\Fixer\Operator\AssignNullCoalescingToCoalesceEqualFixer; use PhpCsFixer\Fixer\Operator\BinaryOperatorSpacesFixer; use PhpCsFixer\Fixer\Operator\ConcatSpaceFixer; -use PhpCsFixer\Fixer\Operator\NewWithBracesFixer; +use PhpCsFixer\Fixer\Operator\NewWithParenthesesFixer; use PhpCsFixer\Fixer\Operator\NoUselessNullsafeOperatorFixer; use PhpCsFixer\Fixer\Operator\ObjectOperatorWithoutWhitespaceFixer; use PhpCsFixer\Fixer\Operator\StandardizeIncrementFixer; @@ -101,7 +102,7 @@ use PhpCsFixer\Fixer\Whitespace\ArrayIndentationFixer; use PhpCsFixer\Fixer\Whitespace\BlankLineBeforeStatementFixer; use PhpCsFixer\Fixer\Whitespace\BlankLineBetweenImportGroupsFixer; -use PhpCsFixer\Fixer\Whitespace\CompactNullableTypehintFixer; +use PhpCsFixer\Fixer\Whitespace\CompactNullableTypeDeclarationFixer; use PhpCsFixer\Fixer\Whitespace\LineEndingFixer; use PhpCsFixer\Fixer\Whitespace\MethodChainingIndentationFixer; use PhpCsFixer\Fixer\Whitespace\NoExtraBlankLinesFixer; @@ -141,7 +142,7 @@ class CommonRules extends Rules SingleClassElementPerStatementFixer::class => [ "elements" => ["const", "property"], ], - NewWithBracesFixer::class => true, + NewWithParenthesesFixer::class => true, ClassDefinitionFixer::class => [ "single_line" => true, ], @@ -194,7 +195,7 @@ class CommonRules extends Rules NoUnusedImportsFixer::class => true, NoEmptyStatementFixer::class => true, NoUnneededControlParenthesesFixer::class => true, - NoUnneededCurlyBracesFixer::class => true, + NoUnneededBracesFixer::class => true, DeclareStrictTypesFixer::class => true, CastSpacesFixer::class => [ "space" => "none", @@ -202,7 +203,9 @@ class CommonRules extends Rules DoubleQuoteFixer::class => true, VoidReturnFixer::class => true, UseArrowFunctionsFixer::class => true, - FullyQualifiedStrictTypesFixer::class => true, + FullyQualifiedStrictTypesFixer::class => [ + "import_symbols" => true, + ], OrderedImportsFixer::class => [ "sort_algorithm" => "alpha", "imports_order" => ["class", "function", "const"], @@ -255,7 +258,7 @@ class CommonRules extends Rules NoLeadingImportSlashFixer::class => true, LowercaseCastFixer::class => true, LowercaseStaticReferenceFixer::class => true, - CompactNullableTypehintFixer::class => true, + CompactNullableTypeDeclarationFixer::class => true, DeclareEqualNormalizeFixer::class => true, ShortScalarCastFixer::class => true, CleanNamespaceFixer::class => true, @@ -328,11 +331,12 @@ class CommonRules extends Rules TypeDeclarationSpacesFixer::class => true, ControlStructureBracesFixer::class => true, ControlStructureContinuationPositionFixer::class => true, - CurlyBracesPositionFixer::class => [ + BracesPositionFixer::class => [ "anonymous_functions_opening_brace" => "same_line", ], LowercaseKeywordsFixer::class => true, NoMultilineWhitespaceAroundDoubleArrowFixer::class => true, CompactEmptyArrayFixer::class => true, + ClassKeywordFixer::class => true, ]; } diff --git a/tests/codestyle/CommonRulesetTest.php b/tests/codestyle/CommonRulesetTest.php index 87c8333..8662f73 100644 --- a/tests/codestyle/CommonRulesetTest.php +++ b/tests/codestyle/CommonRulesetTest.php @@ -65,6 +65,7 @@ public static function providePhp80Fixtures(): array ["lowercaseKeywords"], ["noMultilineWhitespaceAroundDoubleArrow"], ["compactArray"], + ["classesImport"], ]; } diff --git a/tests/fixtures/classesImport/actual.php b/tests/fixtures/classesImport/actual.php new file mode 100644 index 0000000..2735547 --- /dev/null +++ b/tests/fixtures/classesImport/actual.php @@ -0,0 +1,11 @@ +