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