GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Util   B
last analyzed

Complexity

Total Complexity 40

Size/Duplication

Total Lines 182
Duplicated Lines 22.53 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 40
lcom 0
cbo 2
dl 41
loc 182
rs 8.2608
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A stripPhpComments() 0 11 2
A boolToString() 0 4 2
A filterSitesByName() 0 14 4
A findInArrayWithRegex() 0 10 3
A filterPagesByModules() 14 14 4
B filterPagesByEnvironmentType() 14 14 5
A filterPagesByVersion() 13 13 4
B filterPagesByDefaultAdmin() 0 13 5
A VersionStringsToArray() 0 16 4
A VersionStringsGreaterThenOrEqual() 0 12 3
A forceStringMinLength() 0 6 2
A reportError() 0 7 2

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Util often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Util, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Selim;
4
5
class Util
6
{
7
    public static function stripPhpComments($str)
8
    {
9
        $regex_multiline = "/\\/\\*[^\\Z]*\\*\\//m";
10
        $regex_singleline = "/\\/\\/.*/m";
11
        $result = preg_filter($regex_multiline, "", $str);
12
        if ($result) {
13
            $str = $result;
14
        }
15
16
        return preg_filter($regex_singleline, "", $str);
17
    }
18
19
    public static function boolToString($bool)
20
    {
21
        return $bool ? 'true' : 'false';
22
    }
23
24
    public static function filterSitesByName(array $sites, $filterRegex)
25
    {
26
        $arr = array();
27
        foreach ($sites as $sc) {
28
            if (!$sc instanceof SiteConfig) {
29
                continue;
30
            }
31
            if (preg_match("/$filterRegex/", $sc->name)) {
32
                array_push($arr, $sc);
33
            }
34
        }
35
36
        return $arr;
37
    }
38
39
    public static function findInArrayWithRegex(array $arr, $regex)
40
    {
41
        foreach ($arr as $a) {
42
            if (preg_match($regex, $a)) {
43
                return $a;
44
            }
45
        }
46
47
        return null;
48
    }
49
50
    /**
51
     * @param array $sspages should contain only objects of class SilverstripePage
52
     * @param string $filterRegex
53
     * @return array all SilverstripePages that match the regex
54
     *
55
     */
56 View Code Duplication
    public static function filterPagesByModules(array $sspages, $filterRegex)
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...
57
    {
58
        $arr = array();
59
        foreach ($sspages as $sspage) {
60
            if (!$sspage instanceof SilverstripePage) {
61
                continue;
62
            }
63
            if ($sspage->hasModule("/$filterRegex/")) {
64
                array_push($arr, $sspage);
65
            }
66
        }
67
68
        return $arr;
69
    }
70
71
72
    /**
73
     * @param array $sspages
74
     * @param string $env environment type dev OR live OR test
75
     * @return array  all SilverstripePages that have environment_type $env
76
     */
77 View Code Duplication
    public static function filterPagesByEnvironmentType($sspages, $env) {
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...
78
        $arr = array();
79
        if(preg_match("/^(dev|test|live)$/",$env) === 1){
80
            foreach ($sspages as $sspage) {
81
                if (!$sspage instanceof SilverstripePage) {
82
                    continue;
83
                }
84
                if ($env === $sspage->getEnvironmentType()) {
85
                    array_push($arr, $sspage);
86
                }
87
            }
88
        }
89
        return $arr;
90
    }
91
92
    /**
93
     * @param array $sspages
94
     * @param string $filterRegex environment type dev OR live OR test
95
     *
96
     * @return array all SilverstripePages where the version string matches $filterRegex
97
     */
98 View Code Duplication
    public static function filterPagesByVersion($sspages, $filterRegex) {
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...
99
        $arr = array();
100
        foreach ($sspages as $sspage) {
101
            if (!$sspage instanceof SilverstripePage) {
102
                continue;
103
            }
104
            if (preg_match("/$filterRegex/",$sspage->getVersion())) {
105
                array_push($arr, $sspage);
106
            }
107
        }
108
109
        return $arr;
110
    }
111
112
    /**
113
     * @param SilverstripePage[] $sspages
114
     * @param bool $da_val
115
     * @return SilverstripePage[]
116
     */
117
    public static function filterPagesByDefaultAdmin($sspages, $da_val = true) {
118
        $arr = array();
119
        $da_val = $da_val === true ? true : false;
120
        foreach ($sspages as $sspage) {
121
            if (!$sspage instanceof SilverstripePage) {
122
                continue;
123
            }
124
            if ($sspage->hasDefaultAdmin() === $da_val) {
125
                array_push($arr, $sspage);
126
            }
127
        }
128
        return $arr;
129
    }
130
131
    /**
132
     * @param $version "1.1.1" "2.1.4" "2.0"
133
     *
134
     * @return array [1,1,1] [2,1,4] [2,0,0]
135
     */
136
    public static function VersionStringsToArray($version)
137
    {
138
        $arr = array();
139
        $single = explode(".", $version);
140
141
        for ($i = 0;$i<3;$i++) {
142
            if (!isset($single[$i])) {
143
                $x = 0;
144
            } else {
145
                $x = intval($single[$i]);
146
            }
147
            array_push($arr, $x ? $x : 0);
148
        }
149
150
        return $arr;
151
    }
152
153
    public static function VersionStringsGreaterThenOrEqual($version1, $version2)
154
    {
155
        $v1 = Util::VersionStringsToArray($version1);
156
        $v2 = Util::VersionStringsToArray($version2);
157
        for ($i = 0; $i<3;$i++) {
158
            if ($v1[$i] < $v2[$i]) {
159
                return false;
160
            }
161
        }
162
163
        return true;
164
    }
165
166
    /**
167
     * puts blanks at the end of a string until it reaches the minimum length.
168
     *
169
     * @param string $str
170
     * @param int    $min
171
     */
172
    public static function forceStringMinLength(&$str, $min)
173
    {
174
        while (strlen($str) < $min) {
175
            $str .= " ";
176
        }
177
    }
178
179
    public static function reportError($string,$die = true) {
180
        if($die) {
181
            exit($string);
0 ignored issues
show
Coding Style Compatibility introduced by
The method reportError() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
182
        }else{
183
            echo($string);
184
        }
185
    }
186
}
187