-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_imports
82 lines (61 loc) · 2.53 KB
/
check_imports
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
<?php
use Imanghafoori\ImportAnalyzer\Analyzers\ComposerJson;
use Imanghafoori\ImportAnalyzer\ErrorReporters\ErrorPrinter;
use Imanghafoori\ImportAnalyzer\Psr4Report;
use Imanghafoori\ImportAnalyzer\CheckClassReferencesAreValid;
use Imanghafoori\ImportAnalyzer\Handlers\PrintWrongClassRefs;
use Imanghafoori\ImportAnalyzer\FileReaders\FilePath;
use Imanghafoori\ImportAnalyzer\ForPsr4LoadedClasses;
use Imanghafoori\ImportAnalyzer\Reporters\CheckImportReporter;
use Imanghafoori\ImportAnalyzer\Reporters\SummeryReport;
use Imanghafoori\TokenAnalyzer\ImportsAnalyzer;
use Imanghafoori\TokenAnalyzer\ParseUseStatement;
use Symfony\Component\Console\Output\ConsoleOutput;
if (isset($GLOBALS['_composer_autoload_path'])) {
define('COMPOSER_INSTALL', $GLOBALS['_composer_autoload_path']);
unset($GLOBALS['_composer_autoload_path']);
} else {
foreach ([__DIR__.'/../../autoload.php', __DIR__.'/../vendor/autoload.php', __DIR__.'/vendor/autoload.php'] as $file) {
if (file_exists($file)) {
define('COMPOSER_INSTALL', $file);
break;
}
}
unset($file);
}
function base_path() {
$comp = '';
if (file_exists(getcwd().'/../../../composer.json')) {
$comp = getcwd().'/../../..';
} elseif (file_exists(getcwd().'/composer.json')) {
// for dev
$comp = getcwd();
}
return $comp;
}
require_once base_path().'/vendor/autoload.php';
require_once COMPOSER_INSTALL;
ComposerJson::$composer = function () {
$comp = FilePath::$basePath = base_path();
return \ImanGhafoori\ComposerJson\ComposerJson::make($comp);
};
CheckClassReferencesAreValid::$wrongClassRefsHandler = PrintWrongClassRefs::class;
$psr4Stats = ForPsr4LoadedClasses::check([CheckClassReferencesAreValid::class], function ($tokens) {
$imports = ParseUseStatement::parseUseStatements($tokens);
return $imports[0] ?: [$imports[1]];
});
$output = new ConsoleOutput();
$output->writeln('');
$output->writeln('<fg=green>Checking imports and class references...</>');
$errorPrinter = ErrorPrinter::singleton($output);
$refCount = ImportsAnalyzer::$checkedRefCount;
$errorPrinter->logErrors();
$messages = [];
$messages[] = CheckImportReporter::totalImportsMsg($refCount);
$messages[] = Psr4Report::printPsr4($psr4Stats);
$messages[] = SummeryReport::summery($errorPrinter->errorsList);
if (! $refCount) {
$messages = ['<options=bold;fg=yellow>No imports were found!</>'];
}
$output->writeln(implode(PHP_EOL, array_filter($messages)));
return count(ErrorPrinter::singleton($output)->errorsList) === 0 ? 0 : 1;