Completed
Push — 183-default-layout-ext ( a97107 )
by
unknown
10:37
created

path.php ➔ relativeResolvePath()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 1
nop 0
dl 0
loc 17
rs 9.2
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
        return $template->with('path', $resolve_path(ResolvePathArgs::fromTemplate($template, $resolve_path)));
0 ignored issues
show
Unused Code introduced by
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
    };
11
}
12
13
/** appends an extension to the name */
14
15
function extResolvePath($ext = 'phtml') {
16
    $full_ext = '.' . $ext;
17
    $ext_len = strlen($full_ext);
18
    return function(ResolvePathArgs $args, $next) use ($full_ext, $ext_len) {
19
        // ext is already there, just skip
20
        if (strrpos($args->path, $full_ext) === strlen($args->path) - $ext_len) {
21
            return $next($args);
22
        }
23
24
        return $next($args->withPath($args->path . $full_ext));
25
    };
26
}
27
28
function prefixResolvePath(array $prefixes, $file_exists = 'file_exists') {
29
    return function(ResolvePathArgs $args, $next) use ($prefixes, $file_exists) {
30
        if (!$prefixes) {
0 ignored issues
show
Bug Best Practice introduced by
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...
31
            return $next($args);
32
        }
33
34
        foreach ($prefixes as $cur_prefix) {
35
            $path = strpos($args->path, '/') === 0
36
                ? $next($args)
37
                : $next($args->withPath(
38
                    Plates\Util\joinPath([$cur_prefix, $args->path])
39
                ));
40
41
            // we have a match, let's return
42
            if ($file_exists($path)) {
43
                return $path;
44
            }
45
46
            // at this point, we need to try the next prefix, but before we do, let's strip the prefix
47
            // if there is one since this might a be a relative path
48
            $stripped_args = null;
49
            foreach ($prefixes as $prefix) {
50
                if (strpos($path, $prefix) === 0) {
51
                    $stripped_args = $args->withPath(substr($path, strlen($prefix))); // remove the prefix
52
                    break;
53
                }
54
            }
55
56
            // could not strip the prefix, so there's not point in continuing on
57
            if (!$stripped_args) {
58
                return $path;
59
            }
60
61
            $args = $stripped_args;
62
        }
63
64
        // at this point, none of the paths resolved into a valid path, let's just return the last one
65
        return $path;
0 ignored issues
show
Bug introduced by
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...
66
    };
67
}
68
69
/** Figures out the path based off of the parent templates current path */
70
function relativeResolvePath() {
71
    return function(ResolvePathArgs $args, $next) {
72
        $is_relative = (
73
            strpos($args->path, './') === 0
74
            || strpos($args->path, '../') === 0
75
        ) && $args->template->parent;
76
77
        if (!$is_relative) {
78
            return $next($args); // nothing to do
79
        }
80
81
        $current_directory = dirname($args->template->parent()->get('path'));
82
        return $next($args->withPath(
83
            Plates\Util\joinPath([$current_directory, $args->path])
84
        ));
85
    };
86
}
87
88
function idResolvePath() {
89
    return function(ResolvePathArgs $args, $next) {
0 ignored issues
show
Unused Code introduced by
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...
90
        return $args->path;
91
    };
92
}
93