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
|
|
|
* @return $this |
19
|
|
|
*/ |
20
|
|
|
public static function createReview() { |
21
|
|
|
return new static(false); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @inheritdoc |
27
|
|
|
*/ |
28
|
|
|
public function getName() { |
29
|
|
|
return self::NAME; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public function getDescription() { |
37
|
|
|
return 'Set one empty line before class closing tag'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param File $file |
43
|
|
|
* @param Report $report |
44
|
|
|
* @void |
45
|
|
|
*/ |
46
|
4 |
|
public function process(File $file, Report $report) { |
47
|
4 |
|
$collection = \Funivan\PhpTokenizer\Collection::createFromString($file->getContent()->get()); |
48
|
4 |
|
$tokens = $this->getInvalidTokens($collection); |
49
|
|
|
|
50
|
4 |
|
$emptyLines = "\n" . str_repeat("\n", $this->getLinesNum()); |
51
|
|
|
|
52
|
|
|
|
53
|
4 |
|
foreach ($tokens as $token) { |
54
|
3 |
|
$report->addMessage($file, $this, 'Set one line before closing tag', $token->getLine()); |
55
|
|
|
|
56
|
3 |
|
$newValue = $this->getTokenNewValue($token, $emptyLines); |
57
|
3 |
|
$token->setValue($newValue); |
58
|
4 |
|
} |
59
|
|
|
|
60
|
4 |
|
$file->getContent()->set($collection->assemble()); |
61
|
4 |
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param Token $token |
66
|
|
|
* @param string $emptyLines |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
4 |
|
protected function getTokenNewValue(Token $token, $emptyLines) { |
70
|
|
|
|
71
|
3 |
|
if ($token->getType() !== T_WHITESPACE) { |
72
|
1 |
|
return $token->getValue() . $emptyLines; |
73
|
|
|
} |
74
|
|
|
|
75
|
4 |
|
$lines = explode("\n", $token->getValue()); |
76
|
2 |
|
$lineStart = current($lines); |
77
|
2 |
|
$lineEnd = end($lines); |
78
|
2 |
|
return $lineStart . $emptyLines . $lineEnd; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
} |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.