Completed
Push — develop ( 251d34...03436e )
by Paul
02:15
created

RootRetrieverTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A getRoot() 0 10 3
1
<?php
2
3
namespace PhpUnitGen\Util;
4
5
use PhpUnitGen\Model\ModelInterface\PhpFileModelInterface;
6
use PhpUnitGen\Model\PropertyInterface\NodeInterface;
7
use Respect\Validation\Validator;
8
9
/**
10
 * Trait RootRetrieverTrait.
11
 *
12
 * @author     Paul Thébaud <[email protected]>.
13
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
14
 * @license    https://opensource.org/licenses/MIT The MIT license.
15
 * @link       https://github.com/paul-thebaud/phpunit-generator
16
 * @since      Class available since Release 2.0.0.
17
 */
18
trait RootRetrieverTrait
19
{
20
    /**
21
     * Get the root of the node.
22
     *
23
     * @param NodeInterface $node The node to search on.
24
     *
25
     * @return PhpFileModelInterface|null The root if it is found, else null.
26
     */
27
    public function getRoot(NodeInterface $node): ?PhpFileModelInterface
28
    {
29
        $parent = $node;
30
        while ($parent !== null) {
31
            if ($parent instanceof PhpFileModelInterface) {
32
                return $parent;
33
            }
34
            $parent = $parent->getParentNode();
35
        }
36
        return null;
37
    }
38
}
39