-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
StandaloneLineInMultilineArrayFixer.php
131 lines (113 loc) · 3.98 KB
/
StandaloneLineInMultilineArrayFixer.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
<?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 SplFileInfo;
use Symplify\CodingStandard\Fixer\AbstractSymplifyFixer;
use Symplify\CodingStandard\TokenRunner\Analyzer\FixerAnalyzer\BlockFinder;
use Symplify\CodingStandard\TokenRunner\Enum\LineKind;
use Symplify\CodingStandard\TokenRunner\Transformer\FixerTransformer\TokensNewliner;
use Symplify\CodingStandard\TokenRunner\ValueObject\BlockInfo;
use Symplify\CodingStandard\TokenRunner\ValueObject\TokenKinds;
use Symplify\CodingStandard\TokenRunner\Wrapper\FixerWrapper\ArrayWrapperFactory;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Symplify\CodingStandard\Tests\Fixer\ArrayNotation\StandaloneLineInMultilineArrayFixer\StandaloneLineInMultilineArrayFixerTest
*/
final class StandaloneLineInMultilineArrayFixer extends AbstractSymplifyFixer implements DocumentedRuleInterface
{
/**
* @var string
*/
private const ERROR_MESSAGE = 'Indexed arrays must have 1 item per line';
public function __construct(
private readonly ArrayWrapperFactory $arrayWrapperFactory,
private readonly TokensNewliner $tokensNewliner,
private readonly BlockFinder $blockFinder
) {
}
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(self::ERROR_MESSAGE, []);
}
/**
* Must run before
*
* @see \PhpCsFixer\Fixer\ControlStructure\TrailingCommaInMultilineFixer::getPriority()
*/
public function getPriority(): int
{
return 5;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(self::ERROR_MESSAGE, [
new CodeSample(
<<<'CODE_SAMPLE'
$friends = [1 => 'Peter', 2 => 'Paul'];
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$friends = [
1 => 'Peter',
2 => 'Paul'
];
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
{
foreach ($tokens as $index => $token) {
if (! $token->isGivenKind(TokenKinds::ARRAY_OPEN_TOKENS)) {
continue;
}
$blockInfo = $this->blockFinder->findInTokensByEdge($tokens, $index);
if (! $blockInfo instanceof BlockInfo) {
continue;
}
if ($this->shouldSkipNestedArrayValue($tokens, $blockInfo)) {
return;
}
$this->tokensNewliner->breakItems($blockInfo, $tokens, LineKind::ARRAYS);
}
}
/**
* @param Tokens<Token> $tokens
*/
private function shouldSkipNestedArrayValue(Tokens $tokens, BlockInfo $blockInfo): bool
{
$arrayWrapper = $this->arrayWrapperFactory->createFromTokensAndBlockInfo($tokens, $blockInfo);
if (! $arrayWrapper->isAssociativeArray()) {
return true;
}
if ($arrayWrapper->getItemCount() === 1 && ! $arrayWrapper->isFirstItemArray()) {
$previousTokenPosition = $tokens->getPrevMeaningfulToken($blockInfo->getStart());
if ($previousTokenPosition === null) {
return false;
}
/** @var Token $previousToken */
$previousToken = $tokens[$previousTokenPosition];
return ! $previousToken->isGivenKind(T_DOUBLE_ARROW);
}
return false;
}
}