-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
ArrayOpenerAndCloserNewlineFixer.php
199 lines (168 loc) · 6.13 KB
/
ArrayOpenerAndCloserNewlineFixer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
declare(strict_types=1);
namespace Symplify\CodingStandard\Fixer\ArrayNotation;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixer\WhitespacesFixerConfig;
use SplFileInfo;
use Symplify\CodingStandard\Enum\BlockBorderType;
use Symplify\CodingStandard\Fixer\AbstractSymplifyFixer;
use Symplify\CodingStandard\TokenRunner\Analyzer\FixerAnalyzer\ArrayAnalyzer;
use Symplify\CodingStandard\TokenRunner\Traverser\ArrayBlockInfoFinder;
use Symplify\CodingStandard\TokenRunner\ValueObject\TokenKinds;
use Symplify\CodingStandard\ValueObject\BlockInfoMetadata;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Symplify\CodingStandard\Tests\Fixer\ArrayNotation\ArrayOpenerAndCloserNewlineFixer\ArrayOpenerAndCloserNewlineFixerTest
*/
final class ArrayOpenerAndCloserNewlineFixer extends AbstractSymplifyFixer implements DocumentedRuleInterface
{
/**
* @var string
*/
private const ERROR_MESSAGE = 'Indexed PHP array opener [ and closer ] must be on own line';
public function __construct(
private readonly ArrayBlockInfoFinder $arrayBlockInfoFinder,
private readonly WhitespacesFixerConfig $whitespacesFixerConfig,
private readonly ArrayAnalyzer $arrayAnalyzer
) {
}
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(self::ERROR_MESSAGE, []);
}
/**
* Must run before
*
* @see \PhpCsFixer\Fixer\Whitespace\ArrayIndentationFixer::getPriority()
*/
public function getPriority(): int
{
return 34;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(self::ERROR_MESSAGE, [
new CodeSample(
<<<'CODE_SAMPLE'
$items = [1 => 'Hey'];
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$items = [
1 => 'Hey'
];
CODE_SAMPLE
),
]);
}
/**
* @param Tokens<Token> $tokens
*/
public function isCandidate(Tokens $tokens): bool
{
if (! $tokens->isAnyTokenKindsFound(TokenKinds::ARRAY_OPEN_TOKENS)) {
return false;
}
return $tokens->isTokenKindFound(T_DOUBLE_ARROW);
}
/**
* @param Tokens<Token> $tokens
*/
public function fix(SplFileInfo $fileInfo, Tokens $tokens): void
{
$blockInfos = $this->arrayBlockInfoFinder->findArrayOpenerBlockInfos($tokens);
$blockInfoMetadatas = [];
foreach ($blockInfos as $blockInfo) {
$blockInfoMetadatas[$blockInfo->getStart()] = new BlockInfoMetadata(BlockBorderType::OPENER, $blockInfo);
$blockInfoMetadatas[$blockInfo->getEnd()] = new BlockInfoMetadata(BlockBorderType::CLOSER, $blockInfo);
}
// sort from the highest position to the lowest, so we respect the changed tokens from bottom to the top convention
krsort($blockInfoMetadatas);
foreach ($blockInfoMetadatas as $blockInfoMetadata) {
$this->fixPositionAndType($blockInfoMetadata, $tokens);
}
}
/**
* @param Tokens<Token> $tokens
*/
private function fixPositionAndType(BlockInfoMetadata $blockInfoMetadata, Tokens $tokens): void
{
$blockInfo = $blockInfoMetadata->getBlockInfo();
if ($this->isNextTokenAlsoArrayOpener($tokens, $blockInfo->getStart())) {
return;
}
// no items
$itemCount = $this->arrayAnalyzer->getItemCount($tokens, $blockInfo);
if ($itemCount === 0) {
return;
}
if (! $this->arrayAnalyzer->isIndexedList($tokens, $blockInfo)) {
return;
}
// closer must run before the opener, as tokens as added by traversing up
if ($blockInfoMetadata->getBlockType() === BlockBorderType::CLOSER) {
$this->handleArrayCloser($tokens, $blockInfo->getEnd());
} elseif ($blockInfoMetadata->getBlockType() === BlockBorderType::OPENER) {
$this->handleArrayOpener($tokens, $blockInfo->getStart());
}
}
/**
* @param Tokens<Token> $tokens
*/
private function isNextTokenAlsoArrayOpener(Tokens $tokens, int $index): bool
{
$nextToken = $this->getNextMeaningfulToken($tokens, $index);
if (! $nextToken instanceof Token) {
return false;
}
return $nextToken->isGivenKind(TokenKinds::ARRAY_OPEN_TOKENS);
}
/**
* @param Tokens<Token> $tokens
*/
private function handleArrayCloser(Tokens $tokens, int $arrayCloserPosition): void
{
$preArrayCloserPosition = $arrayCloserPosition - 1;
$previousCloserToken = $tokens[$preArrayCloserPosition] ?? null;
if (! $previousCloserToken instanceof Token) {
return;
}
// already whitespace
if (\str_contains($previousCloserToken->getContent(), "\n")) {
return;
}
$tokens->ensureWhitespaceAtIndex($preArrayCloserPosition, 1, $this->whitespacesFixerConfig->getLineEnding());
}
/**
* @param Tokens<Token> $tokens
*/
private function handleArrayOpener(Tokens $tokens, int $arrayOpenerPosition): void
{
$postArrayOpenerPosition = $arrayOpenerPosition + 1;
$nextToken = $tokens[$postArrayOpenerPosition] ?? null;
if (! $nextToken instanceof Token) {
return;
}
// already is whitespace
if (\str_contains($nextToken->getContent(), "\n")) {
return;
}
$tokens->ensureWhitespaceAtIndex($postArrayOpenerPosition, 0, $this->whitespacesFixerConfig->getLineEnding());
}
/**
* @param Tokens<Token> $tokens
*/
private function getNextMeaningfulToken(Tokens $tokens, int $index): ?Token
{
$nextMeaningfulTokenPosition = $tokens->getNextMeaningfulToken($index);
if ($nextMeaningfulTokenPosition === null) {
return null;
}
return $tokens[$nextMeaningfulTokenPosition];
}
}