Total Complexity | 74 |
Total Lines | 446 |
Duplicated Lines | 0 % |
Changes | 0 |
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 |
||
8 | |||
9 | class Utils |
||
10 | { |
||
11 | /** |
||
12 | * @var array $bool |
||
13 | */ |
||
14 | private static $bool=[]; |
||
15 | |||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | private static $recursiveArray= []; |
||
20 | |||
21 | /** |
||
22 | * @return \DI\Container |
||
23 | */ |
||
24 | public static function callBuild() |
||
25 | { |
||
26 | //di-container |
||
27 | return \DI\ContainerBuilder::buildDevContainer(); |
||
28 | } |
||
29 | |||
30 | |||
31 | /** |
||
32 | * @param null $class |
||
33 | * @return mixed |
||
34 | * @throws \DI\DependencyException |
||
35 | * @throws \DI\NotFoundException |
||
36 | */ |
||
37 | public static function resolve($class=null) |
||
38 | { |
||
39 | //class resolve |
||
40 | if($class!==null){ |
||
41 | $container = self::callBuild(); |
||
42 | return $container->get($class); |
||
43 | } |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param $argument |
||
48 | * @param bool $shift |
||
49 | * @return array |
||
50 | */ |
||
51 | public static function upperCase($argument,$shift=true) |
||
52 | { |
||
53 | if($shift){ |
||
54 | array_shift($argument); |
||
55 | } |
||
56 | |||
57 | return array_map(function($argument){ |
||
58 | return ucfirst($argument); |
||
59 | },$argument); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * encrypt array data |
||
64 | * |
||
65 | * @param $class |
||
66 | * @return string |
||
67 | */ |
||
68 | public static function encryptArrayData($class) |
||
69 | { |
||
70 | //the serialized class data |
||
71 | return md5(serialize($class)); |
||
72 | } |
||
73 | |||
74 | |||
75 | /** |
||
76 | * @param $argument |
||
77 | * @return array|string |
||
78 | */ |
||
79 | public static function strtolower($argument) |
||
80 | { |
||
81 | if(!is_array($argument)){ |
||
82 | return strtolower($argument); |
||
83 | } |
||
84 | return array_map(function($argument){ |
||
85 | return strtolower($argument); |
||
86 | },$argument); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param array $data |
||
91 | * @return string |
||
92 | */ |
||
93 | public static function generatorNamespace($data=array()) |
||
94 | { |
||
95 | return str_replace('.php','',implode("\\",$data)); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param $class |
||
100 | * @param bool $extension |
||
101 | * @return mixed |
||
102 | */ |
||
103 | public static function getPathFromNamespace($class,$extension=true) |
||
104 | { |
||
105 | if($extension){ |
||
106 | $default=root.'/'.str_replace("\\","/",$class).'.php'; |
||
107 | } |
||
108 | else{ |
||
109 | $default=root.'/'.str_replace("\\","/",$class).''; |
||
110 | } |
||
111 | |||
112 | return str_replace("/App",'/src/app',$default); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param $namespace |
||
117 | * @return bool |
||
118 | */ |
||
119 | public static function isNamespaceExists($namespace) |
||
120 | { |
||
121 | return (is_string($namespace) && class_exists($namespace)) ? true : false; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @param $class |
||
126 | * @param $method |
||
127 | * @return bool |
||
128 | */ |
||
129 | public static function existMethod($class,$method) |
||
130 | { |
||
131 | return method_exists($class,$method); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @param $first |
||
136 | * @param $second |
||
137 | * @return bool |
||
138 | */ |
||
139 | public static function isArrayEqual($first,$second) |
||
140 | { |
||
141 | return (count( $first ) == count( $second ) && !array_diff( $first, $second )); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @param $path |
||
146 | * @return YamlManager |
||
147 | */ |
||
148 | public static function yaml($path) |
||
149 | { |
||
150 | return new YamlManager($path); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param $path |
||
155 | * @param bool $filename |
||
156 | * @return array |
||
157 | */ |
||
158 | public static function glob($path,$filename=false) |
||
159 | { |
||
160 | $configList = []; |
||
161 | |||
162 | foreach (glob($path.'/*.php') as $config) { |
||
163 | |||
164 | $configArray=str_replace(".php","",explode("/",$config)); |
||
165 | $configList[end($configArray)]=$config; |
||
166 | } |
||
167 | |||
168 | if($filename===true){ |
||
169 | return array_keys($configList); |
||
170 | } |
||
171 | |||
172 | return $configList; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @param $path |
||
177 | */ |
||
178 | public static function makeWritable($path) |
||
179 | { |
||
180 | $dir = new \DirectoryIterator($path); |
||
181 | |||
182 | foreach ($dir as $item) { |
||
183 | |||
184 | chmod($item->getPathname(), 0777); |
||
185 | |||
186 | if ($item->isDir() && !$item->isDot()) { |
||
187 | self::makeWritable($item->getPathname()); |
||
188 | } |
||
189 | } |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @param $namespace |
||
194 | * @param string $seperator |
||
195 | * @return mixed |
||
196 | */ |
||
197 | public static function getJustClassName($namespace,$seperator="\\") |
||
198 | { |
||
199 | $path = explode($seperator, $namespace); |
||
200 | return array_pop($path); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @param $class |
||
205 | * @param array $param |
||
206 | * @return bool |
||
207 | */ |
||
208 | public static function changeClass($class,$param=array()) |
||
209 | { |
||
210 | $executionPath=$class; |
||
211 | $dt = fopen($executionPath, "r"); |
||
212 | $content = fread($dt, filesize($executionPath)); |
||
213 | fclose($dt); |
||
214 | |||
215 | foreach ($param as $key=>$value){ |
||
216 | $content=str_replace($key,$value,$content); |
||
217 | } |
||
218 | |||
219 | $dt = fopen($executionPath, "w"); |
||
220 | fwrite($dt, $content); |
||
221 | fclose($dt); |
||
222 | |||
223 | return true; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * @param $data |
||
228 | * @param $callback |
||
229 | * @return mixed |
||
230 | */ |
||
231 | public static function returnCallback($data,$callback) |
||
232 | { |
||
233 | return call_user_func_array($callback,[$data]); |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @param $namespace |
||
238 | * @return string |
||
239 | */ |
||
240 | public static function getNamespace($namespace) |
||
241 | { |
||
242 | $rootDelete=str_replace(root.''.DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'','',$namespace); |
||
243 | |||
244 | return 'App\\'.self::generatorNamespace( |
||
245 | explode(''.DIRECTORY_SEPARATOR.'',$rootDelete) |
||
246 | ); |
||
247 | |||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @param $callback |
||
252 | * @return mixed |
||
253 | */ |
||
254 | public static function callbackProcess($callback) |
||
255 | { |
||
256 | return (is_callable($callback)) ? call_user_func_array($callback,[app()]) : $callback; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * @param $array1 |
||
261 | * @param $array2 |
||
262 | * @return bool |
||
263 | */ |
||
264 | public static function array_diff_key_recursive ($array1, $array2) |
||
265 | { |
||
266 | if(count($array1)!==count($array2)) self::$bool[]=false; |
||
267 | |||
268 | foreach ($array1 as $array1_key=>$array1_value){ |
||
269 | |||
270 | if(!is_array($array1_value)){ |
||
271 | if(!array_key_exists($array1_key,$array2)) self::$bool[]=false; |
||
272 | } |
||
273 | else{ |
||
274 | if(!array_key_exists($array1_key,$array2)) self::$bool[]=false; |
||
275 | |||
276 | if(!isset($array2[$array1_key]) OR !is_array($array2[$array1_key])) $array2[$array1_key]=[]; |
||
277 | |||
278 | if(isset($array1_value[0])) $array1_value=$array1_value[0]; |
||
279 | |||
280 | if(isset($array2[$array1_key][0])) $array2[$array1_key]=$array2[$array1_key][0]; |
||
281 | |||
282 | self::array_diff_key_recursive($array1_value,$array2[$array1_key]); |
||
283 | } |
||
284 | } |
||
285 | |||
286 | if(in_array(false,self::$bool)){ |
||
287 | return false; |
||
288 | } |
||
289 | return true; |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * @param $data |
||
294 | * @return mixed |
||
295 | */ |
||
296 | public static function slashToBackSlash($data) |
||
297 | { |
||
298 | return str_replace("/","\\",$data); |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @param int $debug |
||
303 | * @param null $key |
||
304 | * @return null |
||
305 | */ |
||
306 | public static function trace($debug=0,$key=null) |
||
307 | { |
||
308 | $trace=debug_backtrace(); |
||
309 | |||
310 | if($key===null){ |
||
311 | return $trace[$debug] ?? null; |
||
312 | } |
||
313 | return $trace[$debug][$key] ?? null; |
||
314 | |||
315 | } |
||
316 | |||
317 | /** |
||
318 | * @param $dir |
||
319 | * @param $dirPermissions |
||
320 | * @param $filePermissions |
||
321 | */ |
||
322 | public static function chmod_r($dir, $dirPermissions, $filePermissions) |
||
323 | { |
||
324 | $dp = opendir($dir); |
||
325 | while($file = readdir($dp)) { |
||
326 | if (($file == ".") || ($file == "..")) |
||
327 | continue; |
||
328 | |||
329 | $fullPath = $dir."/".$file; |
||
330 | |||
331 | if(is_dir($fullPath)) { |
||
332 | chmod($fullPath, $dirPermissions); |
||
333 | self::chmod_r($fullPath, $dirPermissions, $filePermissions); |
||
334 | } else { |
||
335 | chmod($fullPath, $filePermissions); |
||
336 | } |
||
337 | |||
338 | } |
||
339 | closedir($dp); |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * @return array |
||
344 | */ |
||
345 | public static function getServiceConf() |
||
346 | { |
||
347 | if(property_exists(core(),'serviceConf') && defined('methodName')){ |
||
348 | return core()->serviceConf; |
||
349 | } |
||
350 | return []; |
||
351 | } |
||
352 | |||
353 | |||
354 | /** |
||
355 | * @param $dir |
||
356 | * @param bool $recursive |
||
357 | * @param string $basedir |
||
358 | * @param bool $include_dirs |
||
359 | * @return array |
||
360 | */ |
||
361 | public static function getAllFilesInDirectory($dir, $recursive = true, $basedir = '', $include_dirs = false) |
||
362 | { |
||
363 | if ($dir == '') {return array();} else {$results = array(); $subresults = array();} |
||
364 | if (!is_dir($dir)) {$dir = dirname($dir);} // so a files path can be sent |
||
365 | if ($basedir == '') {$basedir = realpath($dir).DIRECTORY_SEPARATOR;} |
||
366 | |||
367 | $files = scandir($dir); |
||
368 | foreach ($files as $key => $value){ |
||
369 | if ( ($value != '.') && ($value != '..') ) { |
||
370 | $path = realpath($dir.DIRECTORY_SEPARATOR.$value); |
||
371 | if (is_dir($path)) { |
||
372 | // optionally include directories in file list |
||
373 | if ($include_dirs) {$subresults[] = str_replace($basedir, '', $path);} |
||
374 | // optionally get file list for all subdirectories |
||
375 | if ($recursive) { |
||
376 | $subdirresults = self::getAllFilesInDirectory($path, $recursive, $basedir, $include_dirs); |
||
377 | $results = array_merge($results, $subdirresults); |
||
378 | } |
||
379 | } else { |
||
380 | // strip basedir and add to subarray to separate file list |
||
381 | $subresults[] = str_replace($basedir, '', $path); |
||
382 | } |
||
383 | } |
||
384 | } |
||
385 | // merge the subarray to give the list of files then subdirectory files |
||
386 | if (count($subresults) > 0) {$results = array_merge($subresults, $results);} |
||
387 | return $results; |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * @param $files |
||
392 | * @param null $reelPath |
||
393 | * @return array |
||
394 | */ |
||
395 | public static function getPathWithPhpExtension($files,$reelPath=null) |
||
396 | { |
||
397 | $pathWithPhpList = []; |
||
398 | |||
399 | foreach ($files as $file){ |
||
400 | |||
401 | if(preg_match('@(.*).php@is',$file,$pathWithPhp)){ |
||
402 | |||
403 | if($reelPath===null){ |
||
404 | $pathWithPhpList[] = $pathWithPhp[0]; |
||
405 | } |
||
406 | else{ |
||
407 | $pathWithPhpList[] = $reelPath.'/'.$pathWithPhp[0]; |
||
408 | } |
||
409 | |||
410 | } |
||
411 | } |
||
412 | |||
413 | return $pathWithPhpList; |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * @return array |
||
418 | */ |
||
419 | public static function getHttpMethods() |
||
420 | { |
||
421 | return [ |
||
422 | 'get', |
||
423 | 'head', |
||
424 | 'post', |
||
425 | 'put', |
||
426 | 'delete', |
||
427 | 'connect', |
||
428 | 'options', |
||
429 | 'trace' |
||
430 | ]; |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * @param $class |
||
435 | * @return mixed |
||
436 | */ |
||
437 | public static function resolverClass($class) |
||
438 | { |
||
439 | if(self::isNamespaceExists($class)){ |
||
440 | return app()->resolve($class); |
||
441 | } |
||
442 | |||
443 | return $class; |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * @return array |
||
448 | */ |
||
449 | public static function getRequestPathInfo() |
||
450 | { |
||
451 | if(is_null(BootStaticManager::getRequestPath())){ |
||
452 | return explode("/",request()->getPathInfo()); |
||
453 | } |
||
454 | return BootStaticManager::getRequestPath(); |
||
475 | } |