1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Funivan\Cs\Tools\Php\LineBeforeClassEnd; |
4
|
|
|
|
5
|
|
|
use Funivan\Cs\Fs\File; |
6
|
|
|
use Funivan\Cs\Report\Report; |
7
|
|
|
use Funivan\PhpTokenizer\Token; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
class LineBeforeClassEndFixer extends AbstractLineBeforeClassEnd { |
13
|
|
|
|
14
|
|
|
const NAME = 'php_line_before_class_end_fixer'; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @inheritdoc |
19
|
|
|
*/ |
20
|
|
|
public function getName() { |
21
|
|
|
return self::NAME; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
public function getDescription() { |
29
|
|
|
return 'Set one empty line before class closing tag'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param File $file |
35
|
|
|
* @param Report $report |
36
|
|
|
* @void |
37
|
|
|
*/ |
38
|
4 |
|
public function process(File $file, Report $report) { |
39
|
4 |
|
$collection = \Funivan\PhpTokenizer\Collection::createFromString($file->getContent()->get()); |
40
|
4 |
|
$tokens = $this->getInvalidTokens($collection); |
41
|
|
|
|
42
|
4 |
|
$emptyLines = "\n" . str_repeat("\n", $this->getLinesNum()); |
43
|
|
|
|
44
|
|
|
|
45
|
4 |
|
foreach ($tokens as $token) { |
46
|
3 |
|
$report->addMessage($file, $this, 'Set one line before closing tag', $token->getLine()); |
47
|
|
|
|
48
|
3 |
|
$newValue = $this->getTokenNewValue($token, $emptyLines); |
49
|
3 |
|
$token->setValue($newValue); |
50
|
4 |
|
} |
51
|
|
|
|
52
|
4 |
|
$file->getContent()->set($collection->assemble()); |
53
|
4 |
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param Token $token |
58
|
|
|
* @param string $emptyLines |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
3 |
|
protected function getTokenNewValue(Token $token, $emptyLines) { |
62
|
|
|
|
63
|
3 |
|
if ($token->getType() !== T_WHITESPACE) { |
64
|
1 |
|
return $token->getValue() . $emptyLines; |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
$lines = explode("\n", $token->getValue()); |
68
|
2 |
|
$lineStart = current($lines); |
69
|
2 |
|
$lineEnd = end($lines); |
70
|
2 |
|
return $lineStart . $emptyLines . $lineEnd; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
} |