|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpUnitGen\Parser\NodeParser; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser\Node; |
|
6
|
|
|
use PhpUnitGen\Model\PropertyInterface\NodeInterface; |
|
7
|
|
|
use Respect\Validation\Validator; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class AbstractNodeParser. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Paul Thébaud <[email protected]>. |
|
13
|
|
|
* @copyright 2017-2018 Paul Thébaud <[email protected]>. |
|
14
|
|
|
* @license https://opensource.org/licenses/MIT The MIT license. |
|
15
|
|
|
* @link https://github.com/paul-thebaud/phpunit-generator |
|
16
|
|
|
* @since Class available since Release 2.0.0. |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class AbstractNodeParser implements NodeParserInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var NodeParserInterface[] $nodeParsers The array of node parsers. A key is node class, and a value is the |
|
22
|
|
|
* parser for this class. |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $nodeParsers = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
public function parse(Node $node, NodeInterface $parent): NodeInterface |
|
30
|
|
|
{ |
|
31
|
|
|
// By default, only return the parent node. |
|
32
|
|
|
return $parent; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
public function parseSubNodes(array $nodes, NodeInterface $parent): NodeInterface |
|
39
|
|
|
{ |
|
40
|
|
|
if (Validator::arrayType()->length(1, null)->validate($nodes)) { |
|
41
|
|
|
foreach ($nodes as $node) { |
|
42
|
|
|
$parent = $this->parseSubNode($node, $parent); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
return $parent; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Parse a sub node to update parent. |
|
50
|
|
|
* |
|
51
|
|
|
* @param Node $node The node to parse. |
|
52
|
|
|
* @param NodeInterface $parent The parent. |
|
53
|
|
|
* |
|
54
|
|
|
* @return NodeInterface The updated parent. |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function parseSubNode(Node $node, NodeInterface $parent): NodeInterface |
|
57
|
|
|
{ |
|
58
|
|
|
$class = get_class($node); |
|
59
|
|
|
|
|
60
|
|
|
var_dump('I want to parse a "' . get_class($node) . '"'); |
|
|
|
|
|
|
61
|
|
|
if (Validator::key($class, Validator::instance(NodeParserInterface::class)) |
|
62
|
|
|
->validate($this->nodeParsers) |
|
63
|
|
|
) { |
|
64
|
|
|
var_dump('Im gonna parse a "' . get_class($node) . '"'); |
|
65
|
|
|
$parent = $this->nodeParsers[$class]->parse($node, $parent); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $parent; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|