Completed
Pull Request — master (#44)
by Cees-Jan
20:58 queued 19:07
created

TreeScanner::convertPathToTree()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 3
crap 6
1
<?php
2
3
namespace WyriHaximus\TwigView\Lib;
4
5
/**
6
 * Class TreeScanner
7
 * @package WyriHaximus\TwigView\Lib
8
 */
9
class TreeScanner
10
{
11
    /**
12
     * Return all sections (app & plugins) with an Template directory.
13
     *
14
     * @return array
15
     */
16
    public static function all()
17
    {
18
        return static::deepen(RelativeScanner::all());
19
    }
20
21
    /**
22
     * Return all templates for a given plugin.
23
     *
24
     * @param string $plugin The plugin to find all templates for.
25
     *
26
     * @return mixed
27
     */
28
    public static function plugin($plugin)
29
    {
30
        return static::deepen([
0 ignored issues
show
Documentation introduced by
array($plugin => \WyriHa...anner::plugin($plugin)) is of type array<string,string>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
31
            $plugin => RelativeScanner::plugin($plugin),
32
        ])[$plugin];
33
    }
34
35
    /**
36
     * Strip the absolute path of template's paths for all given sections.
37
     *
38
     * @param string $sections Sections to iterate over.
39
     *
40
     * @return array
41
     */
42 3
    protected static function deepen($sections)
43
    {
44
        foreach ($sections as $section => $paths) {
0 ignored issues
show
Bug introduced by
The expression $sections of type string is not traversable.
Loading history...
45
            $sections[$section] = static::convertToTree($paths);
46 1
        }
47 3
        return $sections;
48
    }
49
50
    /**
51
     * Turn a set of paths into a tree.
52
     *
53
     * @param array $paths Paths to turn into a tree.
54
     *
55
     * @return array
56
     */
57 1
    protected static function convertToTree(array $paths)
58
    {
59
        foreach ($paths as $index => $path) {
60
            static::convertPathToTree($paths, $index, $path);
61
        }
62
63 1
        return $paths;
64 1
    }
65
66
    /**
67
     * Convert a path into a tree when it contains a directory separator.
68
     *
69
     * @param array  $paths The paths to work on.
70
     * @param mixed  $index Index of $path.
71
     * @param string $path  Path to breakup and turn into a tree.
72
     *
73
     * @return void
74
     */
75
    protected static function convertPathToTree(array &$paths, $index, $path)
76
    {
77
        if (strpos($path, DIRECTORY_SEPARATOR) !== false) {
78
            $chunks = explode(DIRECTORY_SEPARATOR, $path);
79
            $paths = static::branch($paths, $chunks);
80
            unset($paths[$index]);
81
        }
82
    }
83
84
    /**
85
     * Create a branch for the current level and push a twig on it.
86
     *
87
     * @param array $paths    Paths to append.
88
     * @param array $branches Branches to use untill only one left.
89
     *
90
     * @return array
91
     */
92
    protected static function branch(array $paths, array $branches)
93
    {
94
        $twig = array_shift($branches);
95
        if (count($branches) == 0) {
96
            $paths[] = $twig;
97
            return $paths;
98
        }
99
100
        if (!isset($paths[$twig])) {
101
            $paths[$twig] = [];
102
        }
103
104
        $paths[$twig] = static::branch($paths[$twig], $branches);
105
106
        return $paths;
107
    }
108
}
109