Passed
Push — master ( 145b60...d8f516 )
by 世昌
01:51
created

PathTrait::toAbsolutePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\autoloader;
3
4
/**
5
 * 自动加载路径处理
6
 *
7
 */
8
trait PathTrait
9
{    
10
    public static function formatSeparator(string $path):string
11
    {
12
        return str_replace(['\\','/'], DIRECTORY_SEPARATOR, $path);
13
    }
14
15
    public static function toAbsolutePath(string $path, string $separator = DIRECTORY_SEPARATOR):string{
0 ignored issues
show
Unused Code introduced by
The parameter $separator is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

15
    public static function toAbsolutePath(string $path, /** @scrutinizer ignore-unused */ string $separator = DIRECTORY_SEPARATOR):string{

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
16
        list($scheme, $path) = static::parsePathSchemeSubpath($path);
17
        list($root, $path) = static::parsePathRootSubpath($path);
18
        $path = static::parsePathRelativePath($path);
19
        return $scheme.$root.$path;
20
    }
21
22
    protected static function parsePathRootSubpath(string $path):array {
23
        $subpath = str_replace(['/', '\\'], '/', $subpath);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $subpath seems to be never defined.
Loading history...
24
        $root = null;
25
        if (DIRECTORY_SEPARATOR === '/') {
26
            if (strpos($path, '/') !== 0) {
27
                $subpath = getcwd().DIRECTORY_SEPARATOR.$subpath;
28
            }
29
            $root = '/';
0 ignored issues
show
Unused Code introduced by
The assignment to $root is dead and can be removed.
Loading history...
30
            $subpath = substr($subpath, 1);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return array. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
Unused Code introduced by
The assignment to $subpath is dead and can be removed.
Loading history...
31
        } else {
32
            if (strpos($subpath, ':/') === false) {
33
                $subpath=str_replace(['/', '\\'], '/', getcwd()).'/'.$subpath;
34
            }
35
            list($root, $subpath) = explode(':/', $subpath, 2);
36
            $root .= ':'.DIRECTORY_SEPARATOR;
37
        }
38
    }
39
40
    protected static function parsePathRelativePath(string $path, string $separator = DIRECTORY_SEPARATOR):string {
41
        $subpathArr = explode('/', $path);
42
        $absulotePaths = [];
43
        foreach ($subpathArr as $name) {
44
            $name = trim($name);
45
            if ($name === '..') {
46
                array_pop($absulotePaths);
47
            } elseif ($name === '.') {
48
                continue;
49
            } elseif (strlen($name)) {
50
                $absulotePaths[]=$name;
51
            }
52
        }
53
        return implode($separator, $absulotePaths);
54
    }
55
56
    protected static function parsePathSchemeSubpath(string $path):array
57
    {
58
        if (static::getHomePath() !== null && strpos($path, '~') === 0) {
0 ignored issues
show
Bug introduced by
Are you sure the usage of static::getHomePath() targeting nebula\autoloader\PathTrait::getHomePath() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
introduced by
The condition static::getHomePath() !== null is always false.
Loading history...
59
            $scheme ='';
60
            $subpath = static::getHomePath() .DIRECTORY_SEPARATOR.substr($path, 1);
61
        } elseif (strpos($path, '://') !== false) {
62
            list($scheme, $subpath) = explode('://', $path, 2);
63
            $scheme.='://';
64
        } else {
65
            $scheme ='';
66
            $subpath = $path;
67
        }
68
        return [$scheme, $subpath];
69
    }
70
71
72
    public static function getHomePath():?string {
73
        return null;
74
    }
75
}
76