-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp-cheeky-token-checker.php
More file actions
217 lines (176 loc) · 5.28 KB
/
php-cheeky-token-checker.php
File metadata and controls
217 lines (176 loc) · 5.28 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* Run PHP token analysis on the specified file to report on:
* [] Any dangerous / uncommon / noteworthy language features used
* [] Recommended PHP minimum and maximum versions required to run the specified file
* [] Top 5 variables used
* [] Top 5 identifiers used
* {We also report on if the file lints or not}
*
* @link https://www.php.net/manual/en/tokens.php
*/
//TODO: are 'messy', 'risky', 'advanced' interesting categories of language features?
//----input and set up----------------
$FILENAME = $argv[1];
$IGNORE_TOKENS = ['T_WHITESPACE', 'T_COMMENT', 'T_DOC_COMMENT'];
$SAFE_PREFIX_CHAR_TOKEN = 'char_'; //for 1-char tokens
$SAFE_PREFIX_TEXT_TOKEN = 'text_';
$source = file_get_contents($FILENAME);
$fileTokens = token_get_all($source);
$tokens = ['T_ONE_CHAR' => []]; //note 'T_ONE_CHAR' is our creation
//----build tokens array----------------
foreach ($fileTokens as $token) {
// simple 1-character token
if (is_string($token)) {
$safeTokenText = "{$SAFE_PREFIX_CHAR_TOKEN}{$token}";
if (!array_key_exists($safeTokenText, $tokens['T_ONE_CHAR'])) {
$tokens['T_ONE_CHAR'][$safeTokenText] = 0;
}
$tokens['T_ONE_CHAR'][$safeTokenText]++;
} else { // token array
list($id, $tokenText, $lineNumber) = $token;
$tokenName = token_name($id);
if (in_array($tokenName, $IGNORE_TOKENS)) {
continue; //skip
}
$safeTokenText = "{$SAFE_PREFIX_TEXT_TOKEN}{$tokenText}";
if (!array_key_exists($tokenName, $tokens)) {
$tokens[$tokenName] = [];
}
if (!array_key_exists($safeTokenText, $tokens[$tokenName])) {
$tokens[$tokenName][$safeTokenText] = 0;
}
$tokens[$tokenName][$safeTokenText]++;
}
}
//----test for presence of certain token types----------------
//lint check above all else
$lastLine = exec("php -l {$FILENAME}", $lines, $returnCode);
$lintSuccess = $returnCode === 0;
$report = ['lints' => $lintSuccess];
//if multiple, it's an AND test
$tests = [
'openingtag' => [
'T_OPEN_TAG'
],
'closingtag' => [
'T_CLOSE_TAG'
],
'namespace' => [
'T_NAMESPACE'
],
'use' => [
'T_USE'
],
'heredoc/nowdoc' => [
'T_START_HEREDOC',
'T_END_HEREDOC'
],
'switchcase' => [
'T_SWITCH',
'T_CASE'
],
'goto' => [
'T_GOTO'
],
'inlinehtml' => [
'T_INLINE_HTML'
],
'eval' => [
'T_EVAL'
],
'class' => [
'T_CLASS'
],
'attributes' => [
'T_ATTRIBUTE'
],
];
//score the tests
foreach ($tests as $testName => $testTokens) {
$result = true;
foreach ($testTokens as $testToken) {
if (!array_key_exists($testToken, $tokens)) {
$result = false;
continue;//skip
}
}
$report[$testName] = $result;
}
//minimum version suggestions
$versions = [
'T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG' => '8.1.0',
'T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG' => '8.1.0',
'T_ATTRIBUTE' => '8.0.0',
'T_COALESCE_EQUAL' => '7.4.0',
'T_ENUM' => '8.1.0',
'T_FN' => '7.4.0',
'T_MATCH' => '8.0.0',
'T_READONLY' => '8.1.0',
];
$versionMatches = [];
foreach ($versions as $token => $version) {
if (array_key_exists($token, $tokens)) {
$versionMatches[$token] = $version;
}
}
arsort($versionMatches);
$highestTwoVersionMatches = array_slice($versionMatches, 0, 2);
//TODO: *maximum* PHP version (due to deprecated functions / features etc)
//top 5 used variables
$topFiveVariables = [];
if (array_key_exists('T_VARIABLE', $tokens)) {
$variables = $tokens['T_VARIABLE'];
arsort($variables);
$topFiveVariables = array_slice($variables, 0, 5);
}
//top 5 used identifiers
$topFiveIdentifiers = [];
if (array_key_exists('T_STRING', $tokens)) {
$identifiers = $tokens['T_STRING'];
arsort($identifiers);
$topFiveIdentifiers = array_slice($identifiers, 0, 5);
}
//----output results----------------
//debug
//print_r($tokens);
echo "Language features::" . PHP_EOL;
foreach ($report as $test => $result) {
$resultWord = getResultWord($result);
echo "{$test}: $resultWord" . PHP_EOL;
}
echo PHP_EOL;
echo "Minimum PHP version suggestions::" . PHP_EOL;
if (empty($highestTwoVersionMatches)) {
echo "{none}" . PHP_EOL;
}
foreach ($highestTwoVersionMatches as $reason => $minVersion) {
echo "Due to {$reason}: v{$minVersion}" . PHP_EOL;
}
echo PHP_EOL;
echo "Top 5 most used variables::" . PHP_EOL;
if (empty($topFiveVariables)) {
echo "{none}" . PHP_EOL;
}
foreach ($topFiveVariables as $safeVariableName => $count) {
$quotedPrefix = preg_quote($SAFE_PREFIX_TEXT_TOKEN);
$variableName = preg_replace("/^{$quotedPrefix}/", '', $safeVariableName);
echo "{$variableName}: {$count}x" . PHP_EOL;
}
echo PHP_EOL;
echo "Top 5 most used identifiers::" . PHP_EOL;
if (empty($topFiveIdentifiers)) {
echo "{none}" . PHP_EOL;
}
foreach ($topFiveIdentifiers as $safeIdentifierName => $count) {
$quotedPrefix = preg_quote($SAFE_PREFIX_TEXT_TOKEN);
$identifierName = preg_replace("/^{$quotedPrefix}/", '', $safeIdentifierName);
echo "{$identifierName}: {$count}x" . PHP_EOL;
}
//----utility----------------
function getResultWord($bool) {
if($bool === true) {
return 'yes';
}
return 'no';
}