-
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathLineLengthFixer.php
More file actions
240 lines (205 loc) · 7.43 KB
/
Copy pathLineLengthFixer.php
File metadata and controls
240 lines (205 loc) · 7.43 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
declare(strict_types=1);
namespace Symplify\CodingStandard\Fixer\LineLength;
use Override;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
use SplFileInfo;
use Symplify\CodingStandard\Exception\ShouldNotHappenException;
use Symplify\CodingStandard\Fixer\AbstractSymplifyFixer;
use Symplify\CodingStandard\Fixer\Spacing\StandaloneLineConstructorParamFixer;
use Symplify\CodingStandard\TokenAnalyzer\FunctionCallNameMatcher;
use Symplify\CodingStandard\TokenAnalyzer\HeredocAnalyzer;
use Symplify\CodingStandard\TokenAnalyzer\Naming\MethodNameResolver;
use Symplify\CodingStandard\TokenRunner\Analyzer\FixerAnalyzer\BlockFinder;
use Symplify\CodingStandard\TokenRunner\Transformer\FixerTransformer\LineLengthTransformer;
use Symplify\CodingStandard\TokenRunner\ValueObject\BlockInfo;
/**
* @see \Symplify\CodingStandard\Tests\Fixer\LineLength\LineLengthFixer\LineLengthFixerTest
* @see \Symplify\CodingStandard\Tests\Fixer\LineLength\LineLengthFixer\ConfiguredLineLengthFixerTest
*/
final class LineLengthFixer extends AbstractSymplifyFixer implements ConfigurableFixerInterface
{
/**
* @api
*/
public const string LINE_LENGTH = 'line_length';
/**
* @api
*/
public const string BREAK_LONG_LINES = 'break_long_lines';
/**
* @api
*/
public const string INLINE_SHORT_LINES = 'inline_short_lines';
private const string ERROR_MESSAGE = 'Array items, method parameters, method call arguments, new arguments should be on same/standalone line to fit line length.';
private const int DEFAULT_LINE_LENGHT = 120;
private int $lineLength = self::DEFAULT_LINE_LENGHT;
private bool $breakLongLines = true;
private bool $inlineShortLines = true;
public function __construct(
private readonly LineLengthTransformer $lineLengthTransformer,
private readonly BlockFinder $blockFinder,
private readonly FunctionCallNameMatcher $functionCallNameMatcher,
private readonly MethodNameResolver $methodNameResolver,
private readonly HeredocAnalyzer $heredocAnalyzer,
private readonly ?StandaloneLineConstructorParamFixer $standaloneLineConstructorParamFixer = null
) {
}
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(self::ERROR_MESSAGE, []);
}
/**
* @param Tokens<Token> $tokens
*/
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isAnyTokenKindsFound([
// "["
T_ARRAY,
// "array"()
CT::T_ARRAY_SQUARE_BRACE_OPEN,
'(',
')',
// "function"
T_FUNCTION,
// "use" (...)
CT::T_USE_LAMBDA,
// "new"
T_NEW,
// "#["
T_ATTRIBUTE,
]);
}
/**
* @param Tokens<Token> $tokens
*/
public function fix(SplFileInfo $fileInfo, Tokens $tokens): void
{
// function arguments, function call parameters, lambda use()
for ($position = count($tokens) - 1; $position >= 0; --$position) {
/** @var Token $token */
$token = $tokens[$position];
if ($token->equals(')')) {
$this->processMethodCall($tokens, $position);
continue;
}
// opener
if ($token->isGivenKind([T_ATTRIBUTE, T_FUNCTION, CT::T_USE_LAMBDA, T_NEW])) {
$this->processFunctionOrArray($tokens, $position);
continue;
}
// closer
if (! $token->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_CLOSE)) {
continue;
}
if (! $token->isArray()) {
continue;
}
$this->processFunctionOrArray($tokens, $position);
}
}
/**
* Must run before
*
* @see \PhpCsFixer\Fixer\ArrayNotation\TrimArraySpacesFixer::getPriority()
*/
#[Override]
public function getPriority(): int
{
return 5;
}
/**
* @param array<string, mixed> $configuration
*/
public function configure(array $configuration): void
{
$this->lineLength = $configuration[self::LINE_LENGTH] ?? self::DEFAULT_LINE_LENGHT;
$this->breakLongLines = $configuration[self::BREAK_LONG_LINES] ?? true;
$this->inlineShortLines = $configuration[self::INLINE_SHORT_LINES] ?? true;
}
public function getConfigurationDefinition(): FixerConfigurationResolverInterface
{
throw new ShouldNotHappenException();
}
/**
* @param Tokens<Token> $tokens
*/
private function processMethodCall(Tokens $tokens, int $position): void
{
$methodNamePosition = $this->functionCallNameMatcher->matchName($tokens, $position);
if ($methodNamePosition === null) {
return;
}
$blockInfo = $this->blockFinder->findInTokensByPositionAndContent($tokens, $methodNamePosition, '(');
if (! $blockInfo instanceof BlockInfo) {
return;
}
// has comments => dangerous to change: https://github.com/symplify/symplify/issues/973
$comments = $tokens->findGivenKind(T_COMMENT, $blockInfo->getStart(), $blockInfo->getEnd());
if ($comments !== []) {
return;
}
$this->lineLengthTransformer->fixStartPositionToEndPosition(
$blockInfo,
$tokens,
$this->lineLength,
$this->breakLongLines,
$this->inlineShortLines
);
}
/**
* @param Tokens<Token> $tokens
*/
private function processFunctionOrArray(Tokens $tokens, int $position): void
{
$blockInfo = $this->blockFinder->findInTokensByEdge($tokens, $position);
if (! $blockInfo instanceof BlockInfo) {
return;
}
// @todo is __construct() class method and is newline parma enabled? → skip it
if ($this->standaloneLineConstructorParamFixer && $this->methodNameResolver->isMethodName(
$tokens,
$position,
'__construct'
)) {
return;
}
if ($this->shouldSkip($tokens, $blockInfo)) {
return;
}
$this->lineLengthTransformer->fixStartPositionToEndPosition(
$blockInfo,
$tokens,
$this->lineLength,
$this->breakLongLines,
$this->inlineShortLines
);
}
/**
* @param Tokens<Token> $tokens
*/
private function shouldSkip(Tokens $tokens, BlockInfo $blockInfo): bool
{
// no items inside => skip
if ($blockInfo->getEnd() - $blockInfo->getStart() <= 1) {
return true;
}
if ($this->heredocAnalyzer->isHerenowDoc($tokens, $blockInfo)) {
return true;
}
// is array with indexed values "=>"
$doubleArrowTokens = $tokens->findGivenKind(T_DOUBLE_ARROW, $blockInfo->getStart(), $blockInfo->getEnd());
if ($doubleArrowTokens !== []) {
return true;
}
// has comments => dangerous to change: https://github.com/symplify/symplify/issues/973
return (bool) $tokens->findGivenKind(T_COMMENT, $blockInfo->getStart(), $blockInfo->getEnd());
}
}