|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpUnitGen\Parser\NodeParser; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser\Node; |
|
6
|
|
|
use PhpUnitGen\Exception\NodeParserException; |
|
7
|
|
|
use PhpUnitGen\Model\PropertyInterface\NodeInterface; |
|
8
|
|
|
use PhpUnitGen\Parser\NodeParser\NodeParserInterface\NodeParserInterface; |
|
9
|
|
|
use Respect\Validation\Validator; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class AbstractNodeParser. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Paul Thébaud <[email protected]>. |
|
15
|
|
|
* @copyright 2017-2018 Paul Thébaud <[email protected]>. |
|
16
|
|
|
* @license https://opensource.org/licenses/MIT The MIT license. |
|
17
|
|
|
* @link https://github.com/paul-thebaud/phpunit-generator |
|
18
|
|
|
* @since Class available since Release 2.0.0. |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class AbstractNodeParser implements NodeParserInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var NodeParserInterface[] $nodeParsers Mapping array between PhpParser node class and PhpUnitGen node parser. |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $nodeParsers = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
* |
|
30
|
|
|
* The abstract node parser is used with implementation of <NodeName>NodeParserInterface, so it must have the |
|
31
|
|
|
* invoke method from them. |
|
32
|
|
|
* If the invoke method does not exists, or if the parameter class does not match, it is an implementation error. |
|
33
|
|
|
*/ |
|
34
|
|
|
public function parse(Node $node, NodeInterface $parent): NodeInterface |
|
35
|
|
|
{ |
|
36
|
|
|
if (! method_exists($this, 'invoke')) { |
|
37
|
|
|
throw new NodeParserException(sprintf( |
|
38
|
|
|
'Class "%s" must implements method invoke described in AbstractNodeParser', |
|
39
|
|
|
get_class($this) |
|
40
|
|
|
)); |
|
41
|
|
|
} |
|
42
|
|
|
return $parent = $this->invoke($node, $parent); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
public function parseSubNodes(array $nodes, NodeInterface $parent): NodeInterface |
|
49
|
|
|
{ |
|
50
|
|
|
foreach ($nodes as $node) { |
|
51
|
|
|
// Get the node class |
|
52
|
|
|
$class = get_class($node); |
|
53
|
|
|
|
|
54
|
|
|
// If a node parser exists |
|
55
|
|
|
if ($this->hasNodeParser($class)) { |
|
56
|
|
|
// Parse the node |
|
57
|
|
|
$parent = $this->getNodeParser($class)->parse($node, $parent); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
return $parent; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Check if this node parser instance has a node parser. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $class The node parser for this class. |
|
67
|
|
|
* |
|
68
|
|
|
* @return bool True if the node parser exists. |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function hasNodeParser(string $class): bool |
|
71
|
|
|
{ |
|
72
|
|
|
if (Validator::key($class, Validator::instance(NodeParserInterface::class)) |
|
73
|
|
|
->validate($this->nodeParsers)) { |
|
74
|
|
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get a node parser. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $class The node parser for this class. |
|
83
|
|
|
* |
|
84
|
|
|
* @return NodeParserInterface The node parser. |
|
85
|
|
|
* |
|
86
|
|
|
* @throws NodeParserException If the node parser does not exists. |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function getNodeParser(string $class): NodeParserInterface |
|
89
|
|
|
{ |
|
90
|
|
|
if ($this->hasNodeParser($class)) { |
|
91
|
|
|
return $this->nodeParsers[$class]; |
|
92
|
|
|
} |
|
93
|
|
|
throw new NodeParserException(sprintf('The node parser for "%s" cannot be found.', $class)); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|