Passed
Push — master ( 26efa5...f69765 )
by Alex
03:20
created

IsOK   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 95
Duplicated Lines 21.05 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 28
lcom 0
cbo 0
dl 20
loc 95
rs 10
c 0
b 0
f 0

8 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
C isValidArray() 0 22 9
A isURLValid() 0 10 3
A isChildArrayOK() 0 12 4
isOK() 0 1 ?

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
namespace AlgoWeb\ODataMetadata;
3
4
abstract class IsOK
5
{
6
    protected function isStringNotNullOrEmpty($str)
7
    {
8
        if (!$this->isStringNotNull($str)) {
9
            return false;
10
        }
11
        if (empty(trim($str))) {
12
            return false;
13
        }
14
        return true;
15
    }
16
17
    protected function isStringNotNull($str)
18
    {
19
        if (null == $str) {
20
            return false;
21
        }
22
        if (!is_string($str)) {
23
            return false;
24
        }
25
        return true;
26
    }
27
28 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...
29
    {
30
        if (null == $var) {
31
            return false;
32
        }
33
        if (!($var instanceof $instanceOf)) {
34
            return false;
35
        }
36
        return true;
37
    }
38
39 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...
40
    {
41
        if (null == $var) {
42
            return true;
43
        }
44
        if (!($var instanceof $instanceOf)) {
45
            return false;
46
        }
47
        return false;
48
    }
49
50
    protected function isValidArray($arr, $instanceOf, $minCount = -1, $maxCount = -1)
51
    {
52
        if (null == $arr) {
53
            return false;
54
        }
55
        if (!is_array($arr)) {
56
            return false;
57
        }
58
        $numberOfItem = count($arr);
59
        if (-1 != $minCount && $numberOfItem < $minCount) {
60
            return false;
61
        }
62
        if (-1 != $maxCount && $numberOfItem > $maxCount) {
63
            return false;
64
        }
65
        foreach ($arr as $item) {
66
            if (!($item instanceof $instanceOf)) {
67
                return false;
68
            }
69
        }
70
        return true;
71
    }
72
73
    protected function isChildArrayOK(array $arr, &$msg)
74
    {
75
        foreach ($arr as $item) {
76
            if (!($item instanceof IsOK)) {
77
                $msg = "Child item is not an instance of IsOK";
78
                return false;
79
            }
80
            if (!$item->isOK($msg)) {
81
                return false;
82
            }
83
        }
84
    }
85
86
    abstract protected function isOK(&$msg = null);
87
88
    protected function isURLValid($url)
89
    {
90
        if (!isStringNotNull($url)) {
91
            return false;
92
        }
93
        if (false === filter_var($url, FILTER_VALIDATE_URL)) {
94
            return false;
95
        }
96
        return true;
97
    }
98
}
99