|
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, |
|
|
|
|
|
|
51
|
|
|
$class, |
|
|
|
|
|
|
52
|
|
|
$type, |
|
|
|
|
|
|
53
|
|
|
$parent, |
|
|
|
|
|
|
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
|
|
|
|
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.