Completed
Push — master ( 55b21a...aec22b )
by Thomas
06:20
created

FileParser   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
c 1
b 0
f 0
lcom 2
cbo 8
dl 0
loc 86
ccs 55
cts 55
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A addVisitor() 0 4 1
A removeVisitor() 0 4 1
A hasVisitor() 0 3 1
A parse() 0 12 2
C enterNode() 0 44 11
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\Lexer\Emulative;
10
use PhpParser\Node;
11
use PhpParser\NodeTraverser;
12
use PhpParser\NodeVisitorAbstract;
13
use PhpParser\Parser;
14
15
class FileParser extends NodeVisitorAbstract {
16
17
	private $visitors;
18
	private $filename;
19
	
20 18
	public function __construct($filename) {
21 18
		$this->filename = $filename;
22 18
		$this->visitors = new Set();
23 18
	}
24
	
25 17
	public function addVisitor(ParserVisitorInterface $visitor) {
26 17
		$this->visitors->add($visitor);
27 17
		return $this;
28
	}
29
	
30 1
	public function removeVisitor(ParserVisitorInterface $visitor) {
31 1
		$this->visitors->remove($visitor);
32 1
		return $this;
33
	}
34
	
35 1
	public function hasVisitor(ParserVisitorInterface $visitor) {
36 1
		return $this->visitors->contains($visitor);
37
	}
38
	
39
	/**
40
	 * @throws FileNotFoundException
41
	 * @return AbstractPhpStruct
42
	 */
43 17
	public function parse() {
44 17
		$file = new File($this->filename);
45
46 17
		if (!$file->exists()) {
47 1
			throw new FileNotFoundException(sprintf('File (%s) does not exist.', $this->filename));
48
		}
49
50 16
		$parser = new Parser(new Emulative());
51 16
		$traverser = new NodeTraverser();
52 16
		$traverser->addVisitor($this);
53 16
		$traverser->traverse($parser->parse($file->read()));
1 ignored issue
show
Bug introduced by
It seems like $parser->parse($file->read()) targeting PhpParser\ParserAbstract::parse() can also be of type null; however, PhpParser\NodeTraverser::traverse() does only seem to accept array<integer,object<PhpParser\Node>>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
54 16
	}
55
	
56 16
	public function enterNode(Node $node) {
57 16
		foreach ($this->visitors as $visitor) {
58 16
			switch ($node->getType()) {
59 16
				case 'Stmt_Namespace':
60 13
					$visitor->visitNamespace($node);
61 13
					break;
62
		
63 16
				case 'Stmt_UseUse':
64 1
					$visitor->visitUseStatement($node);
65 1
					break;
66
		
67 16
				case 'Stmt_Class':
68 11
					$visitor->visitStruct($node);
69 11
					$visitor->visitClass($node);
70 11
					break;
71
		
72 16
				case 'Stmt_Interface':
73 3
					$visitor->visitStruct($node);
74 3
					$visitor->visitInterface($node);
75 3
					break;
76
		
77 16
				case 'Stmt_Trait':
78 2
					$visitor->visitStruct($node);
79 2
					$visitor->visitTrait($node);
80 2
					break;
81
		
82 16
				case 'Stmt_TraitUse':
83 3
					$visitor->visitTraitUse($node);
84 3
					break;
85
		
86 16
				case 'Stmt_ClassConst':
87 4
					$visitor->visitConstants($node);
88 4
					break;
89
		
90 16
				case 'Stmt_Property':
91 8
					$visitor->visitProperty($node);
92 8
					break;
93
		
94 16
				case 'Stmt_ClassMethod':
95 12
					$visitor->visitMethod($node);
96 16
					break;
97
			}
98
		}
99 16
	}
100
}
101