|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Imanghafoori\LaravelMicroscope; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Imanghafoori\LaravelMicroscope\Analyzers\ComposerJson; |
|
7
|
|
|
use Imanghafoori\LaravelMicroscope\LaravelPaths\FilePath; |
|
8
|
|
|
|
|
9
|
|
|
class Psr4Classes |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
public static $allNamespaces = []; |
|
15
|
|
|
|
|
16
|
|
|
public static $checkedFilesNum = 0; |
|
17
|
|
|
|
|
18
|
|
|
public static function check($checks) |
|
19
|
|
|
{ |
|
20
|
|
|
$psr4 = ComposerJson::readAutoload(); |
|
21
|
|
|
|
|
22
|
|
|
foreach ($psr4 as $psr4Namespace => $psr4Path) { |
|
23
|
|
|
$files = FilePath::getAllPhpFiles($psr4Path); |
|
24
|
|
|
foreach ($files as $classFilePath) { |
|
25
|
|
|
self::$checkedFilesNum++; |
|
26
|
|
|
$absFilePath = $classFilePath->getRealPath(); |
|
27
|
|
|
|
|
28
|
|
|
$tokens = token_get_all(file_get_contents($absFilePath)); |
|
29
|
|
|
|
|
30
|
|
|
foreach ($checks as $check) { |
|
31
|
|
|
$check::check($tokens, $absFilePath, $classFilePath, $psr4Path, $psr4Namespace); |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public static function classList() |
|
38
|
|
|
{ |
|
39
|
|
|
$psr4 = ComposerJson::readAutoload(); |
|
40
|
|
|
|
|
41
|
|
|
if (self::$allNamespaces) { |
|
|
|
|
|
|
42
|
|
|
return self::$allNamespaces; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
foreach ($psr4 as $psr4Namespace => $psr4Path) { |
|
46
|
|
|
$files = FilePath::getAllPhpFiles($psr4Path); |
|
47
|
|
|
foreach ($files as $classFilePath) { |
|
48
|
|
|
$fileName = $classFilePath->getFilename(); |
|
49
|
|
|
if (Str::endsWith($fileName, ['.blade.php'])) { |
|
50
|
|
|
continue; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$filePath = \str_replace(base_path(), '', $classFilePath->getRealPath()); |
|
54
|
|
|
|
|
55
|
|
|
$composerPath = \str_replace('/', '\\', $psr4Path); |
|
56
|
|
|
|
|
57
|
|
|
// replace composer base_path with composer namespace |
|
58
|
|
|
/** |
|
59
|
|
|
* "psr-4": { |
|
60
|
|
|
* "App\\": "app/" |
|
61
|
|
|
* }. |
|
62
|
|
|
*/ |
|
63
|
|
|
$ns = Str::replaceFirst(\trim($composerPath, '\\'), \trim($psr4Namespace, '\\/'), $filePath); |
|
64
|
|
|
$t = \str_replace('.php', '', [$ns, $fileName]); |
|
65
|
|
|
$t = \str_replace('/', '\\', $t); // for linux environments. |
|
66
|
|
|
|
|
67
|
|
|
self::$allNamespaces[$t[1]][] = \trim($t[0], '\\'); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return self::$allNamespaces; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.