Completed
Push — develop ( 34db25...1b1cc6 )
by Paul
02:36
created

UseNodeParser::invoke()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 5
nc 4
nop 2
dl 0
loc 9
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace PhpUnitGen\Parser\NodeParser;
4
5
use PhpParser\Node;
6
use PhpUnitGen\Exception\Exception;
7
use PhpUnitGen\Model\ModelInterface\PhpFileModelInterface;
8
use PhpUnitGen\Model\PropertyInterface\NodeInterface;
9
10
/**
11
 * Class UseNodeParser.
12
 *
13
 * @author     Paul Thébaud <[email protected]>.
14
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
15
 * @license    https://opensource.org/licenses/MIT The MIT license.
16
 * @link       https://github.com/paul-thebaud/phpunit-generator
17
 * @since      Class available since Release 2.0.0.
18
 */
19
class UseNodeParser extends AbstractNodeParser
20
{
21
    /**
22
     * Parse a node to update the parent node model.
23
     *
24
     * @param mixed         $node   The node to parse.
25
     * @param NodeInterface $parent The parent node.
26
     */
27
    public function invoke($node, NodeInterface $parent): void
28
    {
29
        if (! $node instanceof Node\Stmt\Use_ || ! $parent instanceof PhpFileModelInterface) {
30
            throw new Exception('UseNodeParser is made to parse a use node');
31
        }
32
33
        if ($node->type === Node\Stmt\Use_::TYPE_NORMAL) {
34
            foreach ($node->uses as $use) {
35
                $parent->addUse($use->alias, $use->name->toString());
36
            }
37
        }
38
    }
39
}
40