Utils   F
last analyzed

Complexity

Total Complexity 60

Size/Duplication

Total Lines 369
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 95
dl 0
loc 369
rs 3.6
c 0
b 0
f 0
wmc 60

24 Methods

Rating   Name   Duplication   Size   Complexity  
A isNamespaceExists() 0 3 3
A encryptArrayData() 0 4 1
A makeWritable() 0 10 4
A yaml() 0 3 1
A existMethod() 0 3 1
A isArrayEqual() 0 3 2
A getPathFromNamespace() 0 10 2
A upperCase() 0 9 2
A glob() 0 15 3
A getJustClassName() 0 4 1
A generatorNamespace() 0 3 1
A strtolower() 0 8 2
A returnCallback() 0 3 1
A callbackProcess() 0 3 2
A getNamespace() 0 6 1
A changeClass() 0 25 4
B array_diff_key_recursive() 0 26 11
A slashToBackSlash() 0 3 1
A trace() 0 9 2
A getServiceConf() 0 6 3
A getPathWithPhpExtension() 0 19 4
A resolverClass() 0 7 2
A getRequestPathInfo() 0 6 2
A removeTrace() 0 12 4

How to fix   Complexity   

Complex Class

Complex classes like Utils 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.

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 Utils, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Resta\Support;
4
5
class Utils
6
{
7
    /**
8
     * @var array $bool
9
     */
10
    private static $bool = [];
11
12
    /**
13
     * string upper case
14
     *
15
     * @param $argument
16
     * @param bool $shift
17
     * @return array
18
     */
19
    public static function upperCase($argument,$shift=true)
20
    {
21
        if($shift){
22
            array_shift($argument);
23
        }
24
25
        return array_map(function($argument){
26
            return ucfirst($argument);
27
        },$argument);
28
    }
29
30
    /**
31
     * encrypt array data
32
     *
33
     * @param $class
34
     * @return string
35
     */
36
    public static function encryptArrayData($class)
37
    {
38
        //the serialized class data
39
        return md5(serialize($class));
40
    }
41
42
43
    /**
44
     * @param $argument
45
     * @return array|string
46
     */
47
    public static function strtolower($argument)
48
    {
49
        if(!is_array($argument)){
50
            return strtolower($argument);
51
        }
52
        return array_map(function($argument){
53
            return strtolower($argument);
54
        },$argument);
55
    }
56
57
    /**
58
     * @param array $data
59
     * @return string
60
     */
61
    public static function generatorNamespace($data=array())
62
    {
63
        return str_replace('.php','',implode("\\",$data));
64
    }
65
66
    /**
67
     * @param $class
68
     * @param bool $extension
69
     * @return mixed
70
     */
71
    public static function getPathFromNamespace($class,$extension=true)
72
    {
73
        if($extension){
74
            $default=root.'/'.str_replace("\\","/",$class).'.php';
75
        }
76
        else{
77
            $default=root.'/'.str_replace("\\","/",$class).'';
78
        }
79
80
        return str_replace("/App",'/src/app',$default);
81
    }
82
83
    /**
84
     * @param $namespace
85
     * @return bool
86
     */
87
    public static function isNamespaceExists($namespace)
88
    {
89
        return (is_string($namespace) && class_exists($namespace)) ? true : false;
90
    }
91
92
    /**
93
     * @param $class
94
     * @param $method
95
     * @return bool
96
     */
97
    public static function existMethod($class,$method)
98
    {
99
        return method_exists($class,$method);
100
    }
101
102
    /**
103
     * @param $first
104
     * @param $second
105
     * @return bool
106
     */
107
    public static function isArrayEqual($first,$second)
108
    {
109
        return (count( $first ) == count( $second ) && !array_diff( $first, $second ));
110
    }
111
112
    /**
113
     * @param $path
114
     * @return YamlManager
115
     */
116
    public static function yaml($path)
117
    {
118
        return new YamlManager($path);
119
    }
120
121
    /**
122
     * @param $path
123
     * @param bool $filename
124
     * @return array
125
     */
126
    public static function glob($path,$filename=false)
127
    {
128
        $configList = [];
129
130
        foreach (glob($path.'/*.php') as $config) {
131
132
            $configArray=str_replace(".php","",explode("/",$config));
133
            $configList[end($configArray)]=$config;
134
        }
135
136
        if($filename===true){
137
            return array_keys($configList);
138
        }
139
140
        return $configList;
141
    }
142
143
    /**
144
     * @param $path
145
     */
146
    public static function makeWritable($path)
147
    {
148
        $dir = new \DirectoryIterator($path);
149
150
        foreach ($dir as $item) {
151
152
            chmod($item->getPathname(), 0777);
153
154
            if ($item->isDir() && !$item->isDot()) {
155
                self::makeWritable($item->getPathname());
156
            }
157
        }
158
    }
159
160
    /**
161
     * @param $namespace
162
     * @param string $seperator
163
     * @return mixed
164
     */
165
    public static function getJustClassName($namespace,$seperator="\\")
166
    {
167
        $path = explode($seperator, $namespace);
168
        return array_pop($path);
169
    }
170
171
    /**
172
     * @param $class
173
     * @param array $param
174
     * @return bool
175
     */
176
    public static function changeClass($class,$param=array())
177
    {
178
        $executionPath=$class;
179
        $dt = fopen($executionPath, "r");
180
181
        if($dt!==false){
182
183
            $content = fread($dt, filesize($executionPath));
184
            fclose($dt);
185
186
            foreach ($param as $key=>$value){
187
                $content=str_replace($key,$value,$content);
188
            }
189
190
            $forWrite = fopen($executionPath, "w");
191
192
            if($forWrite!==false){
193
                fwrite($forWrite, $content);
194
                fclose($forWrite);
195
196
                return true;
197
            }
198
        }
199
200
        return false;
201
    }
202
203
    /**
204
     * @param $data
205
     * @param $callback
206
     * @return mixed
207
     */
208
    public static function returnCallback($data,$callback)
209
    {
210
        return call_user_func_array($callback,[$data]);
211
    }
212
213
    /**
214
     * @param $namespace
215
     * @return string
216
     */
217
    public static function getNamespace($namespace)
218
    {
219
        $rootDelete=str_replace(root.''.DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'','',$namespace);
220
221
        return 'App\\'.self::generatorNamespace(
222
                explode(''.DIRECTORY_SEPARATOR.'',$rootDelete)
223
            );
224
225
    }
226
227
    /**
228
     * @param $callback
229
     * @return mixed
230
     */
231
    public static function callbackProcess($callback)
232
    {
233
        return (is_callable($callback)) ? call_user_func_array($callback,[app()]) : $callback;
234
    }
235
236
    /**
237
     * @param $array1
238
     * @param $array2
239
     * @return bool
240
     */
241
    public static function array_diff_key_recursive ($array1, $array2)
242
    {
243
        if(count($array1)!==count($array2)) self::$bool[]=false;
244
245
        foreach ($array1 as $array1_key=>$array1_value){
246
247
            if(!is_array($array1_value)){
248
                if(!array_key_exists($array1_key,$array2)) self::$bool[]=false;
249
            }
250
            else{
251
                if(!array_key_exists($array1_key,$array2)) self::$bool[]=false;
252
253
                if(!isset($array2[$array1_key]) OR !is_array($array2[$array1_key])) $array2[$array1_key]=[];
254
255
                if(isset($array1_value[0])) $array1_value=$array1_value[0];
256
257
                if(isset($array2[$array1_key][0])) $array2[$array1_key]=$array2[$array1_key][0];
258
259
                self::array_diff_key_recursive($array1_value,$array2[$array1_key]);
260
            }
261
        }
262
263
        if(in_array(false,self::$bool)){
264
            return false;
265
        }
266
        return true;
267
    }
268
269
    /**
270
     * @param $data
271
     * @return mixed
272
     */
273
    public static function slashToBackSlash($data)
274
    {
275
        return str_replace("/","\\",$data);
276
    }
277
278
    /**
279
     * get trace
280
     *
281
     * @param int $debug
282
     * @param null|string $key
283
     * @return mixed
284
     */
285
    public static function trace($debug=0,$key=null)
286
    {
287
        $trace = debug_backtrace();
288
289
        if($key===null){
290
            return $trace[$debug] ?? null;
291
        }
292
293
        return $trace[$debug][$key] ?? null;
294
    }
295
296
    /**
297
     * @return array
298
     */
299
    public static function getServiceConf()
300
    {
301
        if(property_exists(core(),'serviceConf') && defined('methodName')){
302
            return core()->serviceConf;
303
        }
304
        return [];
305
    }
306
307
    /**
308
     * @param $files
309
     * @param null|string $reelPath
310
     * @return array
311
     */
312
    public static function getPathWithPhpExtension($files,$reelPath=null)
313
    {
314
        $pathWithPhpList = [];
315
316
        foreach ($files as $file){
317
318
            if(preg_match('@(.*).php@is',$file,$pathWithPhp)){
319
320
                if($reelPath===null){
321
                    $pathWithPhpList[] = $pathWithPhp[0];
322
                }
323
                else{
324
                    $pathWithPhpList[] = $reelPath.'/'.$pathWithPhp[0];
325
                }
326
327
            }
328
        }
329
330
        return $pathWithPhpList;
331
    }
332
333
    /**
334
     * @param $class
335
     * @return mixed
336
     */
337
    public static function resolverClass($class)
338
    {
339
        if(self::isNamespaceExists($class)){
340
            return app()->resolve($class);
341
        }
342
343
        return $class;
344
    }
345
346
    /**
347
     * @return array
348
     */
349
    public static function getRequestPathInfo()
350
    {
351
        if(is_null(BootStaticManager::getRequestPath())){
0 ignored issues
show
introduced by
The condition is_null(Resta\Support\Bo...ager::getRequestPath()) is always false.
Loading history...
352
            return explode("/",request()->getPathInfo());
353
        }
354
        return BootStaticManager::getRequestPath();
0 ignored issues
show
Bug Best Practice introduced by
The expression return Resta\Support\Boo...nager::getRequestPath() returns the type string which is incompatible with the documented return type array.
Loading history...
355
    }
356
357
    /**
358
     * @param $trace
359
     * @param null|string $remove
360
     * @return array
361
     */
362
    public static function removeTrace($trace,$remove=null)
363
    {
364
        $list = [];
365
366
        foreach($trace as $key=>$item){
367
368
            if(isset($item['file']) && !preg_match('@'.$remove.'@',$item['file'])){
369
                $list[$key] = $item;
370
            }
371
        }
372
373
        return array_values($list);
374
    }
375
}