NamespaceNodeParser   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 50
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A invoke() 0 13 4
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 PhpParser\Node;
15
use PhpUnitGen\Exception\Exception;
16
use PhpUnitGen\Model\ModelInterface\PhpFileModelInterface;
17
use PhpUnitGen\Model\PropertyInterface\NodeInterface;
18
use PhpUnitGen\Parser\NodeParserUtil\UsePreParseTrait;
19
20
/**
21
 * Class NamespaceNodeParser.
22
 *
23
 * @author     Paul Thébaud <[email protected]>.
24
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
25
 * @license    https://opensource.org/licenses/MIT The MIT license.
26
 * @link       https://github.com/paul-thebaud/phpunit-generator
27
 * @since      Class available since Release 2.0.0.
28
 */
29
class NamespaceNodeParser extends AbstractNodeParser
30
{
31
    use UsePreParseTrait;
32
33
    /**
34
     * NamespaceNodeParser constructor.
35
     *
36
     * @param UseNodeParser       $useNodeParser       The use node parser to use.
37
     * @param GroupUseNodeParser  $groupUseNodeParser  The group use node parser to use.
38
     * @param FunctionNodeParser  $functionNodeParser  The function node parser to use.
39
     * @param ClassNodeParser     $classNodeParser     The class node parser to use.
40
     * @param TraitNodeParser     $traitNodeParser     The trait node parser to use.
41
     * @param InterfaceNodeParser $interfaceNodeParser The interface node parser to use.
42
     */
43
    public function __construct(
44
        UseNodeParser $useNodeParser,
45
        GroupUseNodeParser $groupUseNodeParser,
46
        FunctionNodeParser $functionNodeParser,
47
        ClassNodeParser $classNodeParser,
48
        TraitNodeParser $traitNodeParser,
49
        InterfaceNodeParser $interfaceNodeParser
50
    ) {
51
        $this->nodeParsers[Node\Stmt\Function_::class]  = $functionNodeParser;
52
        $this->nodeParsers[Node\Stmt\Class_::class]     = $classNodeParser;
53
        $this->nodeParsers[Node\Stmt\Trait_::class]     = $traitNodeParser;
54
        $this->nodeParsers[Node\Stmt\Interface_::class] = $interfaceNodeParser;
55
56
        $this->useNodeParser      = $useNodeParser;
57
        $this->groupUseNodeParser = $groupUseNodeParser;
58
    }
59
60
    /**
61
     * Parse a node to update the parent node model.
62
     *
63
     * @param mixed         $node   The node to parse.
64
     * @param NodeInterface $parent The parent node.
65
     */
66
    public function invoke($node, NodeInterface $parent): void
67
    {
68
        if (! $node instanceof Node\Stmt\Namespace_ || ! $parent instanceof PhpFileModelInterface) {
69
            throw new Exception('NamespaceNodeParser is made to parse a namespace node');
70
        }
71
72
        if ($node->name instanceof Node\Name) {
73
            $parent->setNamespace($node->name->parts);
74
        }
75
76
        $this->preParseUses($node->stmts, $parent);
77
78
        $this->parseSubNodes($node->stmts, $parent);
79
    }
80
}
81