forked from hoaproject/Devtools
/
ControlFlowStatement.php
69 lines (54 loc) · 1.88 KB
/
ControlFlowStatement.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
<?php
namespace Hoa\Devtools\Resource\PHPCsFixer\Fixer;
use Symfony\CS\AbstractFixer;
use Symfony\CS\FixerInterface;
use Symfony\CS\Tokenizer\Tokens;
use SplFileInfo;
/**
* Add a newline before `return`, `break` and `continue` if needed.
* Inspired by the `ReturnFixer` class, provided with `php-cs-fixer`.
*/
class ControlFlowStatement extends AbstractFixer
{
public function fix(\SplFileInfo $file, $content)
{
$tokens = Tokens::fromCode($content);
for ($index = 0, $limit = $tokens->count(); $index < $limit; ++$index) {
$token = $tokens[$index];
if (!$token->isGivenKind([T_RETURN, T_BREAK, T_CONTINUE])) {
continue;
}
$prevNonWhitespaceToken = $tokens[$tokens->getPrevNonWhitespace($index)];
if (!$prevNonWhitespaceToken->equalsAny([';', '}'])) {
continue;
}
$prevToken = $tokens[$index - 1];
if ($prevToken->isWhitespace()) {
$parts = explode("\n", $prevToken->getContent());
$countParts = count($parts);
if (1 === $countParts) {
$prevToken->setContent(rtrim($prevToken->getContent(), " \t") . "\n\n");
} elseif (count($parts) <= 2) {
$prevToken->setContent("\n" . $prevToken->getContent());
}
} else {
$tokens->insertAt($index, new Token([T_WHITESPACE, "\n\n"]));
++$index;
++$limit;
}
}
return $tokens->generateCode();
}
public function getDescription()
{
return 'Add a newline before `return`, `break` and `continue` if needed.';
}
public function getName()
{
return 'control_flow_statement';
}
public function getLevel()
{
return FixerInterface::CONTRIB_LEVEL;
}
}