PathResolver::realpath()   C
last analyzed

Complexity

Conditions 16
Paths 100

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 136.4601

Importance

Changes 0
Metric Value
dl 0
loc 53
ccs 6
cts 27
cp 0.2222
rs 5.5666
c 0
b 0
f 0
cc 16
nc 100
nop 2
crap 136.4601

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\ParserReflection\Instrument;
14
15
/**
16
 * Special class for resolving path for different file systems, wrappers, etc
17
 *
18
 * @see  http://stackoverflow.com/questions/4049856/replace-phps-realpath/4050444
19
 * @see  http://bugs.php.net/bug.php?id=52769
20
 *
21
 * @link https://github.com/goaop/framework/blob/master/src/Instrument/PathResolver.php
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 3052
     * @return array|bool|string
33
     */
34
    public static function realpath($somePath, $shouldCheckExistence = false)
35 3052
    {
36
        // Do not resolve empty string/false/arrays into the current path
37
        if (!$somePath) {
38
            return $somePath;
39 3052
        }
40
41
        if (is_array($somePath)) {
42
            return array_map([__CLASS__, __FUNCTION__], $somePath);
43 3052
        }
44 3052
        // Trick to get scheme name and path in one action. If no scheme, then there will be only one part
45
        $components = explode('://', $somePath, 2);
46
        [$pathScheme, $path] = isset($components[1]) ? $components : [null, $components[0]];
0 ignored issues
show
Bug introduced by Lisachenko Alexander
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 Lisachenko Alexander
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 3052
48 3052
        // Optimization to bypass complex logic for simple paths (eg. not in phar archives)
49
        if (!$pathScheme && ($fastPath = stream_resolve_include_path($somePath))) {
50
            return $fastPath;
51
        }
52
53
        $isRelative = !$pathScheme && ($path[0] !== '/') && ($path[1] !== ':');
0 ignored issues
show
Bug introduced by Alexander Lisachenko
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
        if ($isRelative) {
55
            $path = getcwd() . DIRECTORY_SEPARATOR . $path;
0 ignored issues
show
Bug introduced by Alexander Lisachenko
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
        // resolve path parts (single dot, double dot and double delimiters)
59
        $path = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path);
0 ignored issues
show
Bug introduced by Lisachenko Alexander
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
        if (strpos($path, '.') !== false) {
61
            $parts     = explode(DIRECTORY_SEPARATOR, $path);
62
            $absolutes = [];
63
            foreach ($parts as $part) {
64
                if ('.' === $part) {
65
                    continue;
66
                }
67
68
                if ('..' === $part) {
69
                    array_pop($absolutes);
70
                } else {
71
                    $absolutes[] = $part;
72
                }
73
            }
74
            $path = implode(DIRECTORY_SEPARATOR, $absolutes);
75
        }
76
77
        if ($pathScheme) {
78
            $path = "{$pathScheme}://{$path}";
79
        }
80
81
        if ($shouldCheckExistence && !file_exists($path)) {
82
            return false;
83
        }
84
85
        return $path;
86
    }
87
}
88