Completed
Push — master ( 5be53c...92f243 )
by Thomas
03:28
created

FileParser::enterNode()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 44
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 11

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 44
ccs 36
cts 36
cp 1
rs 5.2653
cc 11
eloc 33
nc 11
nop 1
crap 11

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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