Passed
Push — develop ( fb709e...b69568 )
by Paul
03:14
created

NamespaceNodeParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 5
dl 0
loc 14
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\ModelInterface\PhpFileModelInterface;
7
use PhpUnitGen\Parser\NodeParser\NodeParserInterface\ClassNodeParserInterface;
8
use PhpUnitGen\Parser\NodeParser\NodeParserInterface\FunctionNodeParserInterface;
9
use PhpUnitGen\Parser\NodeParser\NodeParserInterface\InterfaceNodeParserInterface;
10
use PhpUnitGen\Parser\NodeParser\NodeParserInterface\NamespaceNodeParserInterface;
11
use PhpUnitGen\Parser\NodeParser\NodeParserInterface\TraitNodeParserInterface;
12
use PhpUnitGen\Parser\NodeParser\NodeParserInterface\UseNodeParserInterface;
13
use PhpUnitGen\Parser\NodeParserUtil\UsePreParseTrait;
14
use Respect\Validation\Validator;
15
16
/**
17
 * Class NamespaceNodeParser.
18
 *
19
 * @author     Paul Thébaud <[email protected]>.
20
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
21
 * @license    https://opensource.org/licenses/MIT The MIT license.
22
 * @link       https://github.com/paul-thebaud/phpunit-generator
23
 * @since      Class available since Release 2.0.0.
24
 */
25
class NamespaceNodeParser extends AbstractNodeParser implements NamespaceNodeParserInterface
26
{
27
    use UsePreParseTrait;
28
29
    /**
30
     * NamespaceNodeParser constructor.
31
     *
32
     * @param UseNodeParserInterface       $useNodeParser       The use node parser to use.
33
     * @param FunctionNodeParserInterface  $functionNodeParser  The function node parser to use.
34
     * @param ClassNodeParserInterface     $classNodeParser     The class node parser to use.
35
     * @param TraitNodeParserInterface     $traitNodeParser     The trait node parser to use.
36
     * @param InterfaceNodeParserInterface $interfaceNodeParser The interface node parser to use.
37
     */
38
    public function __construct(
39
        UseNodeParserInterface $useNodeParser,
40
        FunctionNodeParserInterface $functionNodeParser,
41
        ClassNodeParserInterface $classNodeParser,
42
        TraitNodeParserInterface $traitNodeParser,
43
        InterfaceNodeParserInterface $interfaceNodeParser
44
    ) {
45
        $this->nodeParsers[Node\Stmt\Use_::class]       = $useNodeParser;
46
        $this->nodeParsers[Node\Stmt\Function_::class]  = $functionNodeParser;
47
        $this->nodeParsers[Node\Stmt\Class_::class]     = $classNodeParser;
48
        $this->nodeParsers[Node\Stmt\Trait_::class]     = $traitNodeParser;
49
        $this->nodeParsers[Node\Stmt\Interface_::class] = $interfaceNodeParser;
50
51
        $this->useNodeParser = $useNodeParser;
52
    }
53
54
    /**
55
     * {@inheritdoc }
56
     */
57
    public function invoke(Node\Stmt\Namespace_ $node, PhpFileModelInterface $parent): PhpFileModelInterface
58
    {
59
        if (Validator::instance(Node\Name::class)->validate($node->name)) {
60
            $parent->setNamespace($node->name->parts);
61
        }
62
63
        $parent = $this->preParseUses($node->stmts, $parent);
64
65
        return $this->parseSubNodes($node->stmts, $parent);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->parseSubNo...($node->stmts, $parent) could return the type PhpUnitGen\Model\PropertyInterface\NodeInterface which includes types incompatible with the type-hinted return PhpUnitGen\Model\ModelIn...e\PhpFileModelInterface. Consider adding an additional type-check to rule them out.
Loading history...
66
    }
67
}