-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
4ca2988
commit 12f2e2f
Showing
8 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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,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; | ||
} | ||
} |
This file contains 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 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,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 = [ | ||
]; | ||
} |
This file contains 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,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 = []; | ||
} |
2 changes: 1 addition & 1 deletion
2
tests/fixtures/noMultilineWhitespaceAroundDoubleArrow/actual.php
This file contains 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
class NoMultilineWhitespaceAroundDoubleArrow | ||
class EmptyArray | ||
{ | ||
public function getArray1(): array | ||
{ | ||
|
This file contains 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