Issues (9)

src/AbuseIPDBClient.php (9 issues)

1
<?php declare(strict_types=1); 
2
3
/**
4
 *       _                 ___ ___ ___  ___
5
 *  __ _| |__ _  _ ___ ___|_ _| _ \   \| _ )
6
 * / _` | '_ \ || (_-</ -_)| ||  _/ |) | _ \
7
 * \__,_|_.__/\_,_/__/\___|___|_| |___/|___/
8
 * 
9
 * This file is part of Kristuff\AbuseIPDB.
10
 *
11
 * (c) Kristuff <[email protected]>
12
 *
13
 * For the full copyright and license information, please view the LICENSE
14
 * file that was distributed with this source code.
15
 *
16
 * @version    0.9.20
17
 * @copyright  2020-2022 Kristuff
18
 */
19
namespace Kristuff\AbuseIPDB;
20
21
use Kristuff\Mishell\Console;
22
use Kristuff\Mishell\Program;
23
24
/**
25
 * Class AbuseIPDB
26
 * 
27
 * The main cli program
28
 */
29
class AbuseIPDBClient extends AbstractClient
30
{
31
    /**
32
     * Helper methods
33
     */
34
    use CheckTrait, CheckBlockTrait, BulkReportTrait;
35
36
    /**
37
     * The entry point of our app 
38
     * 
39
     * @access public
40
     * @static
41
     * @param array     $arguments
42
     * @param string    $keyPath        The configuration path
43
     * 
44
     * @return void
45
     */
46
    public static function start(array $arguments, string $confPath): void
47
    {
48
        // required at least one valid argument
49
        self::validate( !empty($arguments), 'No valid arguments given. Run abuseipdb --help to get help.');
50
        self::$configPath = $confPath; 
51
        if (!self::parseCommand($arguments, $confPath)) {
0 ignored issues
show
The call to Kristuff\AbuseIPDB\AbstractClient::parseCommand() has too many arguments starting with $confPath. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        if (!self::/** @scrutinizer ignore-call */ parseCommand($arguments, $confPath)) {

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
52
            self::error('Invalid arguments. Run abuseipdb --help to get help.');
53
            self::printFooter();
54
            Program::exit(1);
55
        }
56
        Program::exit(0);
57
    }
58
 
59
    /**
60
     * Prints the help
61
     * 
62
     * @access protected
63
     * @static
64
     * 
65
     * @return void
66
     */
67
    protected static function printHelp(): void
68
    {
69
        Console::log();
70
        Console::log(' ' . Console::text('SYNOPSIS:', 'white', 'underline')); 
71
        Console::log(' ' . Console::text('    abuseipdb -C ') . 
72
                           Console::text('IP', 'yellow') . 
73
                           Console::text(' [-d ') . 
74
                           Console::text('DAYS', 'yellow') . 
75
                           Console::text('] [-v] [-l ') . 
76
                           Console::text('LIMIT', 'yellow') . 
77
                           Console::text('] [-o ') . 
78
                           Console::text('FORMAT', 'yellow') . 
79
                           Console::text(']')); 
80
81
        Console::log(' ' . Console::text('    abuseipdb -K ') . 
82
                           Console::text('NETWORK', 'yellow') . 
83
                           Console::text(' [-d ') . 
84
                           Console::text('DAYS', 'yellow') . 
85
                           Console::text('] [-o ') . 
86
                           Console::text('FORMAT', 'yellow') . 
87
                           Console::text(']')); 
88
89
        Console::log(' ' . Console::text('    abuseipdb -R ') .
90
                           Console::text('IP', 'yellow') . ' -c ' .
91
                           Console::text('CATEGORIES', 'yellow') . ' -m ' .
92
                           Console::text('MESSAGE', 'yellow') .
93
                           Console::text(' [-o ') . 
94
                           Console::text('FORMAT', 'yellow') . 
95
                           Console::text(']')); 
96
97
        Console::log(' ' . Console::text('    abuseipdb -V ') .
98
                           Console::text('FILE', 'yellow') .
99
                           Console::text(' [-o ') . 
100
                           Console::text('FORMAT', 'yellow') . 
101
                           Console::text(']')); 
102
103
        Console::log(' ' . Console::text('    abuseipdb -E ') .
104
                           Console::text('IP', 'yellow').
105
                           Console::text(' [-o ') . 
106
                           Console::text('FORMAT', 'yellow') . 
107
                           Console::text(']')); 
108
                           
109
        Console::log(' ' . Console::text('    abuseipdb -B ') . 
110
                           Console::text('[-l ') . 
111
                           Console::text('LIMIT', 'yellow') . 
112
                           Console::text('] [-s ') . 
113
                           Console::text('SCORE', 'yellow') . 
114
                           Console::text('] [-o ') . 
115
                           Console::text('FORMAT', 'yellow') . 
116
                           Console::text(']')); 
117
118
        Console::log(' ' . Console::text('    abuseipdb -L | -G | -h | --version'));
119
                           
120
        Console::log();    
121
        Console::log(' ' . Console::text('OPTIONS:', 'white', 'underline')); 
122
        Console::log();
123
        Console::log(Console::text('   -h, --help', 'white')); 
124
        Console::log('       Prints the current help.', 'lightgray');
125
        Console::log();    
126
        Console::log(Console::text('   -G, --config', 'white')); 
127
        Console::log('       Prints the current config.', 'lightgray');
128
        Console::log();    
129
        Console::log(Console::text('   -L, --list', 'white')); 
130
        Console::log('       Prints the list report categories.', 'lightgray');
131
        Console::log();    
132
        Console::log(Console::text('   -C, --check ', 'white') . Console::text('IP', 'yellow', 'underline')); 
133
        Console::log('       Performs a check request for the given IP address. A valid IPv4 or IPv6 address is required.', 'lightgray');
134
        Console::log();    
135
        Console::log(Console::text('   -K, --check-block ', 'white') . Console::text('NETWORK', 'yellow', 'underline')); 
136
        Console::log('       Performs a check-block request for the given network. A valid subnet (v4 or v6) denoted with ', 'lightgray');
137
        Console::log('       CIDR notation is required.', 'lightgray');
138
        Console::log();    
139
        Console::log(Console::text('   -d, --days ', 'white') . Console::text('DAYS', 'yellow', 'underline')); 
140
        Console::log('       For a check or check-block request, defines the maxAgeDays. Min is 1, max is 365, default is 30.', 'lightgray');
141
        Console::log();    
142
        Console::log(Console::text('   -R, --report ', 'white') . Console::text('IP', 'yellow', 'underline')); 
143
        Console::log('       Performs a report request for the given IP address. A valid IPv4 or IPv6 address is required.', 'lightgray');
144
        Console::log();    
145
        Console::log(Console::text('   -V, --bulk-report ', 'white') . Console::text('FILE', 'yellow', 'underline')); 
146
        Console::log('       Performs a bulk-report request sending a csv file. A valid file name or full path is required.', 'lightgray');
147
        Console::log();    
148
        Console::log(Console::text('   -E, --clear ', 'white')); 
149
        Console::log('       Remove own reports for the given IP address. A valid IPv4 or IPv6 address is required.', 'lightgray');
150
        Console::log();
151
        Console::log(Console::text('   -c, --categories ', 'white') . Console::text('CATEGORIES', 'yellow', 'underline')); 
152
        Console::log('       For a report request, defines the report category(ies). Categories must be separate by a comma.', 'lightgray');
153
        Console::log('       Some categories cannot be used alone. A category can be represented by its shortname or by its', 'lightgray');
154
        Console::log(Console::text('       id. Use ','lightgray')  . Console::text('abuseipdb -L', 'white') . Console::text(' to print the categories list.','lightgray'));
155
        Console::log();    
156
        Console::log(Console::text('   -m, --message ', 'white') . Console::text('MESSAGE', 'yellow', 'underline')); 
157
        Console::log('       For a report request, defines the message to send with report. Message is required for all', 'lightgray');
158
        Console::log('       report requests.', 'lightgray');
159
        Console::log();
160
        Console::log(Console::text('   -B, --blacklist ', 'white')); 
161
        Console::log('       Performs a blacklist request. Default limit is 1000. This limit can ne changed with the', 'lightgray');
162
        Console::log('       ' . Console::text('--limit', 'white') . Console::text(' parameter. ', 'lightgray'));
163
        Console::log();    
164
        Console::log(Console::text('   -l, --limit ', 'white') . Console::text('LIMIT', 'yellow', 'underline')); 
165
        Console::log('       For a blacklist request, defines the limit.', 'lightgray');
166
        Console::log('       For a check request with verbose flag, sets the max number of last reports displayed. Default is 10', 'lightgray');
167
        Console::log('       For a check-block request, sets the max number of IPs displayed. Default is 0 (no limit).', 'lightgray');
168
        Console::log();    
169
        Console::log(Console::text('   -o, --output ', 'white') . Console::text('FORMAT', 'yellow', 'underline')); 
170
        Console::log('       Defines the output format for API requests. Default is a colorized report, possible formats are', 'lightgray');
171
        Console::log('       '. Console::text('json', 'yellow', 'underline') . ' or ' . Console::text('plaintext', 'yellow', 'underline') . '. Plaintext option prints partial response (blacklist: IPs list, ');
172
        Console::log('       check or report: confidence score only, check-block: reported IPs list with confidence score, ', 'lightgray');
173
        Console::log('       bulk-report: number of saved reports, clear: number of deleted reports).', 'lightgray');
174
        Console::log();    
175
        Console::log(Console::text('   -s, --score ', 'white'). Console::text('SCORE', 'yellow', 'underline')); 
176
        Console::log('       For a blacklist request, sets the confidence score minimum. The confidence minimum ', 'lightgray');
177
        Console::log('       must be between 25 and 100. This parameter is subscriber feature (not honored otherwise, allways 100).', 'lightgray');
178
        Console::log();    
179
        Console::log(Console::text('   -t, --timeout ', 'white'). Console::text('TIMEOUT', 'yellow', 'underline')); 
180
        Console::log('       Define the timeout in API request and overwrite the value defined in conf.ini or local.ini.', 'lightgray');
181
        Console::log('       Timeout is expressed in milliseconds.', 'lightgray');
182
        Console::log();    
183
        Console::log(Console::text('   -v, --verbose ', 'white')); 
184
        Console::log('       For a check request, display additional fields like the x last reports. This increases ', 'lightgray');
185
        Console::log(Console::text('       request time and response size. Max number of last reports displayed can be changed with the ', 'lightgray'));
186
        Console::log('       ' . Console::text('--limit', 'white') . Console::text(' parameter. ', 'lightgray'));
187
        Console::log();    
188
        Console::log(Console::text('   --version', 'white')); 
189
        Console::log('       Prints the current version.', 'lightgray');
190
        Console::log(); 
191
        self::printFooter();
192
    }
193
194
    /**
195
     * Prints the current config and exit
196
     * 
197
     * @access protected
198
     * @static
199
     * 
200
     * @return void
201
     */
202
    protected static function printConfig(): void
203
    {
204
        $conf = self::$api->getConfig();
205
206
        self::printTitle(Console::text('  â–º Current configuration ', 'darkgray'));
207
        
208
        Console::log(Console::text('  api_key:[', 'white') . Console::text($conf['apiKey'], 'green') . Console::text(']', 'white'));
209
        Console::log(Console::text('  timeout:[', 'white') . Console::text($conf['timeout'], 'green') . Console::text(']', 'white'));
210
        Console::log(Console::text('  self_ips:', 'white'));
211
        
212
        foreach ($conf['selfIps'] as $ip) {
213
            Console::log(Console::text('    [', 'white') . Console::text($ip, 'green') . Console::text(']', 'white'));   
214
        }
215
216
        Console::log();   
217
        self::printFooter();
218
    }
219
220
    /**
221
     * Prints the report categories list
222
     * 
223
     * @access protected
224
     * @static
225
     * 
226
     * @return void
227
     */
228
    protected static function printCategories(): void
229
    {
230
        self::printTitle(Console::text('  â–º Report categories list ', 'darkgray'));
231
232
        $categories = ApiHandler::getCategories();
233
        $rowHeaders = [
234
            Console::text('ShortName',      'darkgray') => 15, 
235
            Console::text('Id',             'darkgray') => 2, 
236
            Console::text('Full name',      'darkgray') => 18,
237
            Console::text('Can be alone?',  'darkgray') => 15
238
        ];
239
        Console::$verticalSeparator = '  ';
240
        Console::$verticalInnerSeparator = '  ';
241
        Console::log(Console::tableRowSeparator($rowHeaders, 'darkgray'));
242
        Console::log(Console::tableRow($rowHeaders));      
243
        Console::log(Console::tableRowSeparator($rowHeaders), 'darkgray');
244
        
245
        foreach ($categories as $cat) {
246
            $id = Console::text($cat[1], 'white');
247
            $standalone = $cat[3] ? Console::text('✓', 'green') . Console::text(' true ', 'lightgray') : 
248
                                    Console::text('✗', 'red')   . Console::text(' false', 'darkgray');
249
            $shortName =  Console::text($cat[0], 'white');
250
            $fullName =   Console::text($cat[2], 'lightgray');
251
252
            Console::log(
253
                Console::TableRowStart().  
254
                Console::TableRowCell( $shortName , 15).  
255
                Console::TableRowCell( $id , 2, Console::ALIGN_CENTER).  
256
                Console::TableRowCell( $fullName , 18).  
257
                Console::TableRowCell( $standalone , 15,  Console::ALIGN_CENTER)  
258
            );
259
        }
260
        //Console::log(Console::tableRowSeparator($rowHeaders), 'darkgray');
261
        Console::log();
262
        self::printFooter();
263
    }
264
265
    /**
266
     * Perform a report request 
267
     * 
268
     * @access protected
269
     * @static
270
     * @param array $arguments
271
     * 
272
     * @return void
273
     */
274
    protected static function reportIP(array $arguments): void
275
    {
276
        $ip      = self::getArgumentValue($arguments,'R', 'report');
277
        $cats    = self::getArgumentValue($arguments,'c', 'categories');
278
        $message = self::getArgumentValue($arguments,'m', 'message');
279
        
280
        self::printTitle(Console::text('  â–º Report IP: ', 'darkgray') . Console::text(escapeshellcmd($ip), 'white'));
281
        self::printTempMessage();
282
283
        // Peforms request 
284
        $timeStart = microtime(true);
285
        $report = self::$api->report($ip, $cats, $message)->getObject();     
286
        $timeEnd = microtime(true);
287
        $time = $timeEnd - $timeStart; // request time
288
        self::clearTempMessage();
289
        
290
        // check for errors / empty response
291
        if (self::hasErrors($report)){
292
            self::printFooter();
293
            Program::exit(1);
294
        }
295
               
296
        // ✓ Done: print reported IP and confidence score
297
        $score = empty($report->data->abuseConfidenceScore) ? 0 : $report->data->abuseConfidenceScore;
298
        $scoreColor = self::getScoreColor($score);
299
300
        switch (self::$outputFormat){
301
            case self::OUTPUT_JSON:
302
                echo json_encode($report, JSON_PRETTY_PRINT);
303
                break;
304
       
305
            case self::OUTPUT_DEFAULT:  
306
                Console::log(
307
                    Console::text('   ✓', 'green').Console::text(' IP: [', 'white') .
308
                    Console::text($ip, $scoreColor).Console::text('] successfully reported', 'white')
309
                );
310
                Console::log(Console::text('     Confidence score: ', 'white').self::getScoreBadge($score));
311
                Console::log();
312
                self::printFooter($time);
313
                break;
314
315
            case self::OUTPUT_PLAINTEXT:
316
                echo $score.PHP_EOL;
317
                break;
318
319
        }
320
    }
321
322
    /**
323
     * Perform a bulk-report request 
324
     * 
325
     * @access protected
326
     * @static
327
     * @param array $arguments
328
     * 
329
     * @return void
330
     */
331
    protected static function bulkReport(array $arguments): void
332
    {
333
        $fileName = self::getArgumentValue($arguments,'V', 'bulk-report');
334
335
        self::printTitle(Console::text('  â–º Bulk report for file: ', 'darkgray') . Console::text(escapeshellcmd($fileName), 'white'));
336
        self::printTempMessage();
337
338
        // Peforms request 
339
        $timeStart = microtime(true);  
340
        $response = self::$api->bulkReport($fileName)->getObject();     
341
        $timeEnd = microtime(true);      
342
        $time = $timeEnd - $timeStart;  // request time
343
        self::clearTempMessage();
344
345
        // check for errors / empty response
346
        if (self::hasErrors($response)){
347
            self::printFooter();
348
            Program::exit(1);
349
        }
350
351
        // ✓ Done
352
        switch (self::$outputFormat){
353
            case self::OUTPUT_JSON:
354
                echo json_encode($response, JSON_PRETTY_PRINT);
355
                break;
356
        
357
            case self::OUTPUT_DEFAULT:  
358
                self::printBulkReportDetail($fileName);
359
                self::printBulkReportSavedReports($response);
0 ignored issues
show
It seems like $response can also be of type null; however, parameter $response of Kristuff\AbuseIPDB\Abuse...ulkReportSavedReports() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

359
                self::printBulkReportSavedReports(/** @scrutinizer ignore-type */ $response);
Loading history...
360
                self::printBulkReportErrors($response);
0 ignored issues
show
It seems like $response can also be of type null; however, parameter $response of Kristuff\AbuseIPDB\Abuse...printBulkReportErrors() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

360
                self::printBulkReportErrors(/** @scrutinizer ignore-type */ $response);
Loading history...
361
                Console::log();
362
                self::printFooter($time);
363
                break;
364
365
            case self::OUTPUT_PLAINTEXT:
366
                $nbSavedReports = isset($response->data->savedReports) ? $response->data->savedReports : 0;
367
                echo $nbSavedReports . PHP_EOL;
368
                break;
369
370
        }
371
    }
372
373
    /**
374
     * Perform a clear-address request 
375
     * 
376
     * @access protected
377
     * @static
378
     * @param array $arguments
379
     * 
380
     * @return void
381
     */
382
    protected static function clearIP(array $arguments): void
383
    {
384
        $ip = self::getArgumentValue($arguments,'E', 'clear');
385
        self::printTitle(Console::text('  â–º Clear reports for IP: ', 'darkgray') . Console::text(escapeshellcmd($ip), 'white'));
386
387
        // Peforms request 
388
        self::printTempMessage();
389
        $timeStart = microtime(true);
390
        $response = self::$api->clearAddress($ip)->getObject();     
391
        $timeEnd = microtime(true);
392
        $time = $timeEnd - $timeStart; // request time
393
        self::clearTempMessage();
394
395
        // check for errors / empty response
396
        if (self::hasErrors($response)){
397
            self::printFooter($time);
398
            Program::exit(1);
399
        }
400
        
401
        // ✓ Done: print deleted report number 
402
        switch (self::$outputFormat){
403
            case self::OUTPUT_JSON:
404
                echo json_encode($response, JSON_PRETTY_PRINT);
405
                break;
406
       
407
            case self::OUTPUT_DEFAULT:  
408
                Console::log(
409
                    Console::text('   ✓', 'green') . 
410
                    Console::text(' Successfull clear request for IP: [', 'white') .
411
                    Console::text($ip, 'lightyellow') .
412
                    Console::text(']', 'white')
413
                );
414
                self::printResult('     Deleted reports: ', $response->data->numReportsDeleted ?? 0, 'lightyellow');
415
                Console::log();
416
                self::printFooter($time);
417
                break;
418
419
            case self::OUTPUT_PLAINTEXT:
420
                echo ($response->data->numReportsDeleted ?? 0) . PHP_EOL;
421
                break;
422
423
        }
424
    }
425
426
    /**
427
     * Perform a blacklist request 
428
     * 
429
     * @access protected
430
     * @static
431
     * @param array $arguments
432
     * 
433
     * @return void
434
     */
435
    protected static function getBlacklist(array $arguments): void
436
    {
437
        self::printTitle(Console::text('  â–º Get Blacklist ', 'darkgray'));
438
439
        $plainText  = (self::$outputFormat === self::OUTPUT_PLAINTEXT); 
440
        $limit      = self::getNumericParameter($arguments,'l', 'limit', 1000);
441
        $scoreMin   = self::getNumericParameter($arguments,'s', 'score', 100);
442
        
443
        self::printTempMessage();
444
        
445
        // do request 
446
        $timeStart = microtime(true);
447
        $response = self::$api->blacklist($limit, $plainText, $scoreMin);
448
        $timeEnd = microtime(true);
449
        $time = $timeEnd - $timeStart; // request time
450
451
        self::clearTempMessage();
452
    
453
        // response could be json on error, while plaintext flag is set
454
        $decodedResponse = $response->getObject();
455
456
        if (self::hasErrors($decodedResponse, false)){
457
            self::printFooter($time);
458
            Program::exit(1);
459
        }
460
461
        // ✓ Done: print deleted report number 
462
        switch (self::$outputFormat){
463
            case self::OUTPUT_JSON:
464
                echo json_encode($decodedResponse, JSON_PRETTY_PRINT);
465
                break;
466
       
467
            case self::OUTPUT_DEFAULT:  
468
                 // print list
469
                self::printResult('  List generated at: ', self::getDate($decodedResponse->meta->generatedAt), 'lightyellow', '');
470
                Console::log();
471
                foreach ($decodedResponse->data as $report){
472
                    $score = empty($report->abuseConfidenceScore) ? 0 : $report->abuseConfidenceScore;
473
                    $defaultColor = self::getScoreColor($score);
474
475
                    $line  = Console::text('    →', $defaultColor);
476
                    $line .= self::printResult(' IP: ', $report->ipAddress, $defaultColor, '', false);
477
                    $line .= self::printResult(' | Last reported at: ', self::getDate($report->lastReportedAt), $defaultColor, '', false);
478
                    $line .= Console::text(' | Confidence score: ', 'white');
479
                    $line .= self::getScoreBadge($score);
480
                    Console::log($line);
481
                }
482
483
                Console::log();
484
                self::printFooter($time);
485
                break;
486
487
            case self::OUTPUT_PLAINTEXT:
488
                // echo response "as is"
489
                Console::log($response->getPlaintext());
490
                break;
491
492
        }
493
    }
494
495
    /**
496
     * Perform a check-block request 
497
     * 
498
     * @access protected
499
     * @static
500
     * @param array $arguments
501
     * 
502
     * @return void
503
     */
504
    protected static function checkBlock(array $arguments): void
505
    {
506
        $network  = self::getArgumentValue($arguments,'K', 'check-block');
507
508
        self::printTitle(Console::text('  â–º Check network: ', 'darkgray') . Console::text(escapeshellcmd($network), 'white') . Console::text('', 'darkgray'));
509
510
        $maxAge   = self::getNumericParameter($arguments, 'd', 'days', 30);
511
        $limit    = self::getNumericParameter($arguments,'l', 'limit', 0); // 0 mean no limit
512
513
        self::printTempMessage();
514
515
        $timeStart = microtime(true);                                       
516
        $check = self::$api->checkBlock($network, $maxAge)->getObject();
517
        $timeEnd = microtime(true);
518
        $time = $timeEnd - $timeStart; // request time
519
        self::clearTempMessage();
520
521
        // check for errors / empty response
522
        if (self::hasErrors($check)){
523
            self::printFooter($time);
524
            Program::exit(1);
525
        }
526
527
        switch (self::$outputFormat){
528
            case self::OUTPUT_JSON:
529
                echo json_encode($check, JSON_PRETTY_PRINT);
530
                break;
531
       
532
            case self::OUTPUT_DEFAULT:  
533
                self::printCheckBlockDetail($check);
0 ignored issues
show
It seems like $check can also be of type null; however, parameter $response of Kristuff\AbuseIPDB\Abuse...printCheckBlockDetail() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

533
                self::printCheckBlockDetail(/** @scrutinizer ignore-type */ $check);
Loading history...
534
                self::printCheckBlockReportedIP($check,$maxAge,$limit);
0 ignored issues
show
It seems like $check can also be of type null; however, parameter $response of Kristuff\AbuseIPDB\Abuse...tCheckBlockReportedIP() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

534
                self::printCheckBlockReportedIP(/** @scrutinizer ignore-type */ $check,$maxAge,$limit);
Loading history...
535
                Console::log();
536
                self::printFooter($time);
537
                break;
538
539
            case self::OUTPUT_PLAINTEXT:
540
                $nbReports = isset($check->data->reportedAddress) ? count($check->data->reportedAddress) : 0;
541
                if ($nbReports > 0) {
542
                    $numberDiplayedReports = 0;
543
                    foreach ($check->data->reportedAddress as $report){
544
                        echo ($report->ipAddress) . ' ' . $report->abuseConfidenceScore . PHP_EOL;
545
546
                        // counter
547
                        $numberDiplayedReports++;
548
                        if ($numberDiplayedReports === $limit) {
549
                            break;
550
                        }
551
                    }
552
                }
553
                break;
554
        }
555
    }
556
557
    /**
558
     * Perform a check request 
559
     * 
560
     * @access protected
561
     * @static
562
     * @param array $arguments
563
     * 
564
     * @return void
565
     */
566
    protected static function checkIP(array $arguments): void
567
    {
568
        $ip = self::getArgumentValue($arguments,'C', 'check');
569
        
570
        self::printTitle(Console::text('  â–º Check IP: ', 'darkgray') . Console::text(escapeshellcmd($ip), 'white') . Console::text('', 'darkgray'));
571
        
572
        $verbose            = self::inArguments($arguments,'v', 'verbose');
573
        $maxAge             = self::getNumericParameter($arguments, 'd', 'days', 30);
574
        $maxReportsNumber   = self::getNumericParameter($arguments,'l', 'limit', 10);
575
        $ip                 = self::getArgumentValue($arguments,'C', 'check');
576
577
        self::printTempMessage();
578
        $timeStart = microtime(true);                                           
579
        $check = self::$api->check($ip, $maxAge, $verbose)->getObject();        
580
        $timeEnd = microtime(true);                                              
581
        $time = $timeEnd - $timeStart; // request time
582
        self::clearTempMessage();
583
584
        // check for errors / empty response
585
        if (self::hasErrors($check)){
586
            self::printFooter($time);
587
            Program::exit(1);
588
        }
589
590
        // score and data color (depending of abuseConfidenceScore)
591
        $score = empty($check->data->abuseConfidenceScore) ? 0 : $check->data->abuseConfidenceScore;
592
593
        switch (self::$outputFormat){
594
            case self::OUTPUT_JSON:
595
                echo json_encode($check, JSON_PRETTY_PRINT);
596
                break;
597
       
598
            case self::OUTPUT_DEFAULT:  
599
                $defaultColor = self::getScoreColor($score);
600
                self::printCheckScore($check);
0 ignored issues
show
It seems like $check can also be of type null; however, parameter $response of Kristuff\AbuseIPDB\Abuse...ient::printCheckScore() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

600
                self::printCheckScore(/** @scrutinizer ignore-type */ $check);
Loading history...
601
                self::printCheckDetail($check, $defaultColor);
0 ignored issues
show
It seems like $check can also be of type null; however, parameter $response of Kristuff\AbuseIPDB\Abuse...ent::printCheckDetail() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

601
                self::printCheckDetail(/** @scrutinizer ignore-type */ $check, $defaultColor);
Loading history...
602
                self::printCheckReports($check, $maxAge, $defaultColor);
0 ignored issues
show
It seems like $check can also be of type null; however, parameter $response of Kristuff\AbuseIPDB\Abuse...nt::printCheckReports() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

602
                self::printCheckReports(/** @scrutinizer ignore-type */ $check, $maxAge, $defaultColor);
Loading history...
603
                if ($verbose){
604
                    self::printCheckLastReports($check, $maxReportsNumber);
0 ignored issues
show
It seems like $check can also be of type null; however, parameter $response of Kristuff\AbuseIPDB\Abuse...printCheckLastReports() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

604
                    self::printCheckLastReports(/** @scrutinizer ignore-type */ $check, $maxReportsNumber);
Loading history...
605
                }
606
                Console::log();
607
                self::printFooter($time);
608
                break;
609
610
            case self::OUTPUT_PLAINTEXT:
611
                echo ($check->data->abuseConfidenceScore ?? 0) . PHP_EOL;
612
                break;
613
614
        }
615
    }
616
}