1
|
|
|
<?php |
2
|
|
|
namespace exussum12\CoverageChecker; |
3
|
|
|
|
4
|
|
|
use PhpParser\Error; |
5
|
|
|
use PhpParser\Node; |
6
|
|
|
use PhpParser\Node\FunctionLike; |
7
|
|
|
use PhpParser\Node\Stmt\ClassLike; |
8
|
|
|
use PhpParser\Node\Stmt\Namespace_; |
9
|
|
|
use PhpParser\Parser; |
10
|
|
|
use PhpParser\ParserFactory; |
11
|
|
|
|
12
|
|
|
class FileParser |
13
|
|
|
{ |
14
|
|
|
protected $sourceCode = ''; |
15
|
|
|
protected $classes = []; |
16
|
|
|
protected $functions = []; |
17
|
|
|
|
18
|
|
|
public function __construct($sourceCode) |
19
|
|
|
{ |
20
|
|
|
$this->sourceCode = $sourceCode; |
21
|
|
|
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); |
22
|
|
|
$this->parse($parser); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return CodeLimits[] |
27
|
|
|
*/ |
28
|
|
|
public function getClassLimits() |
29
|
|
|
{ |
30
|
|
|
return $this->classes; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return CodeLimits[] |
35
|
|
|
*/ |
36
|
|
|
public function getFunctionLimits() |
37
|
|
|
{ |
38
|
|
|
return $this->functions; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function parse(Parser $parser) |
42
|
|
|
{ |
43
|
|
|
try { |
44
|
|
|
$ast = $parser->parse($this->sourceCode); |
45
|
|
|
} catch (Error $exception) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
foreach ($ast as $node) { |
50
|
|
|
$this->handleNode($node); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function getCodeLimits(Node $node): CodeLimits |
55
|
|
|
{ |
56
|
|
|
$startLine = $node->getAttribute('startLine'); |
57
|
|
|
$endLine = $node->getAttribute('endLine'); |
58
|
|
|
if ($node->getDocComment()) { |
59
|
|
|
$startLine = $node->getDocComment()->getLine(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return new CodeLimits($startLine, $endLine); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function addClass($classLimits) |
66
|
|
|
{ |
67
|
|
|
$this->classes[] = $classLimits; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function addFunction($classLimits) |
71
|
|
|
{ |
72
|
|
|
$this->functions[] = $classLimits; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function handleClass(ClassLike $node) |
76
|
|
|
{ |
77
|
|
|
$this->addClass($this->getCodeLimits($node)); |
78
|
|
|
|
79
|
|
|
foreach ($node->getMethods() as $function) { |
80
|
|
|
$this->handleNode($function); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function handleFunction(FunctionLike $node) |
85
|
|
|
{ |
86
|
|
|
$this->addFunction($this->getCodeLimits($node)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function handleNamespace(Namespace_ $node) |
90
|
|
|
{ |
91
|
|
|
foreach ($node->stmts as $part) { |
92
|
|
|
$this->handleNode($part); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function handleNode(Node $node) |
97
|
|
|
{ |
98
|
|
|
$type = $node->getType(); |
99
|
|
|
if ($type == 'Stmt_Namespace') { |
100
|
|
|
$this->handleNamespace($node); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($type == 'Stmt_Class') { |
104
|
|
|
$this->handleClass($node); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($type == "Stmt_Function" || $type == "Stmt_ClassMethod") { |
108
|
|
|
$this->handleFunction($node); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|