| Total Complexity | 72 |
| Total Lines | 423 |
| 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() |
||
| 300 | {
|
||
| 301 | if(property_exists(core(),'serviceConf') && defined('methodName')){
|
||
| 302 | return core()->serviceConf; |
||
| 303 | } |
||
| 304 | return []; |
||
| 305 | } |
||
| 306 | |||
| 307 | |||
| 308 | /** |
||
| 309 | * @param $dir |
||
| 310 | * @param bool $recursive |
||
| 311 | * @param string $basedir |
||
| 312 | * @param bool $include_dirs |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | public static function getAllFilesInDirectory($dir, $recursive = true, $basedir = '', $include_dirs = false) |
||
| 316 | {
|
||
| 317 | if ($dir == '') {return array();} else {$results = array(); $subresults = array();}
|
||
| 318 | if (!is_dir($dir)) {$dir = dirname($dir);} // so a files path can be sent
|
||
| 319 | if ($basedir == '') {$basedir = realpath($dir).DIRECTORY_SEPARATOR;}
|
||
| 320 | |||
| 321 | $files = scandir($dir); |
||
| 322 | foreach ($files as $key => $value){
|
||
| 323 | if ( ($value != '.') && ($value != '..') ) {
|
||
| 324 | $path = realpath($dir.DIRECTORY_SEPARATOR.$value); |
||
| 325 | if (is_dir($path)) {
|
||
| 326 | // optionally include directories in file list |
||
| 327 | if ($include_dirs) {$subresults[] = str_replace($basedir, '', $path);}
|
||
| 328 | // optionally get file list for all subdirectories |
||
| 329 | if ($recursive) {
|
||
| 330 | $subdirresults = self::getAllFilesInDirectory($path, $recursive, $basedir, $include_dirs); |
||
| 331 | $results = array_merge($results, $subdirresults); |
||
| 332 | } |
||
| 333 | } else {
|
||
| 334 | // strip basedir and add to subarray to separate file list |
||
| 335 | $subresults[] = str_replace($basedir, '', $path); |
||
| 336 | } |
||
| 337 | } |
||
| 338 | } |
||
| 339 | // merge the subarray to give the list of files then subdirectory files |
||
| 340 | if (count($subresults) > 0) {$results = array_merge($subresults, $results);}
|
||
| 341 | return $results; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param $files |
||
| 346 | * @param null|string $reelPath |
||
| 347 | * @return array |
||
| 348 | */ |
||
| 349 | public static function getPathWithPhpExtension($files,$reelPath=null) |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @return array |
||
| 372 | */ |
||
| 373 | public static function getHttpMethods() |
||
| 384 | ]; |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @param $class |
||
| 389 | * @return mixed |
||
| 390 | */ |
||
| 391 | public static function resolverClass($class) |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @return array |
||
| 402 | */ |
||
| 403 | public static function getRequestPathInfo() |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param $trace |
||
| 413 | * @param null|string $remove |
||
| 414 | * @return array |
||
| 415 | */ |
||
| 416 | public static function removeTrace($trace,$remove=null) |
||
| 428 | } |
||
| 429 | } |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.