Total Complexity | 60 |
Total Lines | 369 |
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 |
||
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) |
||
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) |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @param $namespace |
||
162 | * @param string $seperator |
||
163 | * @return mixed |
||
164 | */ |
||
165 | public static function getJustClassName($namespace,$seperator="\\") |
||
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) |
||
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) |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @param $array1 |
||
238 | * @param $array2 |
||
239 | * @return bool |
||
240 | */ |
||
241 | public static function array_diff_key_recursive ($array1, $array2) |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * @param $data |
||
271 | * @return mixed |
||
272 | */ |
||
273 | public static function slashToBackSlash($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) |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * @return array |
||
298 | */ |
||
299 | public static function getServiceConf() |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * @param $files |
||
309 | * @param null|string $reelPath |
||
310 | * @return array |
||
311 | */ |
||
312 | public static function getPathWithPhpExtension($files,$reelPath=null) |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * @param $class |
||
335 | * @return mixed |
||
336 | */ |
||
337 | public static function resolverClass($class) |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * @return array |
||
348 | */ |
||
349 | public static function getRequestPathInfo() |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * @param $trace |
||
359 | * @param null|string $remove |
||
360 | * @return array |
||
361 | */ |
||
362 | public static function removeTrace($trace,$remove=null) |
||
374 | } |
||
375 | } |