Completed
Push — develop ( 1b34c6...481302 )
by Paul
02:47
created

UsePreParseTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A preParseUses() 0 11 4
1
<?php
2
3
namespace PhpUnitGen\Parser\NodeParserUtil;
4
5
use PhpParser\Node\Stmt\GroupUse;
6
use PhpParser\Node\Stmt\Use_;
7
use PhpUnitGen\Model\ModelInterface\PhpFileModelInterface;
8
use PhpUnitGen\Parser\NodeParser\NodeParserInterface\GroupUseNodeParserInterface;
9
use PhpUnitGen\Parser\NodeParser\NodeParserInterface\UseNodeParserInterface;
10
use Respect\Validation\Validator;
11
12
/**
13
 * Trait UsePreParseTrait.
14
 *
15
 * @author     Paul Thébaud <[email protected]>.
16
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
17
 * @license    https://opensource.org/licenses/MIT The MIT license.
18
 * @link       https://github.com/paul-thebaud/phpunit-generator
19
 * @since      Class available since Release 2.0.0.
20
 */
21
trait UsePreParseTrait
22
{
23
    /**
24
     * @var UseNodeParserInterface $useNodeParser The use node parser.
25
     */
26
    protected $useNodeParser;
27
28
    /**
29
     * @var GroupUseNodeParserInterface $groupUseNodeParser The group use node parser.
30
     */
31
    protected $groupUseNodeParser;
32
33
    /**
34
     * Pre parse uses in nodes.
35
     *
36
     * @param array                 $nodes  The nodes to parse to find uses.
37
     * @param PhpFileModelInterface $parent The parent to update.
38
     *
39
     * @return PhpFileModelInterface The updated parent.
40
     */
41
    public function preParseUses(array $nodes, PhpFileModelInterface $parent): PhpFileModelInterface
42
    {
43
        foreach ($nodes as $node) {
44
            if (Validator::instance(Use_::class)->validate($node)) {
45
                $parent = $this->useNodeParser->invoke($node, $parent);
46
            }
47
            if (Validator::instance(GroupUse::class)->validate($node)) {
48
                $parent = $this->groupUseNodeParser->invoke($node, $parent);
49
            }
50
        }
51
        return $parent;
52
    }
53
}
54