Completed
Pull Request — master (#96)
by Alex
04:26
created

IsOKToolboxTrait   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 123
Duplicated Lines 16.26 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 34
lcom 0
cbo 1
dl 20
loc 123
rs 9.2
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A isStringNotNullOrEmpty() 0 10 3
A isStringNotNull() 0 10 3
A isNotNullInstanceOf() 10 10 3
A isNullInstanceOf() 10 10 3
A isURLValid() 0 10 3
A isObjectNullOrOK() 0 7 2
A isObjectNullOrType() 0 11 3
A isValidArrayOK() 0 10 2
B isValidArray() 0 17 7
B isChildArrayOK() 0 16 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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. ": " . get_class($this);
79
            return false;
80
        }
81
        return $object->isOK($msg);
82
    }
83
84
    protected function isValidArrayOK(array $arr, $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: " . get_class($this);
89
            return false;
90
        }
91
92
        return $this->isChildArrayOK($arr, $msg);
93
    }
94
95
    protected function isValidArray(array $arr, $instanceOf, $minCount = -1, $maxCount = -1)
96
    {
97
        $numberOfItem = count($arr);
98
        if (-1 != $minCount && $numberOfItem < $minCount) {
99
            return false;
100
        }
101
        if (-1 != $maxCount && $numberOfItem > $maxCount) {
102
            return false;
103
        }
104
105
        foreach ($arr as $item) {
106
            if (!($item instanceof $instanceOf)) {
107
                return false;
108
            }
109
        }
110
        return true;
111
    }
112
113
    protected function isChildArrayOK(array $arr, &$msg)
114
    {
115
        if (empty($arr)) {
116
            return true;
117
        }
118
        foreach ($arr as $item) {
119
            if (!($item instanceof IsOK)) {
120
                $msg = "Child item is not an instance of IsOK: " . get_class($this);
121
                return false;
122
            }
123
            if (!$item->isOK($msg)) {
124
                return false;
125
            }
126
        }
127
        return true;
128
    }
129
}
130