CheckNamespaces::within()   C
last analyzed

Complexity

Conditions 11
Paths 11

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 11
nop 4
dl 0
loc 54
rs 6.8569
c 0
b 0
f 0

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
namespace Imanghafoori\LaravelMicroscope;
4
5
use Illuminate\Support\Str;
6
use Imanghafoori\LaravelMicroscope\Analyzers\GetClassProperties;
7
use Imanghafoori\LaravelMicroscope\Analyzers\NamespaceCorrector;
8
use Imanghafoori\LaravelMicroscope\ErrorReporters\ErrorPrinter;
9
use Imanghafoori\LaravelMicroscope\LaravelPaths\FilePath;
10
use Imanghafoori\LaravelMicroscope\LaravelPaths\LaravelPaths;
11
12
class CheckNamespaces
13
{
14
    public static $checkedNamespaces = 0;
15
16
    public static $changedNamespaces = [];
17
18
    /**
19
     * Get all of the listeners and their corresponding events.
20
     *
21
     * @param  iterable  $paths
22
     * @param  $composerPath
23
     * @param  $composerNamespace
24
     * @param  $command
25
     *
26
     * @return void
27
     */
28
    public static function within($paths, $composerPath, $composerNamespace, $command)
29
    {
30
        $detailed = $command->option('detailed');
31
32
        foreach ($paths as $classFilePath) {
33
            $absFilePath = $classFilePath->getRealPath();
34
35
            // exclude blade files
36
            if (Str::endsWith($absFilePath, ['.blade.php'])) {
37
                continue;
38
            }
39
40
            // exclude migration directories
41
            if (Str::startsWith($absFilePath, LaravelPaths::migrationDirs())) {
42
                continue;
43
            }
44
45
            if (! self::hasOpeningTag($absFilePath)) {
46
                continue;
47
            }
48
49
            [
50
                $currentNamespace,
0 ignored issues
show
Bug introduced by
The variable $currentNamespace 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...
51
                $class,
0 ignored issues
show
Bug introduced by
The variable $class 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...
52
                $type,
0 ignored issues
show
Bug introduced by
The variable $type 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...
53
                $parent,
0 ignored issues
show
Bug introduced by
The variable $parent 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...
54
            ] = GetClassProperties::fromFilePath($absFilePath);
55
56
            // Skip if there is no class/trait/interface definition found.
57
            // For example a route file or a config file.
58
            if (! $class || $parent == 'Migration') {
59
                continue;
60
            }
61
62
            $detailed && event('microscope.checking', [$classFilePath->getRelativePathname(), $command]);
63
64
            $relativePath = FilePath::getRelativePath($absFilePath);
65
            $correctNamespace = NamespaceCorrector::calculateCorrectNamespace($relativePath, $composerPath, $composerNamespace);
66
            self::$checkedNamespaces++;
67
68
            if ($currentNamespace === $correctNamespace) {
69
                continue;
70
            }
71
72
            self::changedNamespaces($class, $currentNamespace, $correctNamespace);
73
            ErrorPrinter::warnIncorrectNamespace($currentNamespace, $relativePath, $class);
74
75
            if (! $command->option('nofix') && ErrorPrinter::ask($command, $correctNamespace)) {
76
                self::doNamespaceCorrection($absFilePath, $currentNamespace, $correctNamespace);
77
                // maybe an event listener
78
                app(ErrorPrinter::class)->badNamespace($relativePath, $correctNamespace, $currentNamespace);
79
            }
80
        }
81
    }
82
83
    public static function hasOpeningTag($file)
84
    {
85
        $fp = fopen($file, 'r');
86
87
        if (feof($fp)) {
88
            return false;
89
        }
90
91
        $buffer = fread($fp, 20);
92
        fclose($fp);
93
94
        return Str::startsWith($buffer, '<?php');
95
    }
96
97
    protected static function doNamespaceCorrection($absFilePath, $currentNamespace, $correctNamespace)
98
    {
99
        event('laravel_microscope.namespace_fixing', get_defined_vars());
100
        NamespaceCorrector::fix($absFilePath, $currentNamespace, $correctNamespace);
101
        event('laravel_microscope.namespace_fixed', get_defined_vars());
102
    }
103
104
    private static function changedNamespaces($class, $currentNamespace, $correctNamespace)
105
    {
106
        $_currentClass = $currentNamespace.'\\'.$class;
107
        $_correctClass = $correctNamespace.'\\'.$class;
108
        $relPath = NamespaceCorrector::getRelativePathFromNamespace($currentNamespace);
109
        if (is_dir(base_path($relPath.DIRECTORY_SEPARATOR.$class))) {
110
            self::$changedNamespaces[$_currentClass.';'] = $_correctClass.';';
111
            self::$changedNamespaces[$_currentClass.'('] = $_correctClass.'(';
112
            self::$changedNamespaces[$_currentClass.'::'] = $_correctClass.'::';
113
            self::$changedNamespaces[$_currentClass.' as'] = $_correctClass.' as';
114
        } else {
115
            self::$changedNamespaces[$_currentClass] = $_correctClass;
116
        }
117
    }
118
}
119