TreeScanner::branch()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.7
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace WyriHaximus\TwigView\Lib;
5
6
/**
7
 * Class TreeScanner.
8
 * @package WyriHaximus\TwigView\Lib
9
 */
10
final class TreeScanner
11
{
12
    /**
13
     * Return all sections (app & plugins) with an Template directory.
14
     *
15
     * @return array
16
     */
17 2
    public static function all(): array
18
    {
19 2
        return static::deepen(RelativeScanner::all());
0 ignored issues
show
Documentation introduced by
\WyriHaximus\TwigView\Lib\RelativeScanner::all() is of type array, 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...
20
    }
21
22
    /**
23
     * Return all templates for a given plugin.
24
     *
25
     * @param string $plugin The plugin to find all templates for.
26
     *
27
     * @return mixed
28
     */
29 1
    public static function plugin($plugin)
30
    {
31 1
        return static::deepen([
0 ignored issues
show
Documentation introduced by
array($plugin => \WyriHa...anner::plugin($plugin)) is of type array<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...
32 1
            $plugin => RelativeScanner::plugin($plugin),
33 1
        ])[$plugin];
34
    }
35
36
    /**
37
     * Strip the absolute path of template's paths for all given sections.
38
     *
39
     * @param string $sections Sections to iterate over.
40
     *
41
     * @return array
42
     */
43 3
    protected static function deepen($sections): array
44
    {
45 3
        foreach ($sections as $section => $paths) {
0 ignored issues
show
Bug introduced by
The expression $sections of type string is not traversable.
Loading history...
46 3
            $sections[$section] = static::convertToTree($paths);
47
        }
48
49 3
        return $sections;
50
    }
51
52
    /**
53
     * Turn a set of paths into a tree.
54
     *
55
     * @param array $paths Paths to turn into a tree.
56
     *
57
     * @return array
58
     */
59 3
    protected static function convertToTree(array $paths): array
60
    {
61 3
        foreach ($paths as $index => $path) {
62 3
            static::convertPathToTree($paths, $index, $path);
63
        }
64
65 3
        return $paths;
66
    }
67
68
    /**
69
     * Convert a path into a tree when it contains a directory separator.
70
     *
71
     * @param array  $paths The paths to work on.
72
     * @param mixed  $index Index of $path.
73
     * @param string $path  Path to breakup and turn into a tree.
74
     *
75
     */
76 3
    protected static function convertPathToTree(array &$paths, $index, $path)
77
    {
78 3
        if (strpos($path, DIRECTORY_SEPARATOR) !== false) {
79 3
            $chunks = explode(DIRECTORY_SEPARATOR, $path);
80 3
            $paths = static::branch($paths, $chunks);
81 3
            unset($paths[$index]);
82
        }
83 3
    }
84
85
    /**
86
     * Create a branch for the current level and push a twig on it.
87
     *
88
     * @param array $paths    Paths to append.
89
     * @param array $branches Branches to use untill only one left.
90
     *
91
     * @return array
92
     */
93 3
    protected static function branch(array $paths, array $branches): array
94
    {
95 3
        $twig = array_shift($branches);
96 3
        if (count($branches) == 0) {
97 3
            $paths[] = $twig;
98
99 3
            return $paths;
100
        }
101
102 3
        if (!isset($paths[$twig])) {
103 3
            $paths[$twig] = [];
104
        }
105
106 3
        $paths[$twig] = static::branch($paths[$twig], $branches);
107
108 3
        return $paths;
109
    }
110
}
111