NamespaceCorrector::getNamespaceFromRelativePath()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\LaravelMicroscope\Analyzers;
4
5
use Illuminate\Support\Str;
6
7
class NamespaceCorrector
8
{
9
    public static function getNamespaceFromFullClass($class)
10
    {
11
        $segments = explode('\\', $class);
12
        array_pop($segments); // removes the last part
13
14
        return trim(implode('\\', $segments), '\\');
15
    }
16
17
    public static function haveSameNamespace($class1, $class2)
18
    {
19
        return self::getNamespaceFromFullClass($class1) == self::getNamespaceFromFullClass($class2);
20
    }
21
22
    public static function fix($classFilePath, $incorrectNamespace, $correctNamespace)
23
    {
24
        // decides to add namespace (in case there is no namespace) or edit the existing one.
25
        [$oldLine, $newline] = self::getNewLine($incorrectNamespace, $correctNamespace);
0 ignored issues
show
Bug introduced by
The variable $oldLine 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...
Bug introduced by
The variable $newline 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...
26
27
        $oldLine = \ltrim($oldLine, '\\');
0 ignored issues
show
Bug introduced by
The variable $oldLine 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...
28
        FileManipulator::replaceFirst($classFilePath, $oldLine, $newline);
29
    }
30
31
    public static function calculateCorrectNamespace($relativeClassPath, $composerPath, $rootNamespace)
32
    {
33
        // removes the filename.php from the end of the string
34
        $classPath = \explode(DIRECTORY_SEPARATOR, $relativeClassPath);
35
        // removes the filename
36
        array_pop($classPath);
37
        // ensure back slashes.
38
        $classPath = \implode('\\', $classPath);
39
40
        $composerPath = \str_replace('/', '\\', $composerPath);
41
42
        // replace composer base_path with composer namespace
43
        /**
44
         *  "psr-4": {
45
         *      "App\\": "app/"
46
         *  }.
47
         */
48
        return Str::replaceFirst(\trim($composerPath, '\\'), \trim($rootNamespace, '\\/'), $classPath);
49
    }
50
51
    private static function getNewLine($incorrectNamespace, $correctNamespace)
52
    {
53
        if ($incorrectNamespace) {
54
            return [$incorrectNamespace, $correctNamespace];
55
        }
56
57
        // In case there is no namespace specified in the file:
58
        return ['<?php', '<?php'.PHP_EOL.PHP_EOL.'namespace '.$correctNamespace.';'.PHP_EOL];
59
    }
60
61
    public static function getRelativePathFromNamespace($namespace)
62
    {
63
        $autoload = ComposerJson::readAutoload();
64
        uksort($autoload, function ($a, $b) {
65
            return strlen($b) <=> strlen($a);
66
        });
67
        $namespaces = array_keys($autoload);
68
        $paths = array_values($autoload);
69
70
        return \str_replace(['\\', '/'], DIRECTORY_SEPARATOR, \str_replace($namespaces, $paths, $namespace));
71
    }
72
73
    public static function getNamespaceFromRelativePath($relPath, $autoload = null)
74
    {
75
        // Remove .php from class path
76
        $relPath = str_replace([base_path(), '.php'], '', $relPath);
77
78
        ($autoload === null) && $autoload = ComposerJson::readAutoload();
79
        uksort($autoload, function ($namespace1, $namespace2) {
80
            return strlen($namespace2) <=> strlen($namespace1);
81
        });
82
83
        $namespaces = array_keys($autoload);
84
        $paths = array_values($autoload);
85
86
        $relPath = \str_replace('\\', '/', $relPath);
87
88
        return trim(\str_replace('/', '\\', \str_replace($paths, $namespaces, $relPath)), '\\');
89
    }
90
}
91