|
@@ -15,6 +15,7 @@ declare(strict_types=1);
|
|
|
namespace PhpCsFixer\Fixer\Whitespace;
|
|
|
|
|
|
use PhpCsFixer\AbstractFixer;
|
|
|
+use PhpCsFixer\Fixer\Indentation;
|
|
|
use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
|
|
|
use PhpCsFixer\FixerDefinition\CodeSample;
|
|
|
use PhpCsFixer\FixerDefinition\FixerDefinition;
|
|
@@ -26,11 +27,7 @@ use PhpCsFixer\Tokenizer\Tokens;
|
|
|
|
|
|
final class ArrayIndentationFixer extends AbstractFixer implements WhitespacesAwareFixerInterface
|
|
|
{
|
|
|
- /** @var int */
|
|
|
- private $newlineTokenIndexCache;
|
|
|
-
|
|
|
- /** @var int */
|
|
|
- private $newlineTokenPositionCache;
|
|
|
+ use Indentation;
|
|
|
|
|
|
/**
|
|
|
* {@inheritdoc}
|
|
@@ -66,8 +63,7 @@ final class ArrayIndentationFixer extends AbstractFixer implements WhitespacesAw
|
|
|
|
|
|
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
|
|
|
{
|
|
|
- $this->returnWithUpdateCache(0, null);
|
|
|
-
|
|
|
+ $lastIndent = '';
|
|
|
$scopes = [];
|
|
|
$previousLineInitialIndent = '';
|
|
|
$previousLineNewIndent = '';
|
|
@@ -91,12 +87,16 @@ final class ArrayIndentationFixer extends AbstractFixer implements WhitespacesAw
|
|
|
$scopes[] = [
|
|
|
'type' => 'array',
|
|
|
'end_index' => $endIndex,
|
|
|
- 'initial_indent' => $this->getLineIndentation($tokens, $index),
|
|
|
+ 'initial_indent' => $lastIndent,
|
|
|
];
|
|
|
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
+ if ($this->isNewLineToken($tokens, $index)) {
|
|
|
+ $lastIndent = $this->extractIndent($this->computeNewLineContent($tokens, $index));
|
|
|
+ }
|
|
|
+
|
|
|
if (null === $currentScope) {
|
|
|
continue;
|
|
|
}
|
|
@@ -139,6 +139,7 @@ final class ArrayIndentationFixer extends AbstractFixer implements WhitespacesAw
|
|
|
}
|
|
|
|
|
|
$tokens[$index] = new Token([T_WHITESPACE, $content]);
|
|
|
+ $lastIndent = $this->extractIndent($content);
|
|
|
|
|
|
continue;
|
|
|
}
|
|
@@ -199,74 +200,4 @@ final class ArrayIndentationFixer extends AbstractFixer implements WhitespacesAw
|
|
|
|
|
|
return $endIndex ?? $tokens->getPrevMeaningfulToken($parentScopeEndIndex);
|
|
|
}
|
|
|
-
|
|
|
- private function getLineIndentation(Tokens $tokens, int $index): string
|
|
|
- {
|
|
|
- $newlineTokenIndex = $this->getPreviousNewlineTokenIndex($tokens, $index);
|
|
|
-
|
|
|
- if (null === $newlineTokenIndex) {
|
|
|
- return '';
|
|
|
- }
|
|
|
-
|
|
|
- return $this->extractIndent($this->computeNewLineContent($tokens, $newlineTokenIndex));
|
|
|
- }
|
|
|
-
|
|
|
- private function extractIndent(string $content): string
|
|
|
- {
|
|
|
- if (Preg::match('/\R(\h*)[^\r\n]*$/D', $content, $matches)) {
|
|
|
- return $matches[1];
|
|
|
- }
|
|
|
-
|
|
|
- return '';
|
|
|
- }
|
|
|
-
|
|
|
- private function getPreviousNewlineTokenIndex(Tokens $tokens, int $startIndex): ?int
|
|
|
- {
|
|
|
- $index = $startIndex;
|
|
|
- while ($index > 0) {
|
|
|
- $index = $tokens->getPrevTokenOfKind($index, [[T_WHITESPACE], [T_INLINE_HTML]]);
|
|
|
-
|
|
|
- if ($this->newlineTokenIndexCache > $index) {
|
|
|
- return $this->returnWithUpdateCache($startIndex, $this->newlineTokenPositionCache);
|
|
|
- }
|
|
|
-
|
|
|
- if (null === $index) {
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- if ($this->isNewLineToken($tokens, $index)) {
|
|
|
- return $this->returnWithUpdateCache($startIndex, $index);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return $this->returnWithUpdateCache($startIndex, null);
|
|
|
- }
|
|
|
-
|
|
|
- private function isNewLineToken(Tokens $tokens, int $index): bool
|
|
|
- {
|
|
|
- if (!$tokens[$index]->isGivenKind([T_WHITESPACE, T_INLINE_HTML])) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- return (bool) Preg::match('/\R/', $this->computeNewLineContent($tokens, $index));
|
|
|
- }
|
|
|
-
|
|
|
- private function computeNewLineContent(Tokens $tokens, int $index): string
|
|
|
- {
|
|
|
- $content = $tokens[$index]->getContent();
|
|
|
-
|
|
|
- if (0 !== $index && $tokens[$index - 1]->equalsAny([[T_OPEN_TAG], [T_CLOSE_TAG]])) {
|
|
|
- $content = Preg::replace('/\S/', '', $tokens[$index - 1]->getContent()).$content;
|
|
|
- }
|
|
|
-
|
|
|
- return $content;
|
|
|
- }
|
|
|
-
|
|
|
- private function returnWithUpdateCache(int $index, ?int $position): ?int
|
|
|
- {
|
|
|
- $this->newlineTokenIndexCache = $index;
|
|
|
- $this->newlineTokenPositionCache = $position;
|
|
|
-
|
|
|
- return $position;
|
|
|
- }
|
|
|
}
|