Completed
Push — master ( 841e81...6f10f1 )
by Thomas
9s
created

FileParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
namespace gossi\codegen\parser;
3
4
use gossi\codegen\model\AbstractPhpStruct;
5
use gossi\codegen\parser\visitor\ParserVisitorInterface;
6
use phootwork\collection\Set;
7
use phootwork\file\exception\FileNotFoundException;
8
use phootwork\file\File;
9
use PhpParser\Node;
10
use PhpParser\NodeTraverser;
11
use PhpParser\NodeVisitorAbstract;
12
13
class FileParser extends NodeVisitorAbstract {
14
15
	private $visitors;
16
	private $filename;
17
	
18 19
	public function __construct($filename) {
19 19
		$this->filename = $filename;
20 19
		$this->visitors = new Set();
21 19
	}
22
	
23 18
	public function addVisitor(ParserVisitorInterface $visitor) {
24 18
		$this->visitors->add($visitor);
25 18
		return $this;
26
	}
27
	
28 1
	public function removeVisitor(ParserVisitorInterface $visitor) {
29 1
		$this->visitors->remove($visitor);
30 1
		return $this;
31
	}
32
	
33 1
	public function hasVisitor(ParserVisitorInterface $visitor) {
34 1
		return $this->visitors->contains($visitor);
35
	}
36
	
37
	/**
38
	 * @throws FileNotFoundException
39
	 * @return AbstractPhpStruct
40
	 */
41 18
	public function parse() {
42 18
		$file = new File($this->filename);
43
44 18
		if (!$file->exists()) {
45 1
			throw new FileNotFoundException(sprintf('File (%s) does not exist.', $this->filename));
46
		}
47
48 17
		$parser = $this->getParser();
49 17
		$traverser = new NodeTraverser();
50 17
		$traverser->addVisitor($this);
51 17
		$traverser->traverse($parser->parse($file->read()));
52 17
	}
53
	
54 17
	private function getParser() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
55 17
		if (class_exists('\\PhpParser\\ParserFactory')) {
56
			$factory = new \PhpParser\ParserFactory();
57
			return $factory->create(\PhpParser\ParserFactory::PREFER_PHP7);
58
		} else {
59 17
			return new \PhpParser\Parser(new \PhpParser\Lexer\Emulative());
60
		}
61
	}
62
	
63 17
	public function enterNode(Node $node) {
64 17
		foreach ($this->visitors as $visitor) {
65 17
			switch ($node->getType()) {
66 17
				case 'Stmt_Namespace':
67 13
					$visitor->visitNamespace($node);
68 13
					break;
69
		
70 17
				case 'Stmt_UseUse':
71 1
					$visitor->visitUseStatement($node);
72 2
					break;
73
		
74 17
				case 'Stmt_Class':
75 12
					$visitor->visitStruct($node);
76 12
					$visitor->visitClass($node);
77 12
					break;
78
		
79 17
				case 'Stmt_Interface':
80 3
					$visitor->visitStruct($node);
81 3
					$visitor->visitInterface($node);
82 3
					break;
83
		
84 17
				case 'Stmt_Trait':
85 2
					$visitor->visitStruct($node);
86 2
					$visitor->visitTrait($node);
87 2
					break;
88
		
89 17
				case 'Stmt_TraitUse':
90 3
					$visitor->visitTraitUse($node);
91 3
					break;
92
		
93 17
				case 'Stmt_ClassConst':
94 4
					$visitor->visitConstants($node);
95 4
					break;
96
		
97 17
				case 'Stmt_Property':
98 8
					$visitor->visitProperty($node);
99 8
					break;
100
		
101 17
				case 'Stmt_ClassMethod':
102 12
					$visitor->visitMethod($node);
103 12
					break;
104 17
			}
105 17
		}
106 17
	}
107
}
108