-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcheck_imports.php
58 lines (48 loc) · 1.42 KB
/
check_imports.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
<?php
require_once 'vendor/autoload.php';
function findAllPhpFiles($dir, &$allFiles = []) {
$files = scandir($dir);
foreach ($files as $file) {
if ($file === '.' || $file === '..') continue;
$path = $dir . DIRECTORY_SEPARATOR . $file;
if (is_dir($path)) {
findAllPhpFiles($path, $allFiles);
} elseif (pathinfo($path, PATHINFO_EXTENSION) === 'php') {
$allFiles[] = $path;
}
}
return $allFiles;
}
function extractUseStatements($filePath) {
$content = file_get_contents($filePath);
preg_match_all('/^use\s+([\w\\\]+);/m', $content, $matches);
return $matches[1];
}
function checkClassExistence($classes) {
$missingClasses = [];
foreach ($classes as $class) {
if (!class_exists($class) && !interface_exists($class) && !trait_exists($class)) {
$missingClasses[] = $class;
}
}
return $missingClasses;
}
// Main process
$directoryToScan = 'src';
$phpFiles = findAllPhpFiles($directoryToScan);
$allClasses = [];
foreach ($phpFiles as $file) {
$classes = extractUseStatements($file);
$allClasses = array_merge($allClasses, $classes);
}
$allClasses = array_unique($allClasses);
$missingClasses = checkClassExistence($allClasses);
if (!empty($missingClasses)) {
echo "\nMissing classes:\n";
print_r($missingClasses);
exit(1);
} else {
echo "\nAll classes are accounted for.\n";
exit(0);
}
?>