RelativeScanner::stripAbsolutePath()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 9
cts 9
cp 1
rs 9.6666
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4
1
<?php
2
declare(strict_types=1);
3
4
namespace WyriHaximus\TwigView\Lib;
5
6
use Cake\Core\App;
7
use Cake\Core\Plugin;
8
9
/**
10
 * Class RelativeScanner.
11
 * @package WyriHaximus\TwigView\Lib
12
 */
13
final class RelativeScanner
14
{
15
    /**
16
     * Return all sections (app & plugins) with an Template directory.
17
     *
18
     * @return array
19
     */
20 3
    public static function all(): array
21
    {
22 3
        return static::strip(Scanner::all());
0 ignored issues
show
Documentation introduced by
\WyriHaximus\TwigView\Lib\Scanner::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...
23
    }
24
25
    /**
26
     * Return all templates for a given plugin.
27
     *
28
     * @param string $plugin The plugin to find all templates for.
29
     *
30
     * @return mixed
31
     */
32 2
    public static function plugin($plugin)
33
    {
34 2
        return static::strip([
0 ignored issues
show
Documentation introduced by
array($plugin => \WyriHa...anner::plugin($plugin)) is of type array<string,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...
35 2
            $plugin => Scanner::plugin($plugin),
36 2
        ])[$plugin];
37
    }
38
39
    /**
40
     * Strip the absolute path of template's paths for all given sections.
41
     *
42
     * @param string $sections Sections to iterate over.
43
     *
44
     * @return array
45
     */
46 5
    protected static function strip($sections): array
47
    {
48 5
        foreach ($sections as $section => $paths) {
0 ignored issues
show
Bug introduced by
The expression $sections of type string is not traversable.
Loading history...
49 5
            $sections[$section] = static::stripAbsolutePath($paths, $section == 'APP' ? null : $section);
50
        }
51
52 5
        return $sections;
53
    }
54
55
    /**
56
     * Strip the absolute path of template's paths.
57
     *
58
     * @param array       $paths  Paths to strip.
59
     * @param string|null $plugin Hold plugin name or null for App.
60
     *
61
     * @return array
62
     */
63 5
    protected static function stripAbsolutePath(array $paths, $plugin = null): array
64
    {
65 5
        if ($plugin === null) {
66 3
            $allPaths = App::path('templates');
67
        } else {
68 4
            $allPaths = [Plugin::templatePath($plugin)];
69
        }
70
71 5
        foreach ($allPaths as $templatesPath) {
72
            array_walk($paths, function (&$path) use ($templatesPath) {
73 5
                if (substr($path, 0, strlen($templatesPath)) == $templatesPath) {
74 5
                    $path = substr($path, strlen($templatesPath));
75
                }
76 5
            });
77
        }
78
79 5
        return $paths;
80
    }
81
}
82