-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFindUnusedClasses.php
129 lines (111 loc) · 3.57 KB
/
FindUnusedClasses.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class FindUnusedClasses extends Command
{
protected $defaultPaths = [];
protected $classNames = [];
protected $controllerNames = [];
protected $massiveString = '';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'findunused:classes';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Find unused classes';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->defaultPaths = collect([
app_path(),
]);
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->populateControllerNamesFromRoutes();
$this->defaultPaths->each(function ($path) {
$phpFiles = collect(File::allFiles($path))->filter(fn ($filename) => Str::endsWith($filename, '.php'))->each(function ($phpFile) {
$fileContents = file_get_contents($phpFile);
if (preg_match('/class\s+(\w+)/', $fileContents, $className) === 1) {
$this->classNames[$className[1]] = $phpFile->getPathName();
$fileContents = str_replace($className[1], Str::random(16), $fileContents);
}
$this->massiveString .= $fileContents;
});
});
foreach ($this->classNames as $className => $files) {
$matches = [];
if (preg_match("/$className/", $this->massiveString, $matches) === 1 or $this->isARegisteredController($className)) {
unset($this->classNames[$className]);
}
}
dump($this->classNames);
}
public function populateControllerNamesFromRoutes()
{
$routes = \Route::getRoutes();
foreach ($routes as $route) {
if (! str_contains($route->getAction()['controller'], 'App')) {
continue;
}
[$controller, $method] = explode('@', $route->getAction()['controller']);
$this->controllerNames[] = class_basename($controller);
}
}
public function isARegisteredController($className)
{
return in_array($className, $this->controllerNames);
}
public function ignoreCommonStuff($funcName, $fileName)
{
if ($funcName == 'handle' and preg_match('/(Middleware|Listeners|Commands)/', $fileName) === 1) {
return true;
}
if ($funcName == 'broadcastOn' and preg_match('/Events/', $fileName) === 1) {
return true;
}
return in_array($funcName, $this->crudNames) and Str::contains($fileName, 'Controller');
}
public function shouldConsider($filename)
{
if (Str::contains($filename, 'ServiceProvider')) {
return false;
}
if (Str::contains($filename, 'Policies')) {
return false;
}
if (Str::contains($filename, 'Observers')) {
return false;
}
return true;
}
protected function mangleLaravelNames($fName)
{
$match = '';
if (preg_match('/^scope(.+$)/', $fName, $match) === 1) {
return Str::camel($match[1]);
}
if (preg_match('/^(get|set)(.+)Attribute$/', $fName, $match) === 1) {
return Str::snake($match[2]);
}
return $fName;
}
}