Test Failed
Push — master ( 8536e5...0f618b )
by Christopher
07:20
created

IsOKToolboxTrait::isNullInstanceOf()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 6
nc 3
nop 2
1
<?php
2
3
namespace AlgoWeb\ODataMetadata\IsOKTraits;
4
5
use AlgoWeb\ODataMetadata\IsOK;
6
7
trait IsOKToolboxTrait
8
{
9
    protected function isStringNotNullOrEmpty($str)
10
    {
11
        if (!$this->isStringNotNull($str)) {
12
            return false;
13
        }
14
        if (empty(trim($str))) {
15
            return false;
16
        }
17
        return true;
18
    }
19
20
    protected function isStringNotNull($str)
21
    {
22
        if (null == $str) {
23
            return false;
24
        }
25
        if (!is_string($str)) {
26
            return false;
27
        }
28
        return true;
29
    }
30
31 View Code Duplication
    protected function isNotNullInstanceOf($var, $instanceOf)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
    {
33
        if (null == $var) {
34
            return false;
35
        }
36
        if (!($var instanceof $instanceOf)) {
37
            return false;
38
        }
39
        return true;
40
    }
41
42 View Code Duplication
    protected function isNullInstanceOf($var, $instanceOf)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44
        if (null == $var) {
45
            return true;
46
        }
47
        if (!($var instanceof $instanceOf)) {
48
            return false;
49
        }
50
        return false;
51
    }
52
53
    protected function isURLValid($url)
54
    {
55
        if (!$this->isStringNotNull($url)) {
56
            return false;
57
        }
58
        if (false === filter_var($url, FILTER_VALIDATE_URL)) {
59
            return false;
60
        }
61
        return true;
62
    }
63
64
    protected function isObjectNullOrOK(IsOK $object = null, &$msg = null)
65
    {
66
        if (null == $object) {
67
            return true;
68
        }
69
        return $object->isOK($msg);
70
    }
71
72
    protected function isObjectNullOrType($instanceOf, IsOK $object = null, &$msg = null)
73
    {
74
        if (null == $object) {
75
            return true;
76
        }
77
        if (!$object instanceof $instanceOf) {
78
            $msg = "Supplied object not an instance of ".$instanceOf;
79
            return false;
80
        }
81
        return $object->isOK($msg);
82
    }
83
84
    protected function isValidArrayOK(array $arr = null, $instanceOf, &$msg = null, $minCount = -1, $maxCount = -1)
85
    {
86
        $result = $this->isValidArray($arr, $instanceOf, $minCount, $maxCount);
87
        if (!$result) {
88
            $msg = "Supplied array not a valid array";
89
            return false;
90
        }
91
92
        return $this->isChildArrayOK($arr, $msg);
93
    }
94
95
    protected function isValidArray(array $arr = null, $instanceOf, $minCount = -1, $maxCount = -1)
96
    {
97
        if (null == $arr && 0 >= $minCount) {
98
            return true;
99
        }
100
        $numberOfItem = count($arr);
101
        if (-1 != $minCount && $numberOfItem < $minCount) {
102
            return false;
103
        }
104
        if (-1 != $maxCount && $numberOfItem > $maxCount) {
105
            return false;
106
        }
107
108
        foreach ($arr as $item) {
0 ignored issues
show
Bug introduced by
The expression $arr of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
109
            if (!($item instanceof $instanceOf)) {
110
                return false;
111
            }
112
        }
113
        return true;
114
    }
115
116
    protected function isChildArrayOK(array $arr = null, &$msg)
117
    {
118
        if ($arr == null || empty($arr)) {
119
            return true;
120
        }
121
        foreach ($arr as $item) {
122
            if (!($item instanceof IsOK)) {
123
                $msg = "Child item is not an instance of IsOK";
124
                return false;
125
            }
126
            if (!$item->isOK($msg)) {
127
                return false;
128
            }
129
        }
130
        return true;
131
    }
132
}
133