Completed
Push — develop ( 59e403...53b1d2 )
by Paul
03:03
created

GroupUseNodeParser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A invoke() 0 12 4
A validateType() 0 3 2
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\GroupUseNodeParserInterface;
8
use PhpUnitGen\Parser\NodeParser\NodeParserInterface\UseNodeParserInterface;
9
10
/**
11
 * Class GroupUseNodeParser.
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 GroupUseNodeParser extends AbstractNodeParser implements GroupUseNodeParserInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function invoke(Node\Stmt\GroupUse $node, PhpFileModelInterface $parent): PhpFileModelInterface
25
    {
26
        if ($this->validateType($node->type)) {
27
            $prefix = $node->prefix->toString();
28
            foreach ($node->uses as $use) {
29
                if ($this->validateType($node->type)) {
30
                    $parent->addUse($use->alias, $prefix . '\\' . $use->name->toString());
31
                }
32
            }
33
        }
34
35
        return $parent;
36
    }
37
38
    /**
39
     * Validate a use type.
40
     *
41
     * @param int $type The type to validate.
42
     *
43
     * @return bool True if the type is valid.
44
     */
45
    private function validateType(int $type): bool
46
    {
47
        return $type === Node\Stmt\Use_::TYPE_NORMAL || $type === Node\Stmt\Use_::TYPE_UNKNOWN;
48
    }
49
}
50