Completed
Push — master ( 77ceee...abe060 )
by Thomas
03:44
created

FileParser::getParser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
crap 2
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
	public function __construct($filename) {
19
		$this->filename = $filename;
20 18
		$this->visitors = new Set();
21 18
	}
22 18
	
23 18
	public function addVisitor(ParserVisitorInterface $visitor) {
24
		$this->visitors->add($visitor);
25 17
		return $this;
26 17
	}
27 17
	
28
	public function removeVisitor(ParserVisitorInterface $visitor) {
29
		$this->visitors->remove($visitor);
30 1
		return $this;
31 1
	}
32 1
	
33
	public function hasVisitor(ParserVisitorInterface $visitor) {
34
		return $this->visitors->contains($visitor);
35 1
	}
36 1
	
37
	/**
38
	 * @throws FileNotFoundException
39
	 * @return AbstractPhpStruct
40
	 */
41
	public function parse() {
42
		$file = new File($this->filename);
43 17
44 17
		if (!$file->exists()) {
45
			throw new FileNotFoundException(sprintf('File (%s) does not exist.', $this->filename));
46 17
		}
47 1
48
		$parser = $this->getParser();
49
		$traverser = new NodeTraverser();
50 16
		$traverser->addVisitor($this);
51 16
		$traverser->traverse($parser->parse($file->read()));
52 16
	}
53 16
	
54 16
	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
		if (class_exists('\\PhpParser\\ParserFactory')) {
56 16
			$factory = new \PhpParser\ParserFactory();
57 16
			return $factory->create(\PhpParser\ParserFactory::PREFER_PHP7);
58 16
		} else {
59 16
			return new \PhpParser\Parser(new \PhpParser\Lexer\Emulative());
60 13
		}
61 13
	}
62
	
63 16
	public function enterNode(Node $node) {
64 1
		foreach ($this->visitors as $visitor) {
65 1
			switch ($node->getType()) {
66
				case 'Stmt_Namespace':
67 16
					$visitor->visitNamespace($node);
68 11
					break;
69 11
		
70 11
				case 'Stmt_UseUse':
71
					$visitor->visitUseStatement($node);
72 16
					break;
73 3
		
74 3
				case 'Stmt_Class':
75 3
					$visitor->visitStruct($node);
76
					$visitor->visitClass($node);
77 16
					break;
78 2
		
79 2
				case 'Stmt_Interface':
80 2
					$visitor->visitStruct($node);
81
					$visitor->visitInterface($node);
82 16
					break;
83 3
		
84 3
				case 'Stmt_Trait':
85
					$visitor->visitStruct($node);
86 16
					$visitor->visitTrait($node);
87 4
					break;
88 4
		
89
				case 'Stmt_TraitUse':
90 16
					$visitor->visitTraitUse($node);
91 8
					break;
92 8
		
93
				case 'Stmt_ClassConst':
94 16
					$visitor->visitConstants($node);
95 12
					break;
96 12
		
97 16
				case 'Stmt_Property':
98 16
					$visitor->visitProperty($node);
99 16
					break;
100
		
101
				case 'Stmt_ClassMethod':
102
					$visitor->visitMethod($node);
103
					break;
104
			}
105
		}
106
	}
107
}
108