Completed
Push — finish-work-from-PR-37 ( 9a2843 )
by Cees-Jan
02:10
created

TreeScanner::convertPathToTree()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 3
crap 2
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 2
    public static function all()
17
    {
18 2
        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 1
    public static function plugin($plugin)
29
    {
30 1
        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 1
            $plugin => RelativeScanner::plugin($plugin),
32 1
        ])[$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 3
        foreach ($sections as $section => $paths) {
0 ignored issues
show
Bug introduced by
The expression $sections of type string is not traversable.
Loading history...
45 3
            $sections[$section] = static::convertToTree($paths);
46 3
        }
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 3
    protected static function convertToTree(array $paths)
58
    {
59 3
        foreach ($paths as $index => $path) {
60 2
            static::convertPathToTree($paths, $index, $path);
61 3
        }
62
63 3
        return $paths;
64
    }
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 2
    protected static function convertPathToTree(array &$paths, $index, $path)
76
    {
77 2
        if (strpos($path, DIRECTORY_SEPARATOR) !== false) {
78 2
            $chunks = explode(DIRECTORY_SEPARATOR, $path);
79 2
            $paths = static::branch($paths, $chunks);
80 2
            unset($paths[$index]);
81 2
        }
82 2
    }
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 2
    protected static function branch(array $paths, array $branches)
93
    {
94 2
        $twig = array_shift($branches);
95 2
        if (count($branches) == 0) {
96 2
            $paths[] = $twig;
97 2
            return $paths;
98
        }
99
100 2
        if (!isset($paths[$twig])) {
101 2
            $paths[$twig] = [];
102 2
        }
103
104 2
        $paths[$twig] = static::branch($paths[$twig], $branches);
105
106 2
        return $paths;
107
    }
108
}
109