Passed
Branch dev (5a99cc)
by Kris
01:42
created

ErrorsTrait::printPlainTextErrors()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
/**
4
 *     _    _                    ___ ____  ____  ____
5
 *    / \  | |__  _   _ ___  ___|_ _|  _ \|  _ \| __ )
6
 *   / _ \ | '_ \| | | / __|/ _ \| || |_) | | | |  _ \
7
 *  / ___ \| |_) | |_| \__ \  __/| ||  __/| |_| | |_) |
8
 * /_/   \_\_.__/ \__,_|___/\___|___|_|   |____/|____/
9
 *
10
 * This file is part of Kristuff\AbsuseIPDB.
11
 *
12
 * (c) Kristuff <[email protected]>
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 *
17
 * @version    0.9.10
18
 * @copyright  2020-2021 Kristuff
19
 */
20
namespace Kristuff\AbuseIPDB;
21
22
/**
23
 * Trait Errors
24
 * 
25
 */
26
trait ErrorsTrait
27
{
28
    /**
29
     * Check and print errors in API response. Null response object is considered as no errors
30
     * 
31
     * @access protected
32
     * @static
33
     * @param object     $response       
34
     * 
35
     * @return void     
36
     */
37
    protected static function printPlainTextErrors(object $response)
38
    {
39
        foreach ($response->errors as $err){
40
            $text = 'Error: ';
41
            $text .= self::getErrorDetail($err, 'title');
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::getErrorDetail($err, 'title') targeting Kristuff\AbuseIPDB\ErrorsTrait::getErrorDetail() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
42
            $text .= self::getErrorDetail($err, 'statuts');
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::getErrorDetail($err, 'statuts') targeting Kristuff\AbuseIPDB\ErrorsTrait::getErrorDetail() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
43
            $text .= self::getErrorDetail($err, 'parameter', 'source');
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::getErrorDetail($err, 'parameter', 'source') targeting Kristuff\AbuseIPDB\ErrorsTrait::getErrorDetail() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
44
            $text .= self::getErrorDetail($err, 'detail');
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::getErrorDetail($err, 'detail') targeting Kristuff\AbuseIPDB\ErrorsTrait::getErrorDetail() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
45
            $text .= PHP_EOL;
46
            echo $text;
47
        }
48
    }
49
50
    /**
51
     * Get error property if exist
52
     * 
53
     * @access protected
54
     * @static
55
     * @param object     $error       
56
     * @param string     $field       
57
     * 
58
     * @return void     
59
     */
60
    private static function getErrorDetail(object $error, string $field, ?string $parent = null)
61
    {
62
        if (!empty($parent)){
63
            return  !empty($error->$parent) && !empty($error->$parent->$field) ? ' ' . $field . ': ' . $error->$parent->$field : '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return ! empty($error->$...r->$parent->$field : '' returns the type string which is incompatible with the documented return type void.
Loading history...
64
        }
65
66
        return !empty($error->$field) ? ' ' . $field . ': ' . $error->$field : '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return ! empty($error->$...' . $error->$field : '' returns the type string which is incompatible with the documented return type void.
Loading history...
67
    }
68
69
}