From 12f2e2ff9b5ab0f0380020764bb01eefa09d65ec Mon Sep 17 00:00:00 2001 From: Kamil Piech Date: Tue, 21 Nov 2023 10:12:51 +0100 Subject: [PATCH] #105 - array no multiline whitespace (#107) * #105 - added array whitespace fixer * #105 - updated tests * #105 - new fixer added * #105 - merged main * #105 - cr fixes * #105 - extended tests * #105 - cr fixes --- src/Config.php | 2 + src/Configuration/Defaults/CommonRules.php | 2 + src/Fixers/CompactEmptyArrayFixer.php | 87 +++++++++++++++++++ tests/codestyle/CommonRulesetTest.php | 2 + tests/fixtures/compactArray/actual.php | 26 ++++++ tests/fixtures/compactArray/expected.php | 18 ++++ .../actual.php | 2 +- .../expected.php | 2 +- 8 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 src/Fixers/CompactEmptyArrayFixer.php create mode 100644 tests/fixtures/compactArray/actual.php create mode 100644 tests/fixtures/compactArray/expected.php diff --git a/src/Config.php b/src/Config.php index f4aa888..64f1992 100644 --- a/src/Config.php +++ b/src/Config.php @@ -9,6 +9,7 @@ use Blumilk\Codestyle\Configuration\Paths; use Blumilk\Codestyle\Configuration\Rules; use Blumilk\Codestyle\Configuration\Utils\Rule; +use Blumilk\Codestyle\Fixers\CompactEmptyArrayFixer; use Blumilk\Codestyle\Fixers\DoubleQuoteFixer; use Blumilk\Codestyle\Fixers\NoCommentFixer; use Blumilk\Codestyle\Fixers\NoLaravelMigrationsGeneratedCommentFixer; @@ -95,6 +96,7 @@ protected function getCustomFixers(): array new DoubleQuoteFixer(), new NoLaravelMigrationsGeneratedCommentFixer(), new NoCommentFixer(), + new CompactEmptyArrayFixer(), ]; } } diff --git a/src/Configuration/Defaults/CommonRules.php b/src/Configuration/Defaults/CommonRules.php index a9cb402..2244d3d 100644 --- a/src/Configuration/Defaults/CommonRules.php +++ b/src/Configuration/Defaults/CommonRules.php @@ -4,6 +4,7 @@ namespace Blumilk\Codestyle\Configuration\Defaults; +use Blumilk\Codestyle\Fixers\CompactEmptyArrayFixer; use Blumilk\Codestyle\Fixers\DoubleQuoteFixer; use Blumilk\Codestyle\Fixers\NoLaravelMigrationsGeneratedCommentFixer; use PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer; @@ -332,5 +333,6 @@ class CommonRules extends Rules ], LowercaseKeywordsFixer::class => true, NoMultilineWhitespaceAroundDoubleArrowFixer::class => true, + CompactEmptyArrayFixer::class => true, ]; } diff --git a/src/Fixers/CompactEmptyArrayFixer.php b/src/Fixers/CompactEmptyArrayFixer.php new file mode 100644 index 0000000..0dae607 --- /dev/null +++ b/src/Fixers/CompactEmptyArrayFixer.php @@ -0,0 +1,87 @@ +isAnyTokenKindsFound([T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN, CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN]); + } + + public function isRisky(): bool + { + return false; + } + + public function fix(SplFileInfo $file, Tokens $tokens): void + { + foreach ($tokens as $index => $token) { + if ($token->isGivenKind([T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN, CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN])) { + $startIndex = $index; + + if ($token->isGivenKind(T_ARRAY)) { + $startIndex = $tokens->getNextMeaningfulToken($startIndex); + $endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startIndex); + } elseif ($token->isGivenKind(CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN)) { + $endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_DESTRUCTURING_SQUARE_BRACE, $startIndex); + } else { + $endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $startIndex); + } + + // Check if the array is empty and multiline + $isEmptyArray = true; + + for ($i = $index + 1; $i < $endIndex; ++$i) { + if (!$tokens[$i]->isWhitespace() && !$tokens[$i]->isComment()) { + $isEmptyArray = false; + + break; + } + } + + if ($isEmptyArray) { + for ($i = $index + 1; $i < $endIndex; ++$i) { + $tokens->clearAt($i); + } + } + } + } + } + + public function getName(): string + { + return "Blumilk/compact_empty_array"; + } + + public function getPriority(): int + { + $fixer = new NoCommentedOutCodeFixer(); + + return $fixer->getPriority() - 1; + } + + public function supports(SplFileInfo $file): bool + { + return true; + } +} diff --git a/tests/codestyle/CommonRulesetTest.php b/tests/codestyle/CommonRulesetTest.php index d7fc94d..87c8333 100644 --- a/tests/codestyle/CommonRulesetTest.php +++ b/tests/codestyle/CommonRulesetTest.php @@ -63,6 +63,8 @@ public static function providePhp80Fixtures(): array ["braces"], ["stringVariables"], ["lowercaseKeywords"], + ["noMultilineWhitespaceAroundDoubleArrow"], + ["compactArray"], ]; } diff --git a/tests/fixtures/compactArray/actual.php b/tests/fixtures/compactArray/actual.php new file mode 100644 index 0000000..996ab2c --- /dev/null +++ b/tests/fixtures/compactArray/actual.php @@ -0,0 +1,26 @@ +