UseNodeParser::invoke()   A
last analyzed

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 9.6111
c 0
b 0
f 0
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
19
/**
20
 * Class UseNodeParser.
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
class UseNodeParser extends AbstractNodeParser
29
{
30
    /**
31
     * Parse a node to update the parent node model.
32
     *
33
     * @param mixed         $node   The node to parse.
34
     * @param NodeInterface $parent The parent node.
35
     */
36
    public function invoke($node, NodeInterface $parent): void
37
    {
38
        if (! $node instanceof Node\Stmt\Use_ || ! $parent instanceof PhpFileModelInterface) {
39
            throw new Exception('UseNodeParser is made to parse a use node');
40
        }
41
42
        if ($node->type === Node\Stmt\Use_::TYPE_NORMAL) {
43
            foreach ($node->uses as $use) {
44
                $parent->addUse($use->getAlias()->name, $use->name->toString());
45
            }
46
        }
47
    }
48
}
49