FileParser   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 8
dl 0
loc 90
ccs 58
cts 58
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getParser() 0 4 1
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
B enterNode() 0 44 11
1
<?php
2
declare(strict_types=1);
3
4
namespace gossi\codegen\parser;
5
6
use gossi\codegen\parser\visitor\ParserVisitorInterface;
7
use phootwork\collection\Set;
8
use phootwork\file\exception\FileNotFoundException;
9
use phootwork\file\File;
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 17
	public function __construct($filename) {
21 17
		$this->filename = $filename;
22 17
		$this->visitors = new Set();
23 17
	}
24
25 16
	public function addVisitor(ParserVisitorInterface $visitor) {
26 16
		$this->visitors->add($visitor);
27 16
		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): bool {
36 1
		return $this->visitors->contains($visitor);
37
	}
38
39
	/**
40
	 * @throws FileNotFoundException
41
	 */
42 16
	public function parse() {
43 16
		$file = new File($this->filename);
44
45 16
		if (!$file->exists()) {
46 1
			throw new FileNotFoundException(sprintf('File (%s) does not exist.', $this->filename));
47
		}
48
49 15
		$parser = $this->getParser();
50 15
		$traverser = new NodeTraverser();
51 15
		$traverser->addVisitor($this);
52 15
		$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...
53 15
	}
54
55 15
	private function getParser(): Parser {
56 15
		$factory = new \PhpParser\ParserFactory();
57 15
		return $factory->create(\PhpParser\ParserFactory::PREFER_PHP7);
58
	}
59
60 15
	public function enterNode(Node $node) {
61 15
		foreach ($this->visitors as $visitor) {
62 15
			switch ($node->getType()) {
63 15
				case 'Stmt_Namespace':
64 9
					$visitor->visitNamespace($node);
65 9
					break;
66
67 15
				case 'Stmt_UseUse':
68 1
					$visitor->visitUseStatement($node);
69 1
					break;
70
71 15
				case 'Stmt_Class':
72 12
					$visitor->visitStruct($node);
73 12
					$visitor->visitClass($node);
74 12
					break;
75
76 15
				case 'Stmt_Interface':
77 2
					$visitor->visitStruct($node);
78 2
					$visitor->visitInterface($node);
79 2
					break;
80
81 15
				case 'Stmt_Trait':
82 1
					$visitor->visitStruct($node);
83 1
					$visitor->visitTrait($node);
84 1
					break;
85
86 15
				case 'Stmt_TraitUse':
87 2
					$visitor->visitTraitUse($node);
88 2
					break;
89
90 15
				case 'Stmt_ClassConst':
91 3
					$visitor->visitConstants($node);
92 3
					break;
93
94 15
				case 'Stmt_Property':
95 5
					$visitor->visitProperty($node);
96 5
					break;
97
98 15
				case 'Stmt_ClassMethod':
99 8
					$visitor->visitMethod($node);
100 8
					break;
101
			}
102
		}
103 15
	}
104
}
105