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

RelativeScanner   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 40%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 62
ccs 6
cts 15
cp 0.4
rs 10
c 0
b 0
f 0

4 Methods

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