| Total Complexity | 53 |
| Total Lines | 206 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Complex classes like PreloaderInternalTrait 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 PreloaderInternalTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | trait PreloaderInternalTrait { |
||
| 14 | private $vendorDir; |
||
| 15 | private static $libraries = [ |
||
| 16 | 'application' => './../app/', |
||
| 17 | 'ubiquity' => 'phpmv/ubiquity/src/Ubiquity/', |
||
| 18 | 'ubiquity-dev' => 'phpmv/ubiquity-dev/src/Ubiquity/', |
||
| 19 | 'ubiquity-webtools' => 'phpmv/ubiquity-webtools/src/Ubiquity/', |
||
| 20 | 'ubiquity-mailer' => 'phpmv/ubiquity-mailer/src/Ubiquity/', |
||
| 21 | 'ubiquity-swoole' => 'phpmv/ubiquity-swoole/src/Ubiquity/', |
||
| 22 | 'ubiquity-workerman' => 'phpmv/ubiquity-workerman/src/Ubiquity/', |
||
| 23 | 'ubiquity-tarantool' => 'phpmv/ubiquity-tarantool/src/Ubiquity/', |
||
| 24 | 'ubiquity-mysqli' => 'phpmv/ubiquity-mysqli/src/Ubiquity/', |
||
| 25 | 'phpmv-ui' => 'phpmv/php-mv-ui/Ajax/' ]; |
||
| 26 | private $excludeds = [ ]; |
||
| 27 | private static $count = 0; |
||
| 28 | private $classes = [ ]; |
||
| 29 | private $loader; |
||
| 30 | |||
| 31 | private function addClassFile($class, $file) { |
||
| 32 | if (! isset ( $this->classes [$class] )) { |
||
| 33 | $this->classes [$class] = $file; |
||
| 34 | } |
||
| 35 | } |
||
| 36 | |||
| 37 | private function loadClass($class, $file = null) { |
||
| 38 | if (! \class_exists ( $class, false )) { |
||
| 39 | $file = $file ?? $this->getPathFromClass ( $class ); |
||
| 40 | if (isset ( $file )) { |
||
| 41 | $this->loadFile ( $file ); |
||
| 42 | } |
||
| 43 | } |
||
| 44 | if (\class_exists ( $class, false )) { |
||
| 45 | echo "$class loaded !\n"; |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | private function getPathFromClass(string $class): ?string { |
||
| 50 | $classPath = $this->loader->findFile ( $class ); |
||
| 51 | if (false !== $classPath) { |
||
| 52 | return \realpath ( $classPath ); |
||
| 53 | } |
||
| 54 | return null; |
||
| 55 | } |
||
| 56 | |||
| 57 | private function loadFile(string $file): void { |
||
| 58 | require_once ($file); |
||
| 59 | self::$count ++; |
||
| 60 | } |
||
| 61 | |||
| 62 | private function isExcluded(string $name): bool { |
||
| 63 | foreach ( $this->excludeds as $excluded ) { |
||
| 64 | if (\strpos ( $name, $excluded ) === 0) { |
||
| 65 | return true; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | return false; |
||
| 69 | } |
||
| 70 | |||
| 71 | private function glob_recursive($pattern, $flags = 0) { |
||
| 72 | $files = \glob ( $pattern, $flags ); |
||
| 73 | foreach ( \glob ( \dirname ( $pattern ) . '/*', GLOB_ONLYDIR | GLOB_NOSORT ) as $dir ) { |
||
| 74 | $files = \array_merge ( $files, $this->glob_recursive ( $dir . '/' . \basename ( $pattern ), $flags ) ); |
||
|
1 ignored issue
–
show
|
|||
| 75 | } |
||
| 76 | return $files; |
||
| 77 | } |
||
| 78 | |||
| 79 | private function getClassFullNameFromFile($filePathName, $backSlash = false) { |
||
| 80 | $phpCode = \file_get_contents ( $filePathName ); |
||
| 81 | $class = $this->getClassNameFromPhpCode ( $phpCode ); |
||
| 82 | if (isset ( $class )) { |
||
| 83 | $ns = $this->getClassNamespaceFromPhpCode ( $phpCode ); |
||
| 84 | if ($backSlash && $ns != null) { |
||
|
1 ignored issue
–
show
|
|||
| 85 | $ns = "\\" . $ns; |
||
| 86 | } |
||
| 87 | return $ns . '\\' . $class; |
||
| 88 | } |
||
| 89 | return null; |
||
| 90 | } |
||
| 91 | |||
| 92 | private function getClassNamespaceFromPhpCode($phpCode) { |
||
| 93 | $tokens = \token_get_all ( $phpCode ); |
||
| 94 | $count = \count ( $tokens ); |
||
| 95 | $i = 0; |
||
| 96 | $namespace = ''; |
||
| 97 | $namespace_ok = false; |
||
| 98 | while ( $i < $count ) { |
||
| 99 | $token = $tokens [$i]; |
||
| 100 | if (\is_array ( $token ) && $token [0] === T_NAMESPACE) { |
||
| 101 | // Found namespace declaration |
||
| 102 | while ( ++ $i < $count ) { |
||
| 103 | if ($tokens [$i] === ';') { |
||
| 104 | $namespace_ok = true; |
||
| 105 | $namespace = \trim ( $namespace ); |
||
| 106 | break; |
||
| 107 | } |
||
| 108 | $namespace .= \is_array ( $tokens [$i] ) ? $tokens [$i] [1] : $tokens [$i]; |
||
| 109 | } |
||
| 110 | break; |
||
| 111 | } |
||
| 112 | $i ++; |
||
| 113 | } |
||
| 114 | if (! $namespace_ok) { |
||
| 115 | return null; |
||
| 116 | } |
||
| 117 | return $namespace; |
||
| 118 | } |
||
| 119 | |||
| 120 | private function getClassNameFromPhpCode($phpCode) { |
||
| 121 | $classes = array (); |
||
| 122 | $tokens = \token_get_all ( $phpCode ); |
||
| 123 | $count = count ( $tokens ); |
||
| 124 | for($i = 2; $i < $count; $i ++) { |
||
| 125 | $elm = $tokens [$i - 2] [0]; |
||
| 126 | if ($elm == T_CLASS && $tokens [$i - 1] [0] == T_WHITESPACE && $tokens [$i] [0] == T_STRING) { |
||
| 127 | $class_name = $tokens [$i] [1]; |
||
| 128 | $classes [] = $class_name; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | if (isset ( $classes [0] )) |
||
| 132 | return $classes [0]; |
||
| 133 | return null; |
||
| 134 | } |
||
| 135 | |||
| 136 | private function asPhpArray($array, $prefix = "", $depth = 1, $format = false) { |
||
| 137 | $exts = array (); |
||
| 138 | $extsStr = ""; |
||
| 139 | $tab = ""; |
||
| 140 | $nl = ""; |
||
| 141 | if ($format) { |
||
| 142 | $tab = \str_repeat ( "\t", $depth ); |
||
| 143 | $nl = PHP_EOL; |
||
| 144 | } |
||
| 145 | foreach ( $array as $k => $v ) { |
||
| 146 | if (\is_string ( $k )) { |
||
| 147 | $exts [] = "\"" . $this->doubleBackSlashes ( $k ) . "\"=>" . $this->parseValue ( $v, 'array', $depth + 1, $format ); |
||
| 148 | } else { |
||
| 149 | $exts [] = $this->parseValue ( $v, $prefix, $depth + 1, $format ); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | if (\sizeof ( $exts ) > 0 || $prefix !== "") { |
||
| 153 | $extsStr = "(" . \implode ( "," . $nl . $tab, $exts ) . ")"; |
||
| 154 | if (\sizeof ( $exts ) > 0) { |
||
| 155 | $extsStr = "(" . $nl . $tab . \implode ( "," . $nl . $tab, $exts ) . $nl . $tab . ")"; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | return $prefix . $extsStr; |
||
| 159 | } |
||
| 160 | |||
| 161 | private function parseValue($v, $prefix = "", $depth = 1, $format = false) { |
||
| 171 | } |
||
| 172 | |||
| 173 | private function closure_dump(\Closure $c) { |
||
| 174 | $str = 'function ('; |
||
| 175 | $r = new \ReflectionFunction ( $c ); |
||
| 176 | $params = array (); |
||
| 177 | foreach ( $r->getParameters () as $p ) { |
||
| 178 | $s = ''; |
||
| 179 | if ($p->isArray ()) { |
||
| 180 | $s .= 'array '; |
||
| 181 | } else if ($p->getClass ()) { |
||
| 182 | $s .= $p->getClass ()->name . ' '; |
||
| 183 | } |
||
| 184 | if ($p->isPassedByReference ()) { |
||
| 185 | $s .= '&'; |
||
| 186 | } |
||
| 187 | $s .= '$' . $p->name; |
||
| 188 | if ($p->isOptional ()) { |
||
| 189 | $s .= ' = ' . \var_export ( $p->getDefaultValue (), TRUE ); |
||
| 190 | } |
||
| 191 | $params [] = $s; |
||
| 192 | } |
||
| 193 | $str .= \implode ( ', ', $params ); |
||
| 194 | $str .= ')'; |
||
| 195 | $lines = file ( $r->getFileName () ); |
||
| 196 | $sLine = $r->getStartLine (); |
||
| 197 | $eLine = $r->getEndLine (); |
||
| 198 | if ($eLine === $sLine) { |
||
| 199 | $match = \strstr ( $lines [$sLine - 1], "function" ); |
||
| 200 | $str .= \strstr ( \strstr ( $match, "{" ), "}", true ) . "}"; |
||
| 201 | } else { |
||
| 202 | $str .= \strrchr ( $lines [$sLine - 1], "{" ); |
||
| 203 | for($l = $sLine; $l < $eLine - 1; $l ++) { |
||
| 204 | $str .= $lines [$l]; |
||
| 205 | } |
||
| 206 | $str .= \strstr ( $lines [$eLine - 1], "}", true ) . "}"; |
||
| 207 | } |
||
| 208 | $vars = $r->getStaticVariables (); |
||
| 209 | foreach ( $vars as $k => $v ) { |
||
| 210 | $str = \str_replace ( '$' . $k, \var_export ( $v, true ), $str ); |
||
| 211 | } |
||
| 212 | return $str; |
||
| 213 | } |
||
| 214 | |||
| 215 | private function doubleBackSlashes($value) { |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 |