Completed
Pull Request — master (#30)
by Scott
02:51
created

FileParser::addFunction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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