Completed
Push — master ( 2377d3...b3978b )
by
unknown
42:22 queued 28:51
created

src/Extension/Folders/folders.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace League\Plates\Extension\Folders;
4
5
use League\Plates;
6
use League\Plates\Extension\Path\ResolvePathArgs;
7
8
function foldersResolvePath(array $folders, $sep = '::', $file_exists = 'file_exists') {
9
    return function(ResolvePathArgs $args, $next) use ($folders, $sep, $file_exists) {
10 16
        if (strpos($args->path, $sep) === false) {
11 16
            return $next($args);
12
        }
13
14 4
        list($folder, $name) = explode($sep, $args->path);
15 4
        if (!isset($folders[$folder])) {
16
            return $next($args);
17
        }
18 4
        $folder_struct = $folders[$folder];
19
20 4
        foreach ($folder_struct['prefixes'] as $prefix) {
21 4
            $path = $next($args->withPath(
22 4
                Plates\Util\joinPath([$prefix, $name])
23
            ));
24
25
            // no need to check if file exists if we only have prefix
26 4
            if (count($folder_struct['prefixes']) == 1 || $file_exists($path)) {
27 4
                return $path;
28
            }
29
        }
30
31
        // none of the paths matched, just return what we have.
32
        return $path;
0 ignored issues
show
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...
33 16
    };
34
}
35
36
function stripFoldersNormalizeName(array $folders, $sep = '::') {
37
    return function($name) use ($folders, $sep) {
38 12
        foreach ($folders as $folder) {
39 4
            foreach (array_filter($folder['prefixes']) as $prefix) {
40 4
                if (strpos($name, $prefix) === 0) {
41 4
                    return $folder['folder'] . $sep . substr($name, strlen($prefix) + 1);
42
                }
43
            }
44
        }
45
46 12
        return $name;
47 16
    };
48
}
49