1
|
|
|
<?php |
2
|
|
|
namespace exussum12\CoverageChecker; |
3
|
|
|
|
4
|
|
|
use PhpParser\Node; |
5
|
|
|
use PhpParser\Parser; |
6
|
|
|
use PhpParser\ParserFactory; |
7
|
|
|
|
8
|
|
|
class FileParser |
9
|
|
|
{ |
10
|
|
|
protected $sourceCode = ''; |
11
|
|
|
protected $classes = []; |
12
|
|
|
protected $functions = []; |
13
|
|
|
|
14
|
|
|
public function __construct($sourceCode) |
15
|
|
|
{ |
16
|
|
|
$this->sourceCode = $sourceCode; |
17
|
|
|
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); |
18
|
|
|
$this->parse($parser); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return CodeLimits[] |
23
|
|
|
*/ |
24
|
|
|
public function getClassLimits() |
25
|
|
|
{ |
26
|
|
|
return $this->classes; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return CodeLimits[] |
31
|
|
|
*/ |
32
|
|
|
public function getFunctionLimits() |
33
|
|
|
{ |
34
|
|
|
return $this->functions; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function parse(Parser $parser) |
38
|
|
|
{ |
39
|
|
|
$ast = $parser->parse($this->sourceCode); |
40
|
|
|
foreach ($ast as $node) { |
|
|
|
|
41
|
|
|
$this->handleNode($node); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function getCodeLimits(Node $node) |
46
|
|
|
{ |
47
|
|
|
$startLine = $node->getAttribute('startLine'); |
48
|
|
|
$endLine = $node->getAttribute('endLine'); |
49
|
|
|
if ($node->getDocComment()) { |
50
|
|
|
$startLine = $node->getDocComment()->getLine(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return new CodeLimits($startLine, $endLine); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function addClass($classLimits) |
57
|
|
|
{ |
58
|
|
|
$this->classes[] = $classLimits; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function addFunction($classLimits) |
62
|
|
|
{ |
63
|
|
|
$this->functions[] = $classLimits; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function handleClass(Node $node) |
67
|
|
|
{ |
68
|
|
|
$this->addClass($this->getCodeLimits($node)); |
69
|
|
|
|
70
|
|
|
foreach ($node->getMethods() as $function) { |
|
|
|
|
71
|
|
|
$this->handleNode($function); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function handleFunction(Node $node) |
76
|
|
|
{ |
77
|
|
|
$this->addFunction($this->getCodeLimits($node)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function handleNamespace(Node $node) |
81
|
|
|
{ |
82
|
|
|
foreach ($node->stmts as $part) { |
|
|
|
|
83
|
|
|
$this->handleNode($part); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param $node |
89
|
|
|
*/ |
90
|
|
|
protected function handleNode($node) |
91
|
|
|
{ |
92
|
|
|
$type = $node->getType(); |
93
|
|
|
if ($type == 'Stmt_Namespace') { |
94
|
|
|
$this->handleNamespace($node); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if ($type == 'Stmt_Class') { |
98
|
|
|
$this->handleClass($node); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($type == "Stmt_Function" || $type == "Stmt_ClassMethod") { |
102
|
|
|
$this->handleFunction($node); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.