Completed
Push — develop ( c4d989...a97889 )
by Jaap
12s
created

Pathfinder::walkObjectTree()   C

Complexity

Conditions 11
Paths 9

Size

Total Lines 35
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 35
rs 5.2653
cc 11
eloc 25
nc 9
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace phpDocumentor\Application\Renderer\TwigRenderer;
4
5
class Pathfinder
6
{
7
    /**
8
     * Combines the query and an object to retrieve a list of nodes that are to be used as node-point in a template.
9
     *
10
     * This method interprets the provided query string and walks through the given object to find the correct
11
     * element. This method will silently fail if an invalid query was provided; in such a case the given object
12
     * is returned.
13
     *
14
     * @param object $object
15
     * @param string $query
16
     *
17
     * @return \Traversable|mixed[]
18
     */
19
    public function find($object, $query)
20
    {
21
        if ($query) {
22
            $node = $this->walkObjectTree($object, $query);
23
24
            if (!is_array($node) && (!$node instanceof \Traversable)) {
25
                $node = array($node);
26
            }
27
28
            return $node;
29
        }
30
31
        return array($object);
32
    }
33
34
    /**
35
     * Walks an object graph and/or array using a twig query string.
36
     *
37
     * @param \Traversable|mixed $objectOrArray
38
     * @param string             $query         A path to walk separated by dots, i.e. `namespace.namespaces`.
39
     *
40
     * @return mixed
41
     */
42
    private function walkObjectTree($objectOrArray, $query)
43
    {
44
        $node = $objectOrArray;
45
        $objectPath = explode('.', $query);
46
47
        // walk through the tree
48
        foreach ($objectPath as $pathNode) {
49
            if (is_array($node)) {
50
                if (isset($node[$pathNode])) {
51
                    $node = $node[$pathNode];
52
                    continue;
53
                }
54
            } elseif (is_object($node)) {
55
                if (isset($node->$pathNode) || (method_exists($node, '__get') && $node->$pathNode)) {
56
                    $node = $node->$pathNode;
57
                    continue;
58
                } elseif (method_exists($node, $pathNode)) {
59
                    $node = $node->$pathNode();
60
                    continue;
61
                } elseif (method_exists($node, 'get' . $pathNode)) {
62
                    $pathNode = 'get' . $pathNode;
63
                    $node = $node->$pathNode();
64
                    continue;
65
                } elseif (method_exists($node, 'is' . $pathNode)) {
66
                    $pathNode = 'is' . $pathNode;
67
                    $node = $node->$pathNode();
68
                    continue;
69
                }
70
            }
71
72
            return null;
73
        }
74
75
        return $node;
76
    }
77
}
78