Completed
Pull Request — master (#68)
by Cristiano
05:36
created

FileParser   A

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
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the php-code-generator package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @license Apache-2.0
8
 */
9
10
namespace gossi\codegen\parser;
11
12
use gossi\codegen\parser\visitor\ParserVisitorInterface;
13
use phootwork\collection\Set;
14
use phootwork\file\exception\FileException;
15
use phootwork\file\File;
16
use PhpParser\Node;
17
use PhpParser\NodeTraverser;
18
use PhpParser\NodeVisitorAbstract;
19
use PhpParser\Parser;
20 17
use PhpParser\ParserFactory;
21 17
22 17
class FileParser extends NodeVisitorAbstract {
23 17
	private Set $visitors;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
24
	private File $file;
25 16
26 16
	public function __construct(string $filename) {
27 16
		$this->file = new File($filename);
28
		$this->visitors = new Set();
29
	}
30 1
31 1
	public function addVisitor(ParserVisitorInterface $visitor): self {
32 1
		$this->visitors->add($visitor);
33
34
		return $this;
35 1
	}
36 1
37
	public function removeVisitor(ParserVisitorInterface $visitor): self {
38
		$this->visitors->remove($visitor);
39
40
		return $this;
41
	}
42 16
43 16
	public function hasVisitor(ParserVisitorInterface $visitor): bool {
44
		return $this->visitors->contains($visitor);
45 16
	}
46 1
47
	/**
48
	 * @throws FileException If not existent or not readable file
49 15
	 */
50 15
	public function parse(): void {
51 15
		$parser = $this->getParser();
52 15
		$traverser = new NodeTraverser();
53 15
		$traverser->addVisitor($this);
54
		$traverser->traverse($parser->parse((string) $this->file->read()));
55 15
	}
56 15
57 15
	private function getParser(): Parser {
58
		$factory = new ParserFactory();
59
60 15
		return $factory->create(ParserFactory::PREFER_PHP7);
61 15
	}
62 15
63 15
	public function enterNode(Node $node): void {
64 9
		foreach ($this->visitors as $visitor) {
65 9
			switch ($node->getType()) {
66
				case 'Stmt_Namespace':
67 15
					$visitor->visitNamespace($node);
68 1
					break;
69 1
70
				case 'Stmt_UseUse':
71 15
					$visitor->visitUseStatement($node);
72 12
					break;
73 12
74 12
				case 'Stmt_Class':
75
					$visitor->visitStruct($node);
76 15
					$visitor->visitClass($node);
77 2
					break;
78 2
79 2
				case 'Stmt_Interface':
80
					$visitor->visitStruct($node);
81 15
					$visitor->visitInterface($node);
82 1
					break;
83 1
84 1
				case 'Stmt_Trait':
85
					$visitor->visitStruct($node);
86 15
					$visitor->visitTrait($node);
87 2
					break;
88 2
89
				case 'Stmt_TraitUse':
90 15
					$visitor->visitTraitUse($node);
91 3
					break;
92 3
93
				case 'Stmt_ClassConst':
94 15
					$visitor->visitConstants($node);
95 5
					break;
96 5
97
				case 'Stmt_Property':
98 15
					$visitor->visitProperty($node);
99 8
					break;
100 8
101
				case 'Stmt_ClassMethod':
102
					$visitor->visitMethod($node);
103 15
					break;
104
			}
105
		}
106
	}
107
}
108