Pathfinder::walkObjectTree()   B
last analyzed

Complexity

Conditions 11
Paths 9

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
cc 11
nc 9
nop 2
dl 0
loc 35
ccs 0
cts 31
cp 0
crap 132
rs 7.3166
c 0
b 0
f 0

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
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Transformer\Writer;
17
18
class Pathfinder
19
{
20
    /**
21
     * Combines the query and an object to retrieve a list of nodes that are to be used as node-point in a template.
22
     *
23
     * This method interprets the provided query string and walks through the given object to find the correct
24
     * element. This method will silently fail if an invalid query was provided; in such a case the given object
25
     * is returned.
26
     *
27
     * @param object $object
28
     * @param string $query
29
     *
30
     * @return \Traversable|array
31
     */
32
    public function find($object, $query)
33
    {
34
        if ($query) {
35
            $node = $this->walkObjectTree($object, $query);
36
37
            if (!is_array($node) && (!$node instanceof \Traversable)) {
38
                $node = [$node];
39
            }
40
41
            return $node;
42
        }
43
44
        return [$object];
45
    }
46
47
    /**
48
     * Walks an object graph and/or array using a twig query string.
49
     *
50
     * @param \Traversable|mixed $objectOrArray
51
     * @param string             $query         A path to walk separated by dots, i.e. `namespace.namespaces`.
52
     *
53
     * @return mixed
54
     */
55
    private function walkObjectTree($objectOrArray, $query)
56
    {
57
        $node = $objectOrArray;
58
        $objectPath = explode('.', $query);
59
60
        // walk through the tree
61
        foreach ($objectPath as $pathNode) {
62
            if (is_array($node)) {
63
                if (isset($node[$pathNode])) {
64
                    $node = $node[$pathNode];
65
                    continue;
66
                }
67
            } elseif (is_object($node)) {
68
                if (isset($node->{$pathNode}) || (method_exists($node, '__get') && $node->{$pathNode})) {
69
                    $node = $node->{$pathNode};
70
                    continue;
71
                } elseif (method_exists($node, $pathNode)) {
72
                    $node = $node->{$pathNode}();
73
                    continue;
74
                } elseif (method_exists($node, 'get' . $pathNode)) {
75
                    $pathNode = 'get' . $pathNode;
76
                    $node = $node->{$pathNode}();
77
                    continue;
78
                } elseif (method_exists($node, 'is' . $pathNode)) {
79
                    $pathNode = 'is' . $pathNode;
80
                    $node = $node->{$pathNode}();
81
                    continue;
82
                }
83
            }
84
85
            return null;
86
        }
87
88
        return $node;
89
    }
90
}
91