-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMultiLineLambdaFunctionArgumentsFixer.php
166 lines (133 loc) · 5.15 KB
/
MultiLineLambdaFunctionArgumentsFixer.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
<?php
declare(strict_types=1);
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao\EasyCodingStandard\Fixer;
use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
final class MultiLineLambdaFunctionArgumentsFixer extends AbstractFixer
{
use IndentationFixerTrait;
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Multi-line lambda function arguments must be on their own line.',
[
new CodeSample(
<<<'EOT'
<?php
$array = array_map(
static function ($i) {
return $i;
},
$array
);
EOT,
),
],
);
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_FUNCTION);
}
/**
* Must run after MethodArgumentSpaceFixer.
*/
public function getPriority(): int
{
return -3;
}
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
for ($index = 1, $count = \count($tokens); $index < $count; ++$index) {
if (!$tokens[$index]->isGivenKind(T_FUNCTION)) {
continue;
}
$nextMeaningful = $tokens->getNextMeaningfulToken($index);
// Not a lambda function
if (!$tokens[$nextMeaningful]->equals('(')) {
continue;
}
$prevMeaningful = $tokens->getPrevMeaningfulToken($index);
if ($tokens[$prevMeaningful]->isGivenKind(T_STATIC)) {
$prevMeaningful = $tokens->getPrevMeaningfulToken($prevMeaningful);
}
// Not inside a method call
if (!\in_array($tokens[$prevMeaningful]->getContent(), ['(', ','], true)) {
continue;
}
// The arguments are on separate lines already
if ($this->hasNewline($tokens, $prevMeaningful + 1)) {
continue;
}
$start = $tokens->getPrevTokenOfKind($index, ['(']);
$end = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $start);
// No line-breaks required for inline lambda functions
if (!$this->isMultiLineStatement($tokens, $start, $end)) {
continue;
}
$this->fixIndentation($tokens, $start, $end, $this->getIndent($tokens, $prevMeaningful));
$index = $end;
}
}
private function hasNewline(Tokens $tokens, int $index): bool
{
return $tokens[$index]->isGivenKind(T_WHITESPACE) && str_contains($tokens[$index]->getContent(), "\n");
}
private function fixIndentation(Tokens $tokens, int $start, int &$end, string $indent): void
{
$argumentsAnalyzer = new ArgumentsAnalyzer();
$argumentsIndexes = $argumentsAnalyzer->getArguments($tokens, $start, $end);
foreach ($argumentsIndexes as $argumentStart => $argumentEnd) {
if ($tokens[$argumentStart]->isGivenKind(T_WHITESPACE)) {
++$argumentStart;
}
if ($tokens[$argumentStart]->isGivenKind([T_STATIC, T_FUNCTION])) {
$this->indentFunctionBody($tokens, $argumentStart, $argumentEnd);
}
if (!$tokens[$argumentEnd + 2]->isGivenKind(T_WHITESPACE)) {
continue;
}
if ($tokens[$argumentEnd + 1]->equals(',')) {
$tokens->offsetSet($argumentEnd + 2, new Token([T_WHITESPACE, $indent.' ']));
} elseif ($tokens[$argumentEnd + 1]->equals(')')) {
$tokens->offsetSet($argumentEnd + 2, new Token([T_WHITESPACE, substr($indent, 0, -4)]));
}
}
// Add a line-break after the opening parenthesis
if (!$tokens[$start + 1]->isGivenKind(T_WHITESPACE)) {
$tokens->insertAt($start + 1, new Token([T_WHITESPACE, $indent.' ']));
++$end;
}
// Add a line-break before the closing parenthesis
if (!$tokens[$end]->isGivenKind(T_WHITESPACE)) {
$tokens->insertAt($end, new Token([T_WHITESPACE, $indent]));
++$end;
}
}
private function indentFunctionBody(Tokens $tokens, int $argumentStart, int $argumentEnd): void
{
if (!$bodyStart = $tokens->getNextTokenOfKind($argumentStart, ['{'])) {
return;
}
$whitespaces = $tokens->findGivenKind(T_WHITESPACE, $bodyStart, $argumentEnd);
foreach ($whitespaces as $pos => $whitespace) {
$ws = $whitespace->getContent();
if (!str_contains($ws, "\n")) {
continue;
}
$tokens->offsetSet($pos, new Token([T_WHITESPACE, $ws.' ']));
}
}
}