Completed
Push — 1.x ( c183a4...05b51a )
by Alexander
8s
created

PathResolver::realpath()   C

Complexity

Conditions 16
Paths 100

Size

Total Lines 51
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 16.0116

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 51
ccs 27
cts 28
cp 0.9643
rs 5.6194
c 2
b 1
f 0
cc 16
eloc 29
nc 100
nop 2
crap 16.0116

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
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2014, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Instrument;
12
13
/**
14
 * Special class for resolving path for different file systems, wrappers, etc
15
 *
16
 * @see http://stackoverflow.com/questions/4049856/replace-phps-realpath/4050444
17
 * @see http://bugs.php.net/bug.php?id=52769
18
 */
19
class PathResolver
20
{
21
22
    /**
23
     * Custom replacement for realpath() and stream_resolve_include_path()
24
     *
25
     * @param string|array $somePath Path without normalization or array of paths
26
     * @param bool $shouldCheckExistence Flag for checking existence of resolved filename
27
     *
28
     * @return array|bool|string
29
     */
30 28
    public static function realpath($somePath, $shouldCheckExistence = false)
31
    {
32
        // Do not resolve empty string/false/arrays into the current path
33 28
        if (!$somePath) {
34
            return $somePath;
35
        }
36
37 28
        if (is_array($somePath)) {
38 1
            return array_map(array(__CLASS__, __FUNCTION__), $somePath);
39
        }
40
        // Trick to get scheme name and path in one action. If no scheme, then there will be only one part
41 28
        $components = explode('://', $somePath, 2);
42 28
        list ($pathScheme, $path) = isset($components[1]) ? $components : array(null, $components[0]);
43
44
        // Optimization to bypass complex logic for simple paths (eg. not in phar archives)
45 28
        if (!$pathScheme && ($fastPath = stream_resolve_include_path($somePath))) {
46 17
            return $fastPath;
47
        }
48
49 13
        $isRelative = !$pathScheme && ($path[0] !== '/') && ($path[1] !== ':');
50 13
        if ($isRelative) {
51 4
            $path = getcwd() . DIRECTORY_SEPARATOR . $path;
52
        }
53
54
        // resolve path parts (single dot, double dot and double delimiters)
55 13
        $path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
56 13
        if (strpos($path, '.') !== false) {
57 10
            $parts     = explode(DIRECTORY_SEPARATOR, $path);
58 10
            $absolutes = [];
59 10
            foreach ($parts as $part) {
60 10
                if ('.' == $part) {
61 2
                    continue;
62 10
                } elseif ('..' == $part) {
63 5
                    array_pop($absolutes);
64
                } else {
65 10
                    $absolutes[] = $part;
66
                }
67
            }
68 10
            $path = implode(DIRECTORY_SEPARATOR, $absolutes);
69
        }
70
71 13
        if ($pathScheme) {
72 4
            $path = "{$pathScheme}://{$path}";
73
        }
74
75 13
        if ($shouldCheckExistence && !file_exists($path)) {
76 2
            return false;
77
        }
78
79 11
        return $path;
80
    }
81
}
82