|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Inspector\Analysis\Checker\Misc; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser\Node; |
|
6
|
|
|
use PhpParser\Node\Stmt\Class_; |
|
7
|
|
|
use PhpParser\Node\Stmt\Function_; |
|
8
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
|
9
|
|
|
use Inspector\Misc\ParametersInterface; |
|
10
|
|
|
use Inspector\Analysis\Checker\CheckerInterface; |
|
11
|
|
|
use Inspector\Analysis\Exception\ClassTooLongException; |
|
12
|
|
|
use Inspector\Analysis\Exception\MethodTooLongException; |
|
13
|
|
|
use Inspector\Analysis\Exception\FunctionTooLongException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The Line of Code Checker |
|
17
|
|
|
*/ |
|
18
|
|
|
class LinesOfCodeChecker implements CheckerInterface, ParametersInterface |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
const THRESHOLD_CLASS = 'class'; |
|
22
|
|
|
|
|
23
|
|
|
const THRESHOLD_METHOD = 'method'; |
|
24
|
|
|
|
|
25
|
|
|
const THRESHOLD_FUNCTION = 'function'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $thresholds = []; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Checks to make sure the classes, methods, and functions are not |
|
35
|
|
|
* very long on the basis of Lines of Code |
|
36
|
|
|
* |
|
37
|
|
|
* @param Node $node |
|
38
|
|
|
* @throws ClassTooLongException |
|
39
|
|
|
* @throws MethodTooLongException |
|
40
|
|
|
* @throws FunctionTooLongException |
|
41
|
|
|
*/ |
|
42
|
|
|
public function check(Node $node) |
|
43
|
|
|
{ |
|
44
|
|
|
if ($node instanceof Function_) { |
|
45
|
|
|
$this->checkFunction($node); |
|
46
|
|
|
} elseif ($node instanceof ClassMethod) { |
|
47
|
|
|
$this->checkClassMethod($node); |
|
48
|
|
|
} elseif ($node instanceof Class_) { |
|
49
|
|
|
$this->checkClass($node); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Checks if a function, class or method is too long according to its lines of code |
|
56
|
|
|
* |
|
57
|
|
|
* @param Node $node |
|
58
|
|
|
* @param int $threshold |
|
59
|
|
|
* @return bool |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function isTooLong(Node $node, $threshold) |
|
62
|
|
|
{ |
|
63
|
|
|
|
|
64
|
|
|
$startLine = $node->getAttribute('startLine'); |
|
65
|
|
|
$endLine = $node->getAttribute('endLine'); |
|
66
|
|
|
|
|
67
|
|
|
$loc = $endLine - $startLine; |
|
68
|
|
|
|
|
69
|
|
|
return $loc > $threshold; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param Function_ $node |
|
74
|
|
|
* @throws FunctionTooLongException |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function checkFunction(Function_ $node) |
|
77
|
|
|
{ |
|
78
|
|
|
if ($this->isTooLong($node, $this->thresholds[self::THRESHOLD_FUNCTION])) { |
|
79
|
|
|
throw (new FunctionTooLongException())->setNode($node); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param ClassMethod $node |
|
85
|
|
|
* @throws MethodTooLongException |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function checkClassMethod(ClassMethod $node) |
|
88
|
|
|
{ |
|
89
|
|
|
if ($this->isTooLong($node, $this->thresholds[self::THRESHOLD_METHOD])) { |
|
90
|
|
|
throw (new MethodTooLongException())->setNode($node); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param Class_ $node |
|
96
|
|
|
* @throws ClassTooLongException |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function checkClass(Class_ $node) |
|
99
|
|
|
{ |
|
100
|
|
|
if ($this->isTooLong($node, $this->thresholds[self::THRESHOLD_CLASS])) { |
|
101
|
|
|
throw (new ClassTooLongException())->setNode($node); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param array $params |
|
107
|
|
|
* @return mixed |
|
108
|
|
|
*/ |
|
109
|
|
|
public function setParameters(array $params) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->thresholds = $params['loc_threshold']; |
|
112
|
|
|
|
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|