-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCodeStyle.php
More file actions
374 lines (333 loc) · 11.3 KB
/
Copy pathCodeStyle.php
File metadata and controls
374 lines (333 loc) · 11.3 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
namespace CleantalkSP\Common\Scanner\HeuristicAnalyser\Modules;
use CleantalkSP\Common\Scanner\HeuristicAnalyser\DataStructures\Token;
class CodeStyle
{
/**
* @var Tokens
*/
private $tokens;
/**
* @var int shows how many symbols could contain normal code line
*/
const CRITICAL_CODE_STRING_LENGTH = 1000;
/**
* @const How many different upper/lowercase chars should be provided to being get in weight count.
*/
const RANDOM_CHAR_CASE_VOLATILITY_LIMIT = 3;
/**
* @const Minimum word length to check for random structures
*/
const RANDOM_MIN_WORD_LEN = 5;
/**
* @const Maximum word length to check for random structures
*/
const RANDOM_MAX_WORD_LEN = 5;
/**
* @const Sensitivity for random total weight
*/
const RANDOM_TOTAL_WEIGHT_THRESHOLD = 1.00;
/**
* @const Sensitivity for special chars proportion
*/
const SPECIAL_CHARS_PROPORTION_THRESHOLD = 0.33;
/**
* Holds numbers of critical long lines
*
* @var int[]
*/
private $critical_long_line_nums = array();
/**
* Holds numbers of comments noise lines
*
* @var int[]
*/
private $comment_noise_line_nums = array();
/**
* Check if file contains unreadable code
*/
private $is_unreadable = false;
/**
* Check if weight of noise > 5
*
* @var bool
*/
public $comments_noise = false;
/**
* Weight of noise
*
* @var int
*/
private $noise_lines = 0;
/**
* Noise threshold
*
* @var int
*/
private $noise_lines_threshold = 5;
/**
* Noise threshold
*
* @var int
*/
private $noise_matches_threshold = 3;
/**
* Array for multi-line comments in one line
*
* @var array
*/
private $matches = array();
/**
* Line numbers with tokens which should be on a different lines
*
* @var array
*/
private $greedy_token_lines = array();
/**
* Number of symbols with code|html|comments
*
* @var int
*/
private $length_of_tokens__code = 0;
private $length_of_tokens__html = 0;
private $length_of_tokens__comments = 0;
/**
* Line numbers with tokens contains code|html|comments
*
* @var array
*/
private $number_of_lines__code = array();
private $number_of_lines__html = array();
private $number_of_lines__comments = array();
public function __construct(Tokens $tokens)
{
$this->tokens = $tokens;
}
/**
* @param $content
* @return void
* @psalm-suppress UnusedMethod
*/
public function analyseLineLengths(&$content)
{
$lines = preg_split("/((\r?\n)|(\r\n?))/", $content);
for ( $line_num = 1; isset($lines[$line_num - 1]); $line_num++ ) {
try {
$line = $lines[$line_num - 1];
if ($this->analyseLineLengthsIsExceptions($line)) {
continue;
}
if ( strlen($line) > self::CRITICAL_CODE_STRING_LENGTH ) {
$this->critical_long_line_nums[] = $line_num;
}
} catch (\Exception $_e) {
continue;
}
}
}
/**
* Check exceptions for long line
*
* @param string $line
* @return bool
*/
public function analyseLineLengthsIsExceptions($line)
{
if (preg_match('#^\s*<path\s+d="[^$][.\w\s-]+"\s*\/>#', $line, $match)) {
return true;
}
return false;
}
/**
* Check if the code is human-unreadable.
* @param string $content File content.
* @return void
*/
public function analyseHumanUnreadableCode($content)
{
$proportion_spec_symbols = $this->proportionOfSpecialSymbols();
$weight = $this->getWeightOfRandomCharStructures($content);
if (
$proportion_spec_symbols >= self::SPECIAL_CHARS_PROPORTION_THRESHOLD
||
$weight > self::RANDOM_TOTAL_WEIGHT_THRESHOLD
) {
$this->is_unreadable = true;
}
}
public function searchIncompatibleOnelinedTokens()
{
if ( $this->tokens->current->isTypeOf('one_line') ) {
$this->greedy_token_lines[] = $this->tokens->current->line;
}
}
public function sortTokensWithDifferentTypes()
{
$current_token_length = $this->tokens->current->length;
$current_token_line = $this->tokens->current->line;
if ( $this->tokens->current->isTypeOf('html') ) {
$this->tokens->html[] = $this->tokens->current;
$this->length_of_tokens__html += $current_token_length;
$this->number_of_lines__html[] = $current_token_line;
} elseif ( $this->tokens->current->isTypeOf('comments') ) {
$this->tokens->comments[] = $this->tokens->current;
$this->length_of_tokens__comments += $current_token_length;
$this->number_of_lines__comments[] = $current_token_line;
} else {
$this->length_of_tokens__code += $current_token_length;
$this->number_of_lines__code[] = $current_token_line;
}
}
public function detectBadLines()
{
$line_nums = array_unique($this->critical_long_line_nums);
$values = array_fill(0, count($line_nums), 'long line');
$result = array_combine($line_nums, $values);
//todo Merging on int indexes will rewrite current line weak_spot
if ($this->is_unreadable) {
$result = array_merge($result, [1 => 'unreadable']);
}
//todo This will replace other weak_spots on the line
if ($this->comments_noise) {
$first_comments_noise_line = (int)array_shift($this->comment_noise_line_nums);
$result[$first_comments_noise_line] = 'comments noise';
}
return $result;
}
/**
* Count special service chars like <>!= etc. and return the proportion to the total chars count.
* Uses $this->tokens as content source.
* @return float 0.0 -> 1.0
*/
private function proportionOfSpecialSymbols()
{
$glued_content = '';
foreach ($this->tokens->tokens as $token) {
$token_value = '';
//convert token to array if object provided
if ($token instanceof Token) {
$token = $token->toArray();
}
if (is_array($token)) {
//if the token is comment, skip it
if (in_array($token[0], [T_COMMENT, T_DOC_COMMENT])) {
continue;
}
$token_value = $token[1];
}
// glue all
$glued_content .= $token_value;
}
if ( !empty($glued_content) ) {
/**
* This regexp match all symbols except letters, digits and whitespaces in a core. However,
* we would exclude some chars that usually not used in the code, but can be used in a human text.
* To check the list of all symbols that will be matched by this regexp, you can use this code:
*
* $arr = array_count_values($symbols[0]);
* asort($arr);
* var_dump($arr);
*
*/
preg_match_all('#[^\pL\s\d\'\"()*\-+;&_@?!.,:%`]#', $glued_content, $symbols);
/**
* Notice:
* Extended regexp to exclude more service chars, use or upgrade the current if there will be false positives:
* preg_match_all('#[^\pL\s\d\'\"()\[\]*{}\-_\\\/@<>?!=.,:%`]#', $glued_content, $symbols);
*/
if (isset($symbols[0]) && count($symbols[0]) > 0) {
return count($symbols[0]) / (strlen($glued_content));
}
}
return 0.0;
}
/**
* Break the content to a several `words` and run a couple of checks
* to calculate weight of random-char structures in this.
* Increments if:
* <ul>
* <li> "+" char provided</li>
* <li> char case volatility more than limit</li>
* <li> if the word contains a lexeme like `x8k`, `p9R` etc.</li>
* </ul>
* @param string $content File content.
* @return float Normal value is <= 1.00
*/
private function getWeightOfRandomCharStructures($content)
{
$weight = 0.0;
/**
* Find the words to check. Global regex.
*/
preg_match_all('#[a-zA-Z\d_\-+\\\/]+#', $content, $words);
$words = isset($words[0]) ? $words[0] : [];
$words = array_filter($words, function ($word) {
return strlen($word) > self::RANDOM_MIN_WORD_LEN && strlen($word) < self::RANDOM_MAX_WORD_LEN;
});
$words = array_values($words);
/**
* Start check for each word.
*/
$words_weight_data = [];
foreach ($words as $word) {
//init word random weight, could be between 0 -> 3
$words_weight_data[$word] = 0;
/**
* Check + char provided
*/
//todo What is this for?
if (strpos($word, '+') !== false) {
$words_weight_data[$word] += 1;
}
/**
* Check char case volatility
*/
$lower_word = strtolower($word);
//skip case checking if no case difference
$skip_caps_checking = $lower_word === $word || strtoupper($word) === $word;
if ( ! $skip_caps_checking ) {
//get count of chars that has case difference
$count_of_case_volatile_chars = strlen($lower_word) - similar_text($lower_word, $word);
//inc weight if volatile chars count more than the limit
if ($count_of_case_volatile_chars > self::RANDOM_CHAR_CASE_VOLATILITY_LIMIT) {
$words_weight_data[$word] += 1;
}
}
/**
* Check if the word contains a lexeme like `x8k`, `p9R` etc
*/
if (preg_match('#[^\d]\d+[\w]#', $word)) {
$words_weight_data[$word] += 1;
}
}
/**
* Calculate result.
* Result is a total sum of words weights divided by total count of words found in the content.
* Normal value is <= 1.
*/
if ( !empty($words_weight_data) ) {
$weight = array_sum(array_values($words_weight_data)) / count($words_weight_data);
}
return $weight;
}
public function analyseWeightOfNoise($content)
{
$lines = preg_split("/((\r?\n)|(\r\n?))/", $content);
// few multiline comments in one string
for ( $line_num = 1; isset($lines[$line_num - 1]); $line_num++ ) {
try {
$line = $lines[$line_num - 1];
preg_match_all('#\/\*\s*\w{1,5}\s*\*\/#', $line, $this->matches);
if (count($this->matches[0]) > $this->noise_matches_threshold) {
$this->noise_lines++;
}
if ($this->noise_lines > $this->noise_lines_threshold) {
$this->comment_noise_line_nums[] = $line_num;
$this->comments_noise = true;
}
} catch (\Exception $_e) {
continue;
}
}
}
}