Completed
Push — master ( e9d759...d58982 )
by Thomas
03:07
created

FileParser   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Test Coverage

Coverage 98.33%

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 9
dl 0
loc 96
ccs 59
cts 60
cp 0.9833
rs 10
c 0
b 0
f 0

7 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
A getParser() 0 9 2
B 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\Node;
10
use PhpParser\NodeTraverser;
11
use PhpParser\NodeVisitorAbstract;
12
13
class FileParser extends NodeVisitorAbstract {
14
15
	private $visitors;
16
	private $filename;
17
18 15
	public function __construct($filename) {
19 15
		$this->filename = $filename;
20 15
		$this->visitors = new Set();
21 15
	}
22
23 14
	public function addVisitor(ParserVisitorInterface $visitor) {
24 14
		$this->visitors->add($visitor);
25 14
		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 14
	public function parse() {
42 14
		$file = new File($this->filename);
43
44 14
		if (!$file->exists()) {
45 1
			throw new FileNotFoundException(sprintf('File (%s) does not exist.', $this->filename));
46
		}
47
48 13
		$parser = $this->getParser();
49 13
		$traverser = new NodeTraverser();
50 13
		$traverser->addVisitor($this);
51 13
		$traverser->traverse($parser->parse($file->read()));
0 ignored issues
show
Bug introduced by
It seems like $parser->parse($file->read()) targeting PhpParser\Parser::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...
52 13
	}
53
54 13
	private function getParser() {
55 13
		if (class_exists('\\PhpParser\\ParserFactory')) {
56 13
			$factory = new \PhpParser\ParserFactory();
57 13
			return $factory->create(\PhpParser\ParserFactory::PREFER_PHP7);
58
		} else {
59
			// because sami v3 requires php-parser v1
60
			return new \PhpParser\Parser(new \PhpParser\Lexer\Emulative());
0 ignored issues
show
Unused Code introduced by
The call to Parser::__construct() has too many arguments starting with new \PhpParser\Lexer\Emulative().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

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