-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathNoSemicolonAfterShortEchoTagFixer.php
58 lines (46 loc) · 1.44 KB
/
NoSemicolonAfterShortEchoTagFixer.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
<?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\Tokens;
final class NoSemicolonAfterShortEchoTagFixer extends AbstractFixer
{
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Remove the semicolon after a short echo tag `<?= $this->foo; ?>` instruction.',
[new CodeSample('<?= $this->foo; ?>\n')],
);
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_OPEN_TAG_WITH_ECHO);
}
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
$hasShortEchoTag = false;
foreach ($tokens as $index => $token) {
if ($token->isGivenKind(T_OPEN_TAG_WITH_ECHO)) {
$hasShortEchoTag = true;
}
if (!$token->isGivenKind(T_CLOSE_TAG)) {
continue;
}
$prev = $tokens->getPrevMeaningfulToken($index);
if ($hasShortEchoTag && $tokens[$prev]->equalsAny([';'])) {
$tokens->clearAt($prev);
}
$hasShortEchoTag = false;
}
}
}