Skip to content

Commit

Permalink
#105 - array no multiline whitespace (#107)
Browse files Browse the repository at this point in the history
* #105 - added array whitespace fixer

* #105 - updated tests

* #105 - new fixer added

* #105 - merged main

* #105 - cr fixes

* #105 - extended tests

* #105 - cr fixes
  • Loading branch information
kamilpiech97 authored Nov 21, 2023
1 parent 4ca2988 commit 12f2e2f
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -95,6 +96,7 @@ protected function getCustomFixers(): array
new DoubleQuoteFixer(),
new NoLaravelMigrationsGeneratedCommentFixer(),
new NoCommentFixer(),
new CompactEmptyArrayFixer(),
];
}
}
2 changes: 2 additions & 0 deletions src/Configuration/Defaults/CommonRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -332,5 +333,6 @@ class CommonRules extends Rules
],
LowercaseKeywordsFixer::class => true,
NoMultilineWhitespaceAroundDoubleArrowFixer::class => true,
CompactEmptyArrayFixer::class => true,
];
}
87 changes: 87 additions & 0 deletions src/Fixers/CompactEmptyArrayFixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace Blumilk\Codestyle\Fixers;

use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixerCustomFixers\Fixer\NoCommentedOutCodeFixer;
use SplFileInfo;

final class CompactEmptyArrayFixer implements FixerInterface
{
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
"Ensures that empty arrays are declared using compact syntax.",
[new CodeSample("<?php\n\$array = [\n];\n")],
);
}

public function isCandidate(Tokens $tokens): bool
{
return $tokens->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;
}
}
2 changes: 2 additions & 0 deletions tests/codestyle/CommonRulesetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public static function providePhp80Fixtures(): array
["braces"],
["stringVariables"],
["lowercaseKeywords"],
["noMultilineWhitespaceAroundDoubleArrow"],
["compactArray"],
];
}

Expand Down
26 changes: 26 additions & 0 deletions tests/fixtures/compactArray/actual.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

class CompactArray
{
protected $except = [
];
protected $excepted = [


];
protected $other = [

];
protected $commentWithValue = [
// \App\Http\Middleware\TrustHosts::class,
123
];
protected $comment = [
// \App\Http\Middleware\TrustHosts::class,
];
/**
* TODO: example todo
*/
protected $doc = [
];
}
18 changes: 18 additions & 0 deletions tests/fixtures/compactArray/expected.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

class CompactArray
{
protected $except = [];
protected $excepted = [];
protected $other = [];
protected $commentWithValue = [
// \App\Http\Middleware\TrustHosts::class,
123,
];
protected $comment = [];

/** TODO: example todo */
protected $doc = [];
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class NoMultilineWhitespaceAroundDoubleArrow
class EmptyArray
{
public function getArray1(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

class NoMultilineWhitespaceAroundDoubleArrow
class EmptyArray
{
public function getArray1(): array
{
Expand Down

0 comments on commit 12f2e2f

Please sign in to comment.