Completed
Push — develop ( 763ea8...df26dc )
by Paul
01:58
created

AbstractNodeParser::parseSubNodes()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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) . '"');
0 ignored issues
show
Security Debugging Code introduced by
var_dump('I want to pars...get_class($node) . '"') looks like debug code. Are you sure you do not want to remove it?
Loading history...
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