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