-
Notifications
You must be signed in to change notification settings - Fork 0
/
use-check
executable file
·172 lines (137 loc) · 4.73 KB
/
use-check
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env php
<?php
/**
* FollowUpBoss: Real Estate Lead Management Software.
*
* @author Kuno Woudt <[email protected]>
* @copyright Copyright (c) 2017, Enchant LLC.
* @license Property of Enchant LLC. All rights reserved.
*
* This file requires the ast extension, see:
*
* https://github.com/nikic/php-ast#installation
*
*/
class Check {
public $exitCode = 0;
public $verbose = false;
public $debug = false;
public function __construct($filename) {
$this->filename = $filename;
}
public function log($priority, $msg, $node) {
if ($priority == "warning") {
$prefix = "[" . chr(27) . "[33;1mwarn" . chr(27) . "[0m] ";
} else {
$prefix = "[" . chr(27) . "[31mfail" . chr(27) . "[0m] ";
}
$prefix .= preg_replace(",^/var/www/fub/,", "", $this->filename)
. ":" . $node->lineno . " " . strtoupper($priority) . ": ";
$this->exitCode = 1;
echo "$prefix $msg\n";
if ($this->verbose) {
print_r($node);
}
}
public static function walker($root) {
if (!is_object($root) || !property_exists ($root, 'children')) {
return;
}
yield $root;
foreach ($root->children as $child) {
$nodes = static::walker($child);
foreach ($nodes as $n) {
yield $n;
}
}
}
public function run () {
$root = ast\parse_file($this->filename, $version=50);
$iterator = static::walker($root);
$imports = [];
$classUsed = [];
foreach ($iterator as $i) {
if ($i->kind == ast\AST_USE_ELEM) {
if (empty($i->children['alias'])) {
$importName = preg_replace(",^.*\\\\,", "", $i->children['name']);
} else {
$importName = $i->children['alias'];
}
$imports[$importName] = $i;
}
// Find classes used in Catch()
if ($i->kind == ast\AST_CATCH
&& !empty($i->children["class"])
&& $i->children["class"]->kind == ast\AST_NAME_LIST
) {
foreach ($i->children['class']->children as $child) {
$key = $child->children["name"];
if (empty($classUsed[$key])) {
$classUsed[$key] = $i;
}
}
}
// Find classes used elsewhere
if (!empty($i->children["class"])
&& !empty($i->children["class"]->children["name"])
&& $i->children["class"]->kind == ast\AST_NAME
) {
$key = $i->children["class"]->children["name"];
if ($this->debug) {
echo "className: $key, kind: "
. ast\get_kind_name($i->kind) . ", "
. ast\get_kind_name($i->children["class"]->kind) . "\n";
}
if (empty($classUsed[$key])) {
$classUsed[$key] = $i;
}
}
}
foreach ($classUsed as $className => $node) {
if (in_array($className, ['parent', 'self', 'static'])) {
continue;
}
if (strpos($className, "\\") !== false) {
// assume an absolute import like "\Httpful\Bootstrap" which doesn't need a 'use'
// FIXME: ast strips the leading "\" from the name, figure out how to properly
// distinguish an absolute import form a relative one.
continue;
}
// FIXME: look for class definitions in the current file, instead of
// relying on filename == classname
if (basename($this->filename, '.php') == $className) {
continue;
}
if (!array_key_exists($className, $imports)) {
$this->log("error", "{$className} not imported", $node);
}
}
foreach ($imports as $className => $node) {
if (!array_key_exists($className, $classUsed)) {
$this->log("warning", "{$className} imported, but not used", $node);
}
}
return $this->exitCode;
}
}
function main() {
global $argv;
$filenames = [];
$verbose = false;
array_shift($argv);
foreach ($argv as $a) {
if ($a == '--verbose') {
$verbose = true;
} else {
$filenames[] = realpath($a);
}
}
$errors = 0;
foreach ($filenames as $fn) {
$check = new Check($fn);
$check->verbose = $verbose;
$errors += $check->run();
}
exit ($errors > 0 ? 1 : 0);
}
main();