Missing dependancies
This commit is contained in:
+151
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of PHP CS Fixer.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace PhpCsFixer\Fixer\ArrayNotation;
|
||||
|
||||
use PhpCsFixer\AbstractFixer;
|
||||
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
|
||||
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
|
||||
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
|
||||
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
|
||||
use PhpCsFixer\FixerDefinition\CodeSample;
|
||||
use PhpCsFixer\FixerDefinition\FixerDefinition;
|
||||
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
|
||||
use PhpCsFixer\Tokenizer\CT;
|
||||
use PhpCsFixer\Tokenizer\Token;
|
||||
use PhpCsFixer\Tokenizer\Tokens;
|
||||
|
||||
/**
|
||||
* @author Gregor Harlan <gharlan@web.de>
|
||||
* @author Sebastiaan Stok <s.stok@rollerscapes.net>
|
||||
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*/
|
||||
final class ArraySyntaxFixer extends AbstractFixer implements ConfigurableFixerInterface
|
||||
{
|
||||
/**
|
||||
* @var null|int
|
||||
*/
|
||||
private $candidateTokenKind;
|
||||
|
||||
/**
|
||||
* @var null|string
|
||||
*/
|
||||
private $fixCallback;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function configure(array $configuration): void
|
||||
{
|
||||
parent::configure($configuration);
|
||||
|
||||
$this->resolveCandidateTokenKind();
|
||||
$this->resolveFixCallback();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefinition(): FixerDefinitionInterface
|
||||
{
|
||||
return new FixerDefinition(
|
||||
'PHP arrays should be declared using the configured syntax.',
|
||||
[
|
||||
new CodeSample(
|
||||
"<?php\narray(1,2);\n"
|
||||
),
|
||||
new CodeSample(
|
||||
"<?php\n[1,2];\n",
|
||||
['syntax' => 'long']
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Must run before BinaryOperatorSpacesFixer, TernaryOperatorSpacesFixer.
|
||||
*/
|
||||
public function getPriority(): int
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isCandidate(Tokens $tokens): bool
|
||||
{
|
||||
return $tokens->isTokenKindFound($this->candidateTokenKind);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
|
||||
{
|
||||
$callback = $this->fixCallback;
|
||||
|
||||
for ($index = $tokens->count() - 1; 0 <= $index; --$index) {
|
||||
if ($tokens[$index]->isGivenKind($this->candidateTokenKind)) {
|
||||
$this->{$callback}($tokens, $index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
|
||||
{
|
||||
return new FixerConfigurationResolver([
|
||||
(new FixerOptionBuilder('syntax', 'Whether to use the `long` or `short` array syntax.'))
|
||||
->setAllowedValues(['long', 'short'])
|
||||
->setDefault('short')
|
||||
->getOption(),
|
||||
]);
|
||||
}
|
||||
|
||||
private function fixToLongArraySyntax(Tokens $tokens, int $index): void
|
||||
{
|
||||
$closeIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $index);
|
||||
|
||||
$tokens[$index] = new Token('(');
|
||||
$tokens[$closeIndex] = new Token(')');
|
||||
|
||||
$tokens->insertAt($index, new Token([T_ARRAY, 'array']));
|
||||
}
|
||||
|
||||
private function fixToShortArraySyntax(Tokens $tokens, int $index): void
|
||||
{
|
||||
$openIndex = $tokens->getNextTokenOfKind($index, ['(']);
|
||||
$closeIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openIndex);
|
||||
|
||||
$tokens[$openIndex] = new Token([CT::T_ARRAY_SQUARE_BRACE_OPEN, '[']);
|
||||
$tokens[$closeIndex] = new Token([CT::T_ARRAY_SQUARE_BRACE_CLOSE, ']']);
|
||||
|
||||
$tokens->clearTokenAndMergeSurroundingWhitespace($index);
|
||||
}
|
||||
|
||||
private function resolveFixCallback(): void
|
||||
{
|
||||
$this->fixCallback = sprintf('fixTo%sArraySyntax', ucfirst($this->configuration['syntax']));
|
||||
}
|
||||
|
||||
private function resolveCandidateTokenKind(): void
|
||||
{
|
||||
$this->candidateTokenKind = 'long' === $this->configuration['syntax'] ? CT::T_ARRAY_SQUARE_BRACE_OPEN : T_ARRAY;
|
||||
}
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of PHP CS Fixer.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace PhpCsFixer\Fixer\ArrayNotation;
|
||||
|
||||
use PhpCsFixer\AbstractFixer;
|
||||
use PhpCsFixer\FixerDefinition\CodeSample;
|
||||
use PhpCsFixer\FixerDefinition\FixerDefinition;
|
||||
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
|
||||
use PhpCsFixer\Tokenizer\Token;
|
||||
use PhpCsFixer\Tokenizer\Tokens;
|
||||
|
||||
/**
|
||||
* @author Carlos Cirello <carlos.cirello.nl@gmail.com>
|
||||
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
* @author Graham Campbell <hello@gjcampbell.co.uk>
|
||||
*/
|
||||
final class NoMultilineWhitespaceAroundDoubleArrowFixer extends AbstractFixer
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefinition(): FixerDefinitionInterface
|
||||
{
|
||||
return new FixerDefinition(
|
||||
'Operator `=>` should not be surrounded by multi-line whitespaces.',
|
||||
[new CodeSample("<?php\n\$a = array(1\n\n=> 2);\n")]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Must run before BinaryOperatorSpacesFixer, MethodArgumentSpaceFixer, TrailingCommaInMultilineFixer.
|
||||
*/
|
||||
public function getPriority(): int
|
||||
{
|
||||
return 31;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isCandidate(Tokens $tokens): bool
|
||||
{
|
||||
return $tokens->isTokenKindFound(T_DOUBLE_ARROW);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
|
||||
{
|
||||
foreach ($tokens as $index => $token) {
|
||||
if (!$token->isGivenKind(T_DOUBLE_ARROW)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$tokens[$index - 2]->isComment() || str_starts_with($tokens[$index - 2]->getContent(), '/*')) {
|
||||
$this->fixWhitespace($tokens, $index - 1);
|
||||
}
|
||||
|
||||
// do not move anything about if there is a comment following the whitespace
|
||||
if (!$tokens[$index + 2]->isComment()) {
|
||||
$this->fixWhitespace($tokens, $index + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function fixWhitespace(Tokens $tokens, int $index): void
|
||||
{
|
||||
$token = $tokens[$index];
|
||||
|
||||
if ($token->isWhitespace() && !$token->isWhitespace(" \t")) {
|
||||
$tokens[$index] = new Token([T_WHITESPACE, rtrim($token->getContent()).' ']);
|
||||
}
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of PHP CS Fixer.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace PhpCsFixer\Fixer\ArrayNotation;
|
||||
|
||||
use PhpCsFixer\AbstractProxyFixer;
|
||||
use PhpCsFixer\Fixer\Basic\NoTrailingCommaInSinglelineFixer;
|
||||
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
|
||||
use PhpCsFixer\FixerDefinition\CodeSample;
|
||||
use PhpCsFixer\FixerDefinition\FixerDefinition;
|
||||
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*
|
||||
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
* @author Sebastiaan Stok <s.stok@rollerscapes.net>
|
||||
*/
|
||||
final class NoTrailingCommaInSinglelineArrayFixer extends AbstractProxyFixer implements DeprecatedFixerInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefinition(): FixerDefinitionInterface
|
||||
{
|
||||
return new FixerDefinition(
|
||||
'PHP single-line arrays should not have trailing comma.',
|
||||
[new CodeSample("<?php\n\$a = array('sample', );\n")]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSuccessorsNames(): array
|
||||
{
|
||||
return array_keys($this->proxyFixers);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function createProxyFixers(): array
|
||||
{
|
||||
$fixer = new NoTrailingCommaInSinglelineFixer();
|
||||
$fixer->configure(['elements' => ['array']]);
|
||||
|
||||
return [$fixer];
|
||||
}
|
||||
}
|
||||
+155
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of PHP CS Fixer.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace PhpCsFixer\Fixer\ArrayNotation;
|
||||
|
||||
use PhpCsFixer\AbstractFixer;
|
||||
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
|
||||
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
|
||||
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
|
||||
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
|
||||
use PhpCsFixer\FixerDefinition\CodeSample;
|
||||
use PhpCsFixer\FixerDefinition\FixerDefinition;
|
||||
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
|
||||
use PhpCsFixer\FixerDefinition\VersionSpecification;
|
||||
use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
|
||||
use PhpCsFixer\Tokenizer\CT;
|
||||
use PhpCsFixer\Tokenizer\Tokens;
|
||||
|
||||
/**
|
||||
* @author Adam Marczuk <adam@marczuk.info>
|
||||
*/
|
||||
final class NoWhitespaceBeforeCommaInArrayFixer extends AbstractFixer implements ConfigurableFixerInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefinition(): FixerDefinitionInterface
|
||||
{
|
||||
return new FixerDefinition(
|
||||
'In array declaration, there MUST NOT be a whitespace before each comma.',
|
||||
[
|
||||
new CodeSample("<?php \$x = array(1 , \"2\");\n"),
|
||||
new VersionSpecificCodeSample(
|
||||
<<<'PHP'
|
||||
<?php
|
||||
$x = [<<<EOD
|
||||
foo
|
||||
EOD
|
||||
, 'bar'
|
||||
];
|
||||
|
||||
PHP,
|
||||
new VersionSpecification(70300),
|
||||
['after_heredoc' => true]
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isCandidate(Tokens $tokens): bool
|
||||
{
|
||||
return $tokens->isAnyTokenKindsFound([T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
|
||||
{
|
||||
for ($index = $tokens->count() - 1; $index > 0; --$index) {
|
||||
if ($tokens[$index]->isGivenKind([T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN])) {
|
||||
$this->fixSpacing($index, $tokens);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
|
||||
{
|
||||
return new FixerConfigurationResolver([
|
||||
(new FixerOptionBuilder('after_heredoc', 'Whether the whitespace between heredoc end and comma should be removed.'))
|
||||
->setAllowedTypes(['bool'])
|
||||
->setDefault(false)
|
||||
->getOption(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to fix spacing in array declaration.
|
||||
*/
|
||||
private function fixSpacing(int $index, Tokens $tokens): void
|
||||
{
|
||||
if ($tokens[$index]->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_OPEN)) {
|
||||
$startIndex = $index;
|
||||
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $startIndex);
|
||||
} else {
|
||||
$startIndex = $tokens->getNextTokenOfKind($index, ['(']);
|
||||
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startIndex);
|
||||
}
|
||||
|
||||
for ($i = $endIndex - 1; $i > $startIndex; --$i) {
|
||||
$i = $this->skipNonArrayElements($i, $tokens);
|
||||
$currentToken = $tokens[$i];
|
||||
$prevIndex = $tokens->getPrevNonWhitespace($i - 1);
|
||||
|
||||
if (
|
||||
$currentToken->equals(',') && !$tokens[$prevIndex]->isComment()
|
||||
&& (true === $this->configuration['after_heredoc'] || !$tokens[$prevIndex]->isGivenKind(T_END_HEREDOC))
|
||||
) {
|
||||
$tokens->removeLeadingWhitespace($i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to move index over the non-array elements like function calls or function declarations.
|
||||
*/
|
||||
private function skipNonArrayElements(int $index, Tokens $tokens): int
|
||||
{
|
||||
if ($tokens[$index]->equals('}')) {
|
||||
return $tokens->findBlockStart(Tokens::BLOCK_TYPE_CURLY_BRACE, $index);
|
||||
}
|
||||
|
||||
if ($tokens[$index]->equals(')')) {
|
||||
$startIndex = $tokens->findBlockStart(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);
|
||||
$startIndex = $tokens->getPrevMeaningfulToken($startIndex);
|
||||
if (!$tokens[$startIndex]->isGivenKind([T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN])) {
|
||||
return $startIndex;
|
||||
}
|
||||
}
|
||||
|
||||
if ($tokens[$index]->equals(',') && $this->commaIsPartOfImplementsList($index, $tokens)) {
|
||||
--$index;
|
||||
}
|
||||
|
||||
return $index;
|
||||
}
|
||||
|
||||
private function commaIsPartOfImplementsList(int $index, Tokens $tokens): bool
|
||||
{
|
||||
do {
|
||||
$index = $tokens->getPrevMeaningfulToken($index);
|
||||
|
||||
$current = $tokens[$index];
|
||||
} while ($current->isGivenKind(T_STRING) || $current->equals(','));
|
||||
|
||||
return $current->isGivenKind(T_IMPLEMENTS);
|
||||
}
|
||||
}
|
||||
Vendored
+62
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of PHP CS Fixer.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace PhpCsFixer\Fixer\ArrayNotation;
|
||||
|
||||
use PhpCsFixer\AbstractFixer;
|
||||
use PhpCsFixer\FixerDefinition\CodeSample;
|
||||
use PhpCsFixer\FixerDefinition\FixerDefinition;
|
||||
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
|
||||
use PhpCsFixer\Tokenizer\CT;
|
||||
use PhpCsFixer\Tokenizer\Token;
|
||||
use PhpCsFixer\Tokenizer\Tokens;
|
||||
|
||||
/**
|
||||
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*/
|
||||
final class NormalizeIndexBraceFixer extends AbstractFixer
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefinition(): FixerDefinitionInterface
|
||||
{
|
||||
return new FixerDefinition(
|
||||
'Array index should always be written by using square braces.',
|
||||
[new CodeSample("<?php\necho \$sample{\$index};\n")]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isCandidate(Tokens $tokens): bool
|
||||
{
|
||||
return $tokens->isTokenKindFound(CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
|
||||
{
|
||||
foreach ($tokens as $index => $token) {
|
||||
if ($token->isGivenKind(CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN)) {
|
||||
$tokens[$index] = new Token('[');
|
||||
} elseif ($token->isGivenKind(CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE)) {
|
||||
$tokens[$index] = new Token(']');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of PHP CS Fixer.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace PhpCsFixer\Fixer\ArrayNotation;
|
||||
|
||||
use PhpCsFixer\AbstractFixer;
|
||||
use PhpCsFixer\FixerDefinition\CodeSample;
|
||||
use PhpCsFixer\FixerDefinition\FixerDefinition;
|
||||
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
|
||||
use PhpCsFixer\Tokenizer\CT;
|
||||
use PhpCsFixer\Tokenizer\Tokens;
|
||||
|
||||
/**
|
||||
* @author Jared Henderson <jared@netrivet.com>
|
||||
*/
|
||||
final class TrimArraySpacesFixer extends AbstractFixer
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefinition(): FixerDefinitionInterface
|
||||
{
|
||||
return new FixerDefinition(
|
||||
'Arrays should be formatted like function/method arguments, without leading or trailing single line space.',
|
||||
[new CodeSample("<?php\n\$sample = array( );\n\$sample = array( 'a', 'b' );\n")]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isCandidate(Tokens $tokens): bool
|
||||
{
|
||||
return $tokens->isAnyTokenKindsFound([T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
|
||||
{
|
||||
for ($index = 0, $c = $tokens->count(); $index < $c; ++$index) {
|
||||
if ($tokens[$index]->isGivenKind([T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN])) {
|
||||
self::fixArray($tokens, $index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to trim leading/trailing whitespace within single line arrays.
|
||||
*/
|
||||
private static function fixArray(Tokens $tokens, int $index): void
|
||||
{
|
||||
$startIndex = $index;
|
||||
|
||||
if ($tokens[$startIndex]->isGivenKind(T_ARRAY)) {
|
||||
$startIndex = $tokens->getNextMeaningfulToken($startIndex);
|
||||
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startIndex);
|
||||
} else {
|
||||
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $startIndex);
|
||||
}
|
||||
|
||||
$nextIndex = $startIndex + 1;
|
||||
$nextToken = $tokens[$nextIndex];
|
||||
$nextNonWhitespaceIndex = $tokens->getNextNonWhitespace($startIndex);
|
||||
$nextNonWhitespaceToken = $tokens[$nextNonWhitespaceIndex];
|
||||
$tokenAfterNextNonWhitespaceToken = $tokens[$nextNonWhitespaceIndex + 1];
|
||||
|
||||
$prevIndex = $endIndex - 1;
|
||||
$prevToken = $tokens[$prevIndex];
|
||||
$prevNonWhitespaceIndex = $tokens->getPrevNonWhitespace($endIndex);
|
||||
$prevNonWhitespaceToken = $tokens[$prevNonWhitespaceIndex];
|
||||
|
||||
if (
|
||||
$nextToken->isWhitespace(" \t")
|
||||
&& (
|
||||
!$nextNonWhitespaceToken->isComment()
|
||||
|| $nextNonWhitespaceIndex === $prevNonWhitespaceIndex
|
||||
|| $tokenAfterNextNonWhitespaceToken->isWhitespace(" \t")
|
||||
|| str_starts_with($nextNonWhitespaceToken->getContent(), '/*')
|
||||
)
|
||||
) {
|
||||
$tokens->clearAt($nextIndex);
|
||||
}
|
||||
|
||||
if (
|
||||
$prevToken->isWhitespace(" \t")
|
||||
&& !$prevNonWhitespaceToken->equals(',')
|
||||
) {
|
||||
$tokens->clearAt($prevIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
+149
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of PHP CS Fixer.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
namespace PhpCsFixer\Fixer\ArrayNotation;
|
||||
|
||||
use PhpCsFixer\AbstractFixer;
|
||||
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
|
||||
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
|
||||
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
|
||||
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
|
||||
use PhpCsFixer\FixerDefinition\CodeSample;
|
||||
use PhpCsFixer\FixerDefinition\FixerDefinition;
|
||||
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
|
||||
use PhpCsFixer\Preg;
|
||||
use PhpCsFixer\Tokenizer\CT;
|
||||
use PhpCsFixer\Tokenizer\Token;
|
||||
use PhpCsFixer\Tokenizer\Tokens;
|
||||
|
||||
/**
|
||||
* @author Adam Marczuk <adam@marczuk.info>
|
||||
*/
|
||||
final class WhitespaceAfterCommaInArrayFixer extends AbstractFixer implements ConfigurableFixerInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDefinition(): FixerDefinitionInterface
|
||||
{
|
||||
return new FixerDefinition(
|
||||
'In array declaration, there MUST be a whitespace after each comma.',
|
||||
[
|
||||
new CodeSample("<?php\n\$sample = array(1,'a',\$b,);\n"),
|
||||
new CodeSample("<?php\n\$sample = [1,2, 3, 4, 5];\n", ['ensure_single_space' => true]),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isCandidate(Tokens $tokens): bool
|
||||
{
|
||||
return $tokens->isAnyTokenKindsFound([T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
|
||||
{
|
||||
return new FixerConfigurationResolver([
|
||||
(new FixerOptionBuilder('ensure_single_space', 'If there are only horizontal whitespaces after the comma then ensure it is a single space.'))
|
||||
->setAllowedTypes(['bool'])
|
||||
->setDefault(false)
|
||||
->getOption(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
|
||||
{
|
||||
$tokensToInsert = [];
|
||||
|
||||
for ($index = $tokens->count() - 1; $index >= 0; --$index) {
|
||||
if (!$tokens[$index]->isGivenKind([T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($tokens[$index]->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_OPEN)) {
|
||||
$startIndex = $index;
|
||||
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $startIndex);
|
||||
} else {
|
||||
$startIndex = $tokens->getNextTokenOfKind($index, ['(']);
|
||||
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $startIndex);
|
||||
}
|
||||
|
||||
for ($i = $endIndex - 1; $i > $startIndex; --$i) {
|
||||
$i = $this->skipNonArrayElements($i, $tokens);
|
||||
if (!$tokens[$i]->equals(',')) {
|
||||
continue;
|
||||
}
|
||||
if (!$tokens[$i + 1]->isWhitespace()) {
|
||||
$tokensToInsert[$i + 1] = new Token([T_WHITESPACE, ' ']);
|
||||
} elseif (
|
||||
$this->configuration['ensure_single_space']
|
||||
&& ' ' !== $tokens[$i + 1]->getContent()
|
||||
&& 1 === Preg::match('/^\h+$/', $tokens[$i + 1]->getContent())
|
||||
&& (!$tokens[$i + 2]->isComment() || 1 === Preg::match('/^\h+$/', $tokens[$i + 3]->getContent()))
|
||||
) {
|
||||
$tokens[$i + 1] = new Token([T_WHITESPACE, ' ']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ([] !== $tokensToInsert) {
|
||||
$tokens->insertSlices($tokensToInsert);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to move index over the non-array elements like function calls or function declarations.
|
||||
*
|
||||
* @return int New index
|
||||
*/
|
||||
private function skipNonArrayElements(int $index, Tokens $tokens): int
|
||||
{
|
||||
if ($tokens[$index]->equals('}')) {
|
||||
return $tokens->findBlockStart(Tokens::BLOCK_TYPE_CURLY_BRACE, $index);
|
||||
}
|
||||
|
||||
if ($tokens[$index]->equals(')')) {
|
||||
$startIndex = $tokens->findBlockStart(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);
|
||||
$startIndex = $tokens->getPrevMeaningfulToken($startIndex);
|
||||
if (!$tokens[$startIndex]->isGivenKind([T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN])) {
|
||||
return $startIndex;
|
||||
}
|
||||
}
|
||||
|
||||
if ($tokens[$index]->equals(',') && $this->commaIsPartOfImplementsList($index, $tokens)) {
|
||||
--$index;
|
||||
}
|
||||
|
||||
return $index;
|
||||
}
|
||||
|
||||
private function commaIsPartOfImplementsList(int $index, Tokens $tokens): bool
|
||||
{
|
||||
do {
|
||||
$index = $tokens->getPrevMeaningfulToken($index);
|
||||
|
||||
$current = $tokens[$index];
|
||||
} while ($current->isGivenKind(T_STRING) || $current->equals(','));
|
||||
|
||||
return $current->isGivenKind(T_IMPLEMENTS);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user