PathResolver   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 96.43%

Importance

Changes 0
Metric Value
wmc 16
lcom 0
cbo 0
dl 0
loc 64
ccs 27
cts 28
cp 0.9643
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C realpath() 0 52 16
1
<?php
2
3
declare(strict_types = 1);
4
/*
5
 * Go! AOP framework
6
 *
7
 * @copyright Copyright 2014, Lisachenko Alexander <[email protected]>
8
 *
9
 * This source file is subject to the license that is bundled
10
 * with this source code in the file LICENSE.
11
 */
12
13
namespace Go\Instrument;
14
15
use function is_array;
16
17
/**
18
 * Special class for resolving path for different file systems, wrappers, etc
19
 *
20
 * @see http://stackoverflow.com/questions/4049856/replace-phps-realpath/4050444
21
 * @see http://bugs.php.net/bug.php?id=52769
22
 */
23
class PathResolver
24
{
25
26
    /**
27
     * Custom replacement for realpath() and stream_resolve_include_path()
28
     *
29
     * @param string|array $somePath Path without normalization or array of paths
30
     * @param bool $shouldCheckExistence Flag for checking existence of resolved filename
31
     *
32
     * @return array|bool|string
33 47
     */
34
    public static function realpath($somePath, bool $shouldCheckExistence = false)
35
    {
36 47
        // Do not resolve empty string/false/arrays into the current path
37
        if (!$somePath) {
38
            return $somePath;
39
        }
40 47
41 2
        if (is_array($somePath)) {
42
            return array_map([self::class, __FUNCTION__], $somePath);
43
        }
44 47
        // Trick to get scheme name and path in one action. If no scheme, then there will be only one part
45 47
        $components = explode('://', $somePath, 2);
46
        [$pathScheme, $path] = isset($components[1]) ? $components : [null, $components[0]];
0 ignored issues
show
Bug introduced by
The variable $pathScheme does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $path seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
47
48 47
        // Optimization to bypass complex logic for simple paths (eg. not in phar archives)
49 36
        if (!$pathScheme && ($fastPath = stream_resolve_include_path($somePath))) {
50
            return $fastPath;
51
        }
52 25
53 25
        $isRelative = !$pathScheme && ($path[0] !== '/') && ($path[1] !== ':');
0 ignored issues
show
Bug introduced by
The variable $path seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
54 4
        if ($isRelative) {
55
            $path = getcwd() . DIRECTORY_SEPARATOR . $path;
0 ignored issues
show
Bug introduced by
The variable $path seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
56
        }
57
58 25
        // resolve path parts (single dot, double dot and double delimiters)
59 25
        $path = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $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...
60 22
        if (strpos($path, '.') !== false) {
61 22
            $parts     = explode(DIRECTORY_SEPARATOR, $path);
62 22
            $absolutes = [];
63 22
            foreach ($parts as $part) {
64 2
                if ('.' === $part) {
65
                    continue;
66 22
                }
67 17
                if ('..' === $part) {
68
                    array_pop($absolutes);
69 22
                } else {
70
                    $absolutes[] = $part;
71
                }
72 22
            }
73
            $path = implode(DIRECTORY_SEPARATOR, $absolutes);
74
        }
75 25
76 4
        if ($pathScheme) {
77
            $path = "{$pathScheme}://{$path}";
78
        }
79 25
80 2
        if ($shouldCheckExistence && !file_exists($path)) {
81
            return false;
82
        }
83 23
84
        return $path;
85
    }
86
}
87