path.php ➔ stripPrefixNormalizeName()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 1
nop 1
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 3
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace League\Plates\Extension\Path;
4
5
use League\Plates;
6
7
function resolvePathCompose(callable $resolve_path) {
8
    return function(Plates\Template $template) use ($resolve_path) {
9 24
        return $template->with('path', $resolve_path(ResolvePathArgs::fromTemplate($template, $resolve_path)));
0 ignored issues
show
Unused Code introduced by RJ Garcia
The call to ResolvePathArgs::fromTemplate() has too many arguments starting with $resolve_path.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
10 24
    };
11
}
12
13
function normalizeNameCompose(callable $normalize_name) {
14
    return function(Plates\Template $template) use ($normalize_name) {
15 32
        return $template->with(
16 32
            'normalized_name',
17 32
             Plates\Util\isPath($template->name) ? $normalize_name($template->get('path')) : $template->name
18
        );
19 32
    };
20
}
21
22
function stripExtNormalizeName() {
23
    return function($name) {
24 24
        $ext = pathinfo($name, PATHINFO_EXTENSION);
25 24
        if (!$ext) {
26 4
            return $name;
27
        }
28
29 20
        return substr($name, 0, (strlen($ext) + 1) * -1); // +1 for the leading `.`
30 32
    };
31
}
32
33
function stripPrefixNormalizeName(array $prefixes) {
34 32
    $prefixes = array_filter($prefixes);
35
    return function($name) use ($prefixes) {
36 24
        foreach ($prefixes as $prefix) {
37 20
            if (strpos($name, $prefix . '/') === 0) {
38 20
                return substr($name, strlen($prefix) + 1); // +1 for the trailing `/`
39
            }
40
        }
41
42 8
        return $name;
43 32
    };
44
}
45
46
/** appends an extension to the name */
47
48
function extResolvePath($ext = 'phtml') {
49 32
    $full_ext = '.' . $ext;
50 32
    $ext_len = strlen($full_ext);
51
    return function(ResolvePathArgs $args, $next) use ($full_ext, $ext_len) {
52
        // ext is already there, just skip
53 32
        if (strrpos($args->path, $full_ext) === strlen($args->path) - $ext_len) {
54 8
            return $next($args);
55
        }
56
57 28
        return $next($args->withPath($args->path . $full_ext));
58 32
    };
59
}
60
61
function prefixResolvePath(array $prefixes, $file_exists = 'file_exists') {
62
    return function(ResolvePathArgs $args, $next) use ($prefixes, $file_exists) {
63 28
        if (!$prefixes) {
0 ignored issues
show
Bug Best Practice introduced by RJ Garcia
The expression $prefixes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
64
            return $next($args);
65
        }
66
67 28
        foreach ($prefixes as $cur_prefix) {
68 28
            $path = Plates\Util\isAbsolutePath($args->path)
69 16
                ? $next($args)
70 20
                : $next($args->withPath(
71 28
                    Plates\Util\joinPath([$cur_prefix, $args->path])
72
                ));
73
74
            // we have a match, let's return
75 28
            if ($file_exists($path)) {
76 28
                return $path;
77
            }
78
79
            // at this point, we need to try the next prefix, but before we do, let's strip the prefix
80
            // if there is one since this might a be a relative path
81 4
            $stripped_args = null;
82 4
            foreach ($prefixes as $prefix) {
83 4
                if (strpos($path, $prefix) === 0) {
84 4
                    $stripped_args = $args->withPath(substr($path, strlen($prefix))); // remove the prefix
85 4
                    break;
86
                }
87
            }
88
89
            // could not strip the prefix, so there's not point in continuing on
90 4
            if (!$stripped_args) {
91
                return $path;
92
            }
93
94 4
            $args = $stripped_args;
95
        }
96
97
        // at this point, none of the paths resolved into a valid path, let's just return the last one
98 4
        return $path;
0 ignored issues
show
Bug introduced by RJ Garcia
The variable $path does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
99 28
    };
100
}
101
102
/** Figures out the path based off of the parent templates current path */
103
function relativeResolvePath() {
104
    return function(ResolvePathArgs $args, $next) {
105 32
        $is_relative = Plates\Util\isRelativePath($args->path) && $args->template->parent;
106
107 32
        if (!$is_relative) {
108 28
            return $next($args); // nothing to do
109
        }
110
111 12
        $current_directory = dirname($args->template->parent()->get('path'));
112 12
        return $next($args->withPath(
113 12
            Plates\Util\joinPath([$current_directory, $args->path])
114
        ));
115 32
    };
116
}
117
118
function idResolvePath() {
119
    return function(ResolvePathArgs $args, $next) {
0 ignored issues
show
Unused Code introduced by RJ Garcia
The parameter $next is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
120 52
        return $args->path;
121 52
    };
122
}
123