Total Complexity | 50 |
Total Lines | 591 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like AbuseIPDBClient often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbuseIPDBClient, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class AbuseIPDBClient extends AbstractClient |
||
32 | { |
||
33 | /** |
||
34 | * Helper methods |
||
35 | */ |
||
36 | use CheckTrait, CheckBlockTrait, BulkReportTrait; |
||
3 ignored issues
–
show
|
|||
37 | |||
38 | /** |
||
39 | * The entry point of our app |
||
40 | * |
||
41 | * @access public |
||
42 | * @static |
||
43 | * @param array $arguments |
||
44 | * @param string $keyPath The key file path |
||
45 | * |
||
46 | * @return void |
||
47 | */ |
||
48 | public static function start(array $arguments, string $keyPath) |
||
49 | { |
||
50 | // required at least one valid argument |
||
51 | self::$keyPath = $keyPath; |
||
52 | self::validate( !empty($arguments), 'No valid arguments given. Run abuseipdb --help to get help.'); |
||
53 | if (!self::parseCommand($arguments, $keyPath)) { |
||
54 | self::error('Invalid arguments. Run abuseipdb --help to get help.'); |
||
55 | self::printFooter(); |
||
56 | Program::exit(1); |
||
57 | } |
||
58 | Program::exit(0); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Register API key in a config file |
||
63 | * |
||
64 | * @access protected |
||
65 | * @static |
||
66 | * |
||
67 | * @return bool |
||
68 | */ |
||
69 | protected static function registerApiKey($arguments) |
||
70 | { |
||
71 | self::printTitle(Console::text(' ► Register API key ', 'darkgray')); |
||
72 | |||
73 | $key = self::getArgumentValue($arguments,'S', 'save-key'); |
||
74 | |||
75 | if (empty($key)){ |
||
76 | self::error('Null or invalid key argument.'); |
||
77 | self::printFooter(); |
||
78 | Program::exit(1); |
||
79 | } |
||
80 | |||
81 | $data = json_encode(['api_key' => $key]); |
||
82 | |||
83 | if (file_put_contents(self::$keyPath, $data, LOCK_EX) === false){ |
||
84 | self::error('An error occured during writing config file. Make sure to give the appropriate permissions do the config directory.'); |
||
85 | self::printFooter(); |
||
86 | Program::exit(1); |
||
87 | } |
||
88 | Console::log(Console::text(' ✓ ', 'green') . Console::text('Your config key file has been successfully created.', 'white')); |
||
89 | Console::log(); |
||
90 | self::printFooter(); |
||
91 | Program::exit(); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Prints the help |
||
96 | * |
||
97 | * @access protected |
||
98 | * @static |
||
99 | * |
||
100 | * @return void |
||
101 | */ |
||
102 | protected static function printHelp() |
||
103 | { |
||
104 | self::printBanner(); |
||
105 | |||
106 | Console::log(' ' . Console::text('SYNOPSIS:', 'white', 'underline')); |
||
107 | Console::log(' ' . Console::text(' abuseipdb -C ') . |
||
108 | Console::text('IP', 'yellow') . |
||
109 | Console::text(' [-d ') . |
||
110 | Console::text('DAYS', 'yellow') . |
||
111 | Console::text('] [-v] [-l ') . |
||
112 | Console::text('LIMIT', 'yellow') . |
||
113 | Console::text(']')); |
||
114 | |||
115 | Console::log(' ' . Console::text(' abuseipdb -K ') . |
||
116 | Console::text('NETWORK', 'yellow') . |
||
117 | Console::text(' [-d ') . |
||
118 | Console::text('DAYS', 'yellow') . |
||
119 | Console::text(']')); |
||
120 | |||
121 | Console::log(' ' . Console::text(' abuseipdb -R ' . |
||
122 | Console::text('IP', 'yellow') . ' -c ' . |
||
123 | Console::text('CATEGORIES', 'yellow') . ' -m ' . |
||
124 | Console::text('MESSAGE', 'yellow'))); |
||
125 | |||
126 | Console::log(' ' . Console::text(' abuseipdb -V ' . |
||
127 | Console::text('FILE', 'yellow'))); |
||
128 | |||
129 | Console::log(' ' . Console::text(' abuseipdb -E ' . |
||
130 | Console::text('IP', 'yellow'))); |
||
131 | |||
132 | Console::log(' ' . Console::text(' abuseipdb -B ') . |
||
133 | Console::text('[-l ') . |
||
134 | Console::text('LIMIT', 'yellow') . |
||
135 | Console::text('] [-s ') . |
||
136 | Console::text('SCORE', 'yellow') . |
||
137 | Console::text('] [-p ') . |
||
138 | Console::text('', 'yellow') . |
||
139 | Console::text(']')); |
||
140 | |||
141 | Console::log(' ' . Console::text(' abuseipdb -L | -G | -h | --version')); |
||
142 | |||
143 | Console::log(); |
||
144 | Console::log(' ' . Console::text('OPTIONS:', 'white', 'underline')); |
||
145 | Console::log(); |
||
146 | Console::log(Console::text(' -h, --help', 'white')); |
||
147 | Console::log(' Prints the current help. If given, all next arguments are ignored.', 'lightgray'); |
||
148 | Console::log(); |
||
149 | Console::log(Console::text(' -G, --config', 'white')); |
||
150 | Console::log(' Prints the current config. If given, all next arguments are ignored.', 'lightgray'); |
||
151 | Console::log(); |
||
152 | Console::log(Console::text(' -L, --list', 'white')); |
||
153 | Console::log(' Prints the list report categories. If given, all next arguments are ignored.', 'lightgray'); |
||
154 | Console::log(); |
||
155 | Console::log(Console::text(' -C, --check ', 'white') . Console::text('ip', 'yellow', 'underline')); |
||
156 | Console::log(' Performs a check request for the given IP address. A valid IPv4 or IPv6 address is required.', 'lightgray'); |
||
157 | Console::log(); |
||
158 | Console::log(Console::text(' -K, --check-block ', 'white') . Console::text('network', 'yellow', 'underline')); |
||
159 | Console::log(' Performs a check-block request for the given network. A valid subnet (v4 or v6) denoted with ', 'lightgray'); |
||
160 | Console::log(' CIDR notation is required.', 'lightgray'); |
||
161 | Console::log(); |
||
162 | Console::log(Console::text(' -d, --days ', 'white') . Console::text('days', 'yellow', 'underline')); |
||
163 | Console::log(' For a check or check-block request, defines the maxAgeDays. Min is 1, max is 365, default is 30.', 'lightgray'); |
||
164 | Console::log(); |
||
165 | Console::log(Console::text(' -R, --report ', 'white') . Console::text('ip', 'yellow', 'underline')); |
||
166 | Console::log(' Performs a report request for the given IP address. A valid IPv4 or IPv6 address is required.', 'lightgray'); |
||
167 | Console::log(); |
||
168 | Console::log(Console::text(' -V, --bulk-report ', 'white') . Console::text('path', 'yellow', 'underline')); |
||
169 | Console::log(' Performs a bulk-report request sending a csv file. A valid file name or full path is required.', 'lightgray'); |
||
170 | Console::log(); |
||
171 | Console::log(Console::text(' -E, --clear ', 'white')); |
||
172 | Console::log(' Remove own reports for the given IP address. A valid IPv4 or IPv6 address is required.', 'lightgray'); |
||
173 | Console::log(); |
||
174 | Console::log(Console::text(' -c, --categories ', 'white') . Console::text('categories', 'yellow', 'underline')); |
||
175 | Console::log(' For a report request, defines the report category(ies). Categories must be separate by a comma.', 'lightgray'); |
||
176 | Console::log(' Some categories cannot be used alone. A category can be represented by its shortname or by its', 'lightgray'); |
||
177 | Console::log(Console::text(' id. Use ','lightgray') . Console::text('abuseipdb -L', 'white') . Console::text(' to print the categories list.','lightgray')); |
||
178 | Console::log(); |
||
179 | Console::log(Console::text(' -m, --message ', 'white') . Console::text('message', 'yellow', 'underline')); |
||
180 | Console::log(' For a report request, defines the message to send with report. Message is required for all', 'lightgray'); |
||
181 | Console::log(' report requests.', 'lightgray'); |
||
182 | Console::log(); |
||
183 | Console::log(Console::text(' -B, --blacklist ', 'white')); |
||
184 | Console::log(' Performs a blacklist request. Default limit is 1000. This limit can ne changed with the', 'lightgray'); |
||
185 | Console::log(' ' . Console::text('--limit', 'white') . Console::text(' parameter. ', 'lightgray')); |
||
186 | Console::log(); |
||
187 | Console::log(Console::text(' -l, --limit ', 'white') . Console::text('limit', 'yellow', 'underline')); |
||
188 | Console::log(' For a blacklist request, defines the limit.', 'lightgray'); |
||
189 | Console::log(' For a check request with verbose flag, sets the max number of last reports displayed. Default is 10', 'lightgray'); |
||
190 | Console::log(' For a check-block request, sets the max number of IPs displayed. Default is 0 (no limit).', 'lightgray'); |
||
191 | Console::log(); |
||
192 | Console::log(Console::text(' -p, --plaintext ', 'white')); |
||
193 | Console::log(' For a blacklist request, output only ip list as plain text.', 'lightgray'); |
||
194 | Console::log(); |
||
195 | Console::log(Console::text(' -s, --score ', 'white')); |
||
196 | Console::log(' For a blacklist request, sets the confidence score minimum. The confidence minimum ', 'lightgray'); |
||
197 | Console::log(' must be between 25 and 100. This parameter is subscriber feature (not honored otherwise, allways 100).', 'lightgray'); |
||
198 | Console::log(); |
||
199 | Console::log(Console::text(' -v, --verbose ', 'white')); |
||
200 | Console::log(' For a check request, display additional fields like the x last reports. This increases ', 'lightgray'); |
||
201 | Console::log(Console::text(' request time and response size. Max number of last reports displayed can be changed with the ', 'lightgray')); |
||
202 | Console::log(' ' . Console::text('--limit', 'white') . Console::text(' parameter. ', 'lightgray')); |
||
203 | Console::log(); |
||
204 | Console::log(Console::text(' --version', 'white')); |
||
205 | Console::log(' Prints the current version. If given, all next arguments are ignored.', 'lightgray'); |
||
206 | Console::log(); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Prints the current config and exit |
||
211 | * |
||
212 | * @access protected |
||
213 | * @static |
||
214 | * |
||
215 | * @return void |
||
216 | */ |
||
217 | protected static function printConfig() |
||
218 | { |
||
219 | $conf = self::$api->getConfig(); |
||
220 | |||
221 | self::printTitle(Console::text(' ► Current configuration ', 'darkgray')); |
||
222 | |||
223 | Console::log(Console::text(' api_key:[', 'white') . Console::text($conf['apiKey'], 'green') . Console::text(']', 'white')); |
||
224 | Console::log(Console::text(' self_ips:', 'white')); |
||
225 | |||
226 | foreach ($conf['selfIps'] as $ip) { |
||
227 | Console::log(Console::text(' [', 'white') . Console::text($ip, 'green') . Console::text(']', 'white')); |
||
228 | } |
||
229 | |||
230 | Console::log(); |
||
231 | self::printFooter(); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Prints the report categories list |
||
236 | * |
||
237 | * @access protected |
||
238 | * @static |
||
239 | * |
||
240 | * @return void |
||
241 | */ |
||
242 | protected static function printCategories() |
||
243 | { |
||
244 | self::printTitle(Console::text(' ► Report categories list ', 'darkgray')); |
||
245 | |||
246 | $categories = ApiHandler::getCategories(); |
||
247 | $rowHeaders = [ |
||
248 | Console::text('ShortName', 'darkgray') => 15, |
||
249 | Console::text('Id', 'darkgray') => 2, |
||
250 | Console::text('Full name', 'darkgray') => 18, |
||
251 | Console::text('Can be alone?', 'darkgray') => 15 |
||
252 | ]; |
||
253 | Console::$verticalSeparator = ' '; |
||
254 | Console::$verticalInnerSeparator = ' '; |
||
255 | Console::log(Console::tableRowSeparator($rowHeaders, 'darkgray')); |
||
256 | Console::log(Console::tableRow($rowHeaders)); |
||
257 | Console::log(Console::tableRowSeparator($rowHeaders), 'darkgray'); |
||
258 | |||
259 | foreach ($categories as $cat) { |
||
260 | $id = Console::text($cat[1], 'white'); |
||
261 | $standalone = $cat[3] ? Console::text('✓', 'green') . Console::text(' true ', 'lightgray') : |
||
262 | Console::text('✗', 'red') . Console::text(' false', 'darkgray'); |
||
263 | $shortName = Console::text($cat[0], 'white'); |
||
264 | $fullName = Console::text($cat[2], 'lightgray'); |
||
265 | |||
266 | Console::log( |
||
267 | Console::TableRowStart(). |
||
268 | Console::TableRowCell( $shortName , 15). |
||
269 | Console::TableRowCell( $id , 2, Console::ALIGN_CENTER). |
||
270 | Console::TableRowCell( $fullName , 18). |
||
271 | Console::TableRowCell( $standalone , 15, Console::ALIGN_CENTER) |
||
272 | ); |
||
273 | } |
||
274 | //Console::log(Console::tableRowSeparator($rowHeaders), 'darkgray'); |
||
275 | Console::log(); |
||
276 | self::printFooter(); |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Perform a report request |
||
281 | * |
||
282 | * @access protected |
||
283 | * @static |
||
284 | * @param array $arguments |
||
285 | * |
||
286 | * @return void |
||
287 | */ |
||
288 | protected static function reportIP(array $arguments) |
||
289 | { |
||
290 | $ip = self::getArgumentValue($arguments,'R', 'report'); |
||
291 | $cats = self::getArgumentValue($arguments,'c', 'categories'); |
||
292 | $message = self::getArgumentValue($arguments,'m', 'message'); |
||
293 | |||
294 | self::printTitle(Console::text(' ► Report IP: ', 'darkgray') . Console::text(escapeshellcmd($ip), 'white')); |
||
295 | self::printTempMessage(); |
||
296 | |||
297 | // Peforms request |
||
298 | $timeStart = microtime(true); |
||
299 | $report = self::$api->report($ip, $cats, $message)->getObject(); |
||
300 | $timeEnd = microtime(true); |
||
301 | $time = $timeEnd - $timeStart; // request time |
||
302 | self::clearTempMessage(); |
||
303 | |||
304 | // check for errors / empty response |
||
305 | if (self::printErrors($report)){ |
||
306 | self::printFooter(); |
||
307 | Program::exit(1); |
||
308 | } |
||
309 | |||
310 | // ✓ Done: print reported IP and confidence score |
||
311 | $score = empty($report->data->abuseConfidenceScore) ? 0 : $report->data->abuseConfidenceScore; |
||
312 | $scoreColor = self::getScoreColor($score); |
||
313 | |||
314 | switch (self::$outputFormat){ |
||
315 | case self::OUTPUT_JSON: |
||
316 | echo json_encode($report, JSON_PRETTY_PRINT); |
||
317 | break; |
||
318 | |||
319 | case self::OUTPUT_DEFAULT: |
||
320 | Console::log( |
||
321 | Console::text(' ✓', 'green') . Console::text(' IP: [', 'white') . |
||
322 | Console::text($ip, $scoreColor) . Console::text('] successfully reported', 'white') |
||
323 | ); |
||
324 | Console::log(Console::text(' Confidence score: ', 'white') . self::getScoreBadge($score)); |
||
325 | Console::log(); |
||
326 | self::printFooter($time); |
||
327 | break; |
||
328 | |||
329 | case self::OUTPUT_PLAINTEXT: |
||
330 | echo $score . PHP_EOL; |
||
331 | break; |
||
332 | |||
333 | } |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Perform a bulk-report request |
||
338 | * |
||
339 | * @access protected |
||
340 | * @static |
||
341 | * @param array $arguments |
||
342 | * |
||
343 | * @return void |
||
344 | */ |
||
345 | protected static function bulkReport(array $arguments) |
||
346 | { |
||
347 | $fileName = self::getArgumentValue($arguments,'V', 'bulk-report'); |
||
348 | |||
349 | self::printTitle(Console::text(' ► Bulk report for file: ', 'darkgray') . Console::text(escapeshellcmd($fileName), 'white')); |
||
350 | self::printTempMessage(); |
||
351 | |||
352 | // Peforms request |
||
353 | $timeStart = microtime(true); |
||
354 | $response = self::$api->bulkReport($fileName)->getObject(); |
||
355 | $timeEnd = microtime(true); |
||
356 | $time = $timeEnd - $timeStart; // request time |
||
357 | self::clearTempMessage(); |
||
358 | |||
359 | // check for errors / empty response |
||
360 | if (self::printErrors($response)){ |
||
361 | self::printFooter(); |
||
362 | Program::exit(1); |
||
363 | } |
||
364 | |||
365 | // ✓ Done |
||
366 | switch (self::$outputFormat){ |
||
367 | case self::OUTPUT_JSON: |
||
368 | echo json_encode($response, JSON_PRETTY_PRINT); |
||
369 | break; |
||
370 | |||
371 | case self::OUTPUT_DEFAULT: |
||
372 | self::printBulkReportDetail($response, $fileName); |
||
373 | Console::log(); |
||
374 | self::printFooter($time); |
||
375 | break; |
||
376 | |||
377 | case self::OUTPUT_PLAINTEXT: |
||
378 | $nbSavedReports = isset($response->data->savedReports) ? $response->data->savedReports : 0; |
||
379 | echo $nbSavedReports . PHP_EOL; |
||
380 | break; |
||
381 | |||
382 | } |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * Perform a clear-address request |
||
387 | * |
||
388 | * @access protected |
||
389 | * @static |
||
390 | * @param array $arguments |
||
391 | * |
||
392 | * @return void |
||
393 | */ |
||
394 | protected static function clearIP(array $arguments) |
||
395 | { |
||
396 | $ip = self::getArgumentValue($arguments,'E', 'clear'); |
||
397 | self::printTitle(Console::text(' ► Clear reports for IP: ', 'darkgray') . Console::text(escapeshellcmd($ip), 'white')); |
||
398 | |||
399 | // Peforms request |
||
400 | self::printTempMessage(); |
||
401 | $timeStart = microtime(true); |
||
402 | $response = self::$api->clearAddress($ip)->getObject(); |
||
403 | $timeEnd = microtime(true); |
||
404 | $time = $timeEnd - $timeStart; // request time |
||
405 | self::clearTempMessage(); |
||
406 | |||
407 | // check for errors / empty response |
||
408 | if (self::printErrors($response)){ |
||
409 | self::printFooter($time); |
||
410 | Program::exit(1); |
||
411 | } |
||
412 | |||
413 | // ✓ Done: print deleted report number |
||
414 | switch (self::$outputFormat){ |
||
415 | case self::OUTPUT_JSON: |
||
416 | echo json_encode($response, JSON_PRETTY_PRINT); |
||
417 | break; |
||
418 | |||
419 | case self::OUTPUT_DEFAULT: |
||
420 | Console::log( |
||
421 | Console::text(' ✓', 'green') . |
||
422 | Console::text(' Successfull clear request for IP: [', 'white') . |
||
423 | Console::text($ip, 'lightyellow') . |
||
424 | Console::text(']', 'white') |
||
425 | ); |
||
426 | self::printResult(' Deleted reports: ', $response->data->numReportsDeleted ?? 0, 'lightyellow'); |
||
427 | Console::log(); |
||
428 | self::printFooter($time); |
||
429 | break; |
||
430 | |||
431 | case self::OUTPUT_PLAINTEXT: |
||
432 | echo ($response->data->numReportsDeleted ?? 0) . PHP_EOL; |
||
433 | break; |
||
434 | |||
435 | } |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * Perform a blacklist request |
||
440 | * |
||
441 | * @access protected |
||
442 | * @static |
||
443 | * @param array $arguments |
||
444 | * |
||
445 | * @return void |
||
446 | */ |
||
447 | protected static function getBlacklist(array $arguments) |
||
448 | { |
||
449 | self::printTitle(Console::text(' ► Get Blacklist ', 'darkgray')); |
||
450 | |||
451 | $plainText = self::$outputFormat === self::OUTPUT_PLAINTEXT; |
||
452 | $limit = self::getNumericParameter($arguments,'l', 'limit', 1000); |
||
453 | $scoreMin = self::getNumericParameter($arguments,'s', 'score', 100); |
||
454 | |||
455 | self::printTempMessage(); |
||
456 | |||
457 | // do request |
||
458 | $timeStart = microtime(true); |
||
459 | $response = self::$api->blacklist($limit, $plainText, $scoreMin); |
||
460 | $timeEnd = microtime(true); |
||
461 | $time = $timeEnd - $timeStart; // request time |
||
462 | |||
463 | self::clearTempMessage(); |
||
464 | |||
465 | // response could be json on error, while plaintext flag is set |
||
466 | $decodedResponse = $response->getObject(); |
||
467 | if (self::printErrors($decodedResponse, false)){ |
||
468 | self::printFooter($time); |
||
469 | Program::exit(1); |
||
470 | } |
||
471 | |||
472 | // ✓ Done: print deleted report number |
||
473 | switch (self::$outputFormat){ |
||
474 | case self::OUTPUT_JSON: |
||
475 | echo json_encode($response, JSON_PRETTY_PRINT); |
||
476 | break; |
||
477 | |||
478 | case self::OUTPUT_DEFAULT: |
||
479 | // print list |
||
480 | self::printResult(' List generated at: ', self::getDate($decodedResponse->meta->generatedAt), 'lightyellow', ''); |
||
481 | Console::log(); |
||
482 | foreach ($decodedResponse->data as $report){ |
||
483 | $score = empty($report->abuseConfidenceScore) ? 0 : $report->abuseConfidenceScore; |
||
484 | $defaultColor = self::getScoreColor($score); |
||
485 | |||
486 | $line = Console::text(' →', $defaultColor); |
||
487 | $line .= self::printResult(' IP: ', $report->ipAddress, $defaultColor, '', false); |
||
488 | $line .= self::printResult(' | Last reported at: ', self::getDate($report->lastReportedAt), $defaultColor, '', false); |
||
489 | $line .= Console::text(' | Confidence score: ', 'white'); |
||
490 | $line .= self::getScoreBadge($score); |
||
491 | Console::log($line); |
||
492 | } |
||
493 | |||
494 | Console::log(); |
||
495 | self::printFooter($time); |
||
496 | break; |
||
497 | |||
498 | case self::OUTPUT_PLAINTEXT: |
||
499 | // echo response "as is" |
||
500 | Console::log($response->getPlaintext()); |
||
501 | break; |
||
502 | |||
503 | } |
||
504 | } |
||
505 | |||
506 | /** |
||
507 | * Perform a check-block request |
||
508 | * |
||
509 | * @access protected |
||
510 | * @static |
||
511 | * @param array $arguments |
||
512 | * |
||
513 | * @return void |
||
514 | */ |
||
515 | protected static function checkBlock(array $arguments) |
||
565 | } |
||
566 | } |
||
567 | |||
568 | /** |
||
569 | * Perform a check request |
||
570 | * |
||
571 | * @access protected |
||
572 | * @static |
||
573 | * @param array $arguments |
||
574 | * |
||
575 | * @return void |
||
576 | */ |
||
577 | protected static function checkIP(array $arguments) |
||
622 | |||
623 | } |
||
624 | } |
||
625 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths