Repository navigation
Expand file tree
/
Copy pathSingleLineConfigureCommandFixer.php
More file actions
152 lines (122 loc) · 5.34 KB
/
Copy pathSingleLineConfigureCommandFixer.php
File metadata and controls
152 lines (122 loc) · 5.34 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
<?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\Token;
use PhpCsFixer\Tokenizer\Tokens;
final class SingleLineConfigureCommandFixer extends AbstractFixer
{
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Defining command arguments and options must be done in a single line.',
[
new CodeSample(
<<<'EOT'
<?php
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
class SomeCommand extends Command
{
protected function configure(): void
{
$this
->addArgument("foo", InputArgument::REQUIRED, "The argument")
->addOption("bar", null, InputOption::VALUE_NONE, "The option");
}
}
EOT,
),
],
);
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isAllTokenKindsFound([T_CLASS, T_FUNCTION, T_OBJECT_OPERATOR]);
}
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
for ($index = 1, $count = \count($tokens); $index < $count; ++$index) {
switch (true) {
case $tokens[$index]->isGivenKind(T_CLASS):
$prev = $tokens->getPrevMeaningfulToken($index);
if (null !== $prev && $tokens[$prev]->isGivenKind(T_PAAMAYIM_NEKUDOTAYIM)) {
break;
}
$next = $tokens->getNextMeaningfulToken($index);
if (null === $next) {
break;
}
// Skip the class if it is not a command
if (!str_ends_with($tokens[$next]->getContent(), 'Command')) {
$bodyStart = $tokens->getNextTokenOfKind($next, ['{']);
if (null === $bodyStart) {
break;
}
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_BRACE, $bodyStart);
}
break;
case $tokens[$index]->isGivenKind(T_FUNCTION):
$next = $tokens->getNextMeaningfulToken($index);
if (null === $next) {
break;
}
// Skip the method if it is not the configure() method
if ('configure' !== $tokens[$next]->getContent()) {
$argsStart = $tokens->getNextTokenOfKind($next, ['(']);
if (null === $argsStart) {
break;
}
$argsEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS, $argsStart);
$bodyStart = $tokens->getNextTokenOfKind($argsEnd, ['{', ';']);
if (null === $bodyStart) {
break;
}
if ($tokens[$bodyStart]->equals('{')) {
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_BRACE, $bodyStart);
} else {
$index = $bodyStart;
}
}
break;
case $tokens[$index]->isGivenKind(T_OBJECT_OPERATOR):
$next = $tokens->getNextMeaningfulToken($index);
if (null === $next) {
break;
}
if (!\in_array($tokens[$next]->getContent(), ['addArgument', 'addOption'], true)) {
continue 2;
}
$blockStart = $next + 1;
$blockEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS, $blockStart);
// Do not remove the line breaks that terminate comments.
if (array_filter($tokens->findGivenKind([T_COMMENT, T_DOC_COMMENT], $blockStart + 1, $blockEnd))) {
$index = $blockEnd;
continue 2;
}
if ($tokens[$blockStart + 1]->isGivenKind(T_WHITESPACE)) {
$tokens->clearAt(++$blockStart);
}
if ($tokens[$blockEnd - 1]->isGivenKind(T_WHITESPACE)) {
$tokens->clearAt(--$blockEnd);
}
for ($i = $blockStart + 1; $i < $blockEnd; ++$i) {
if ($tokens[$i]->isWhitespace()) {
$tokens->offsetSet($i, new Token([T_WHITESPACE, ' ']));
}
}
}
}
}
}