-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathExpectsWithCallbackFixer.php
176 lines (146 loc) · 5.56 KB
/
ExpectsWithCallbackFixer.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
<?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 ExpectsWithCallbackFixer extends AbstractFixer
{
use IndentationFixerTrait;
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Unless there are several $this->callback() calls, there must not be a line break before the call.',
[
new CodeSample(
<<<'EOT'
<?php
public function testFoo(): void
{
$foo = $this->createMock(Foo::class);
$foo
->method("bar")
->with($this->callback(
function () {
}
));
}
EOT,
),
new CodeSample(
<<<'EOT'
<?php
public function testFoo(): void
{
$foo = $this->createMock(Foo::class);
$foo
->method("bar")
->with(
$this->callback(
function () {
}
),
$this->callback(
function () {
}
)
);
}
EOT,
),
],
);
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_STRING);
}
/**
* Must run after MultiLineLambdaFunctionArgumentsFixer,
* MultilineWhitespaceBeforeSemicolonsFixer.
*/
public function getPriority(): int
{
return 1;
}
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
for ($index = 1, $count = \count($tokens); $index < $count; ++$index) {
if (!$tokens[$index]->isGivenKind(T_STRING)) {
continue;
}
if (
'callback' !== $tokens[$index]->getContent()
|| !$tokens[$index - 1]->isGivenKind(T_OBJECT_OPERATOR)
|| !$tokens[$index + 1]->equals('(')
) {
continue;
}
if (!$start = $this->findParentMethodStart($tokens, $index - 1)) {
continue;
}
$end = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $start);
$argumentsAnalyzer = new ArgumentsAnalyzer();
$argumentsIndexes = $argumentsAnalyzer->getArguments($tokens, $start, $end);
$needsLineBreak = \count($argumentsIndexes) > 1;
$hasLineBreak = $tokens[$start + 1]->isGivenKind(T_WHITESPACE);
if ($needsLineBreak && !$hasLineBreak) {
$this->addLineBreak($tokens, $argumentsIndexes, $start, $end);
} elseif (!$needsLineBreak && $hasLineBreak) {
$this->removeLineBreak($tokens, $start, $end);
}
}
}
private function findParentMethodStart(Tokens $tokens, int $start): int
{
do {
if (!$start = (int) $tokens->getPrevTokenOfKind($start, ['('])) {
return 0;
}
} while ('with' !== $tokens[$start - 1]->getContent());
return $start;
}
private function addLineBreak(Tokens $tokens, array $argumentsIndexes, int $start, int $end): void
{
$indent = $this->getIndent($tokens, $start);
foreach ($argumentsIndexes as $argEnd) {
if ($tokens[$argEnd + 1]->equals(',') && !str_contains($tokens[$argEnd + 2]->getContent(), "\n")) {
$tokens->offsetSet($argEnd + 2, new Token([T_WHITESPACE, $indent]));
}
}
$whitespaces = $tokens->findGivenKind(T_WHITESPACE, $start + 2, $end);
foreach ($whitespaces as $pos => $whitespace) {
$content = $whitespace->getContent();
if (str_contains($content, "\n")) {
$tokens->offsetSet($pos, new Token([T_WHITESPACE, $content.' ']));
}
}
$tokens->insertAt($start + 1, new Token([T_WHITESPACE, $indent.' ']));
if ($tokens[$end + 1]->equals(')')) {
$tokens->insertAt($end + 1, new Token([T_WHITESPACE, $indent]));
}
}
private function removeLineBreak(Tokens $tokens, int $start, int $end): void
{
$whitespaces = $tokens->findGivenKind(T_WHITESPACE, $start, $end);
foreach ($whitespaces as $pos => $whitespace) {
$content = $whitespace->getContent();
if (str_contains($content, "\n")) {
$tokens->offsetSet($pos, new Token([T_WHITESPACE, substr($content, 0, -4)]));
}
}
$tokens->clearAt($start + 1);
$tokens->clearAt($end - 1);
}
}