| Total Complexity | 45 |
| Total Lines | 295 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like DIC 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 DIC, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class DIC |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var string |
||
| 11 | */ |
||
| 12 | private static $filename; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | private static $cacheDir; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @param string $filename |
||
| 21 | * |
||
| 22 | * @throws Exceptions\ParserException |
||
| 23 | */ |
||
| 24 | public static function initFromFile($filename) |
||
| 25 | { |
||
| 26 | self::$filename = $filename; |
||
| 27 | |||
| 28 | // create cache file if does not exists |
||
| 29 | if (false === file_exists($sha1file = self::getCacheFilePath())) { |
||
| 30 | $cachedMap = []; |
||
| 31 | |||
| 32 | foreach (Parser::parse(self::$filename) as $key => $content) { |
||
| 33 | $start = microtime(true); |
||
| 34 | $memoryUsage = memory_get_usage(); |
||
| 35 | |||
| 36 | $cachedMap[$key] = [ |
||
| 37 | 'value' => self::setInCache($cachedMap, $content), |
||
| 38 | '@metadata' => [ |
||
| 39 | 'type' => gettype(self::setInCache($cachedMap, $content)), |
||
| 40 | 'create_time' => self::calculateCreatingTime($start), |
||
| 41 | 'memory_usage' => (memory_get_usage() - $memoryUsage), |
||
| 42 | ], |
||
| 43 | ]; |
||
| 44 | } |
||
| 45 | |||
| 46 | if (false === is_dir(self::getCacheDir())) { |
||
| 47 | mkdir(self::getCacheDir(), 0755, true); |
||
| 48 | } |
||
| 49 | |||
| 50 | file_put_contents($sha1file, '<?php return unserialize(\''. serialize($cachedMap) .'\');' . PHP_EOL); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param float $start |
||
| 56 | * |
||
| 57 | * @return float |
||
| 58 | */ |
||
| 59 | private static function calculateCreatingTime($start) |
||
| 60 | { |
||
| 61 | $stringval = microtime(true) - $start; |
||
| 62 | $numericval = sscanf((string)$stringval, "%f")[0]; |
||
| 63 | $seconds = number_format($numericval, 8); |
||
| 64 | |||
| 65 | return (float)$seconds * 1000000; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param string $cacheDir |
||
| 70 | */ |
||
| 71 | public static function setCacheDir($cacheDir) |
||
| 72 | { |
||
| 73 | self::$cacheDir = $cacheDir; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | private static function getCacheDir() |
||
| 80 | { |
||
| 81 | return (self::$cacheDir) ? self::$cacheDir : __DIR__.'/../_cache/'; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | private static function getCacheFilePath() |
||
| 88 | { |
||
| 89 | return self::getCacheDir() . DIRECTORY_SEPARATOR . sha1_file(self::$filename) .'.php'; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return mixed |
||
| 94 | */ |
||
| 95 | private static function getCache() |
||
| 96 | { |
||
| 97 | return include(self::getCacheFilePath()); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return int |
||
| 102 | */ |
||
| 103 | public static function count() |
||
| 104 | { |
||
| 105 | return count(self::getCache()); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param string $id |
||
| 110 | * |
||
| 111 | * @return mixed |
||
| 112 | */ |
||
| 113 | public static function get($id) |
||
| 114 | { |
||
| 115 | if (self::has($id)) { |
||
| 116 | return self::getCache()[$id]['value']; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param string $id |
||
| 122 | * |
||
| 123 | * @return mixed |
||
| 124 | */ |
||
| 125 | public static function getMetadata($id) |
||
| 126 | { |
||
| 127 | if (self::has($id)) { |
||
| 128 | return self::getCache()[$id]['@metadata']; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param string $id |
||
| 134 | * |
||
| 135 | * @return bool |
||
| 136 | */ |
||
| 137 | public static function has($id) |
||
| 138 | { |
||
| 139 | if (false === file_exists($sha1file = self::getCacheFilePath())) { |
||
| 140 | return false; |
||
| 141 | } |
||
| 142 | |||
| 143 | return isset(self::getCache()[$id]); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return array |
||
| 148 | */ |
||
| 149 | public static function keys() |
||
| 150 | { |
||
| 151 | return array_keys(self::getCache()); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Set an entry in the container. |
||
| 156 | * |
||
| 157 | * @param array $cachedMap |
||
| 158 | * @param mixed $content |
||
| 159 | * |
||
| 160 | * @return mixed|bool|null |
||
| 161 | */ |
||
| 162 | private static function setInCache($cachedMap, $content) |
||
| 163 | { |
||
| 164 | // if is not a class set the entry value in DIC |
||
| 165 | if (false === isset($content['class'])) { |
||
| 166 | return self::getFromEnvOrDICParams($content); |
||
| 167 | } |
||
| 168 | |||
| 169 | // otherwise it's a class, so extract variables |
||
| 170 | $class = $content['class']; |
||
| 171 | $classArguments = isset($content['arguments']) ? $content['arguments'] : null; |
||
| 172 | $method = isset($content['method']) ? $content['method'] : null; |
||
| 173 | $methodArguments = isset($content['method_arguments']) ? $content['method_arguments'] : null; |
||
| 174 | |||
| 175 | $methodArgsToInject = self::getArgumentsToInject($cachedMap, $methodArguments); |
||
| 176 | $classArgsToInject = self::getArgumentsToInject($cachedMap, $classArguments); |
||
| 177 | |||
| 178 | try { |
||
| 179 | return self::instantiateTheClass($class, $classArgsToInject, $method, $methodArgsToInject); |
||
| 180 | } catch (\Error $error) { |
||
| 181 | return false; |
||
| 182 | } catch (\Exception $exception) { |
||
| 183 | return false; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param string $class |
||
| 189 | * @param array $classArguments |
||
| 190 | * @param null $method |
||
|
|
|||
| 191 | * @param array $methodArguments |
||
| 192 | * |
||
| 193 | * @return mixed|bool |
||
| 194 | * |
||
| 195 | * @throws \ReflectionException |
||
| 196 | */ |
||
| 197 | private static function instantiateTheClass($class, array $classArguments = [], $method = null, array $methodArguments = []) |
||
| 198 | { |
||
| 199 | if (false === class_exists($class)) { |
||
| 200 | return false; |
||
| 201 | } |
||
| 202 | |||
| 203 | $reflected = new \ReflectionClass($class); |
||
| 204 | |||
| 205 | // 1. the class has no method to call |
||
| 206 | if (null == $method) { |
||
| 207 | return new $class(...$classArguments); |
||
| 208 | } |
||
| 209 | |||
| 210 | if (false === $reflected->hasMethod($method)) { |
||
| 211 | return false; |
||
| 212 | } |
||
| 213 | |||
| 214 | // 2. the method to call is static |
||
| 215 | if ($reflected->getMethod($method)->isStatic()) { |
||
| 216 | return $class::$method(...$methodArguments); |
||
| 217 | } |
||
| 218 | |||
| 219 | // 3. the class has a private constructor |
||
| 220 | if ($reflected->hasMethod('__construct') and $reflected->getConstructor()->isPrivate()) { |
||
| 221 | return call_user_func_array([$class, $method], $methodArguments); |
||
| 222 | } |
||
| 223 | |||
| 224 | // 4. the class has a public constructor |
||
| 225 | return (new $class(...$classArguments))->$method(...$methodArguments); |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get the arguments to inject into the class to instantiate within DIC. |
||
| 230 | * |
||
| 231 | * @param array $cachedMap |
||
| 232 | * @param null $providedArguments |
||
| 233 | * |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | private static function getArgumentsToInject(array $cachedMap = [], $providedArguments = null) |
||
| 237 | { |
||
| 238 | $returnArguments = []; |
||
| 239 | |||
| 240 | if (null != $providedArguments) { |
||
| 241 | foreach ($providedArguments as $argument) { |
||
| 242 | $returnArguments[] = self::getArgumentToInject($cachedMap, $argument); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | return $returnArguments; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param array $cachedMap |
||
| 251 | * @param string $argument |
||
| 252 | * |
||
| 253 | * @return mixed|string|null |
||
| 254 | */ |
||
| 255 | private static function getArgumentToInject(array $cachedMap = [], $argument) |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param string $parameter |
||
| 264 | * |
||
| 265 | * @return mixed|string|null |
||
| 266 | */ |
||
| 267 | private static function getFromEnvOrDICParams($parameter) |
||
| 268 | { |
||
| 269 | if (is_string($parameter)) { |
||
| 270 | if (null !== self::getEnvKey($parameter)) { |
||
| 271 | return (getenv(self::getEnvKey($parameter))) ? getenv(self::getEnvKey($parameter)) : $parameter; |
||
| 272 | } |
||
| 273 | |||
| 274 | return (DICParams::has(self::getParamKey($parameter))) ? DICParams::get(self::getParamKey($parameter)) : $parameter; |
||
| 275 | } |
||
| 276 | |||
| 277 | return $parameter; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Extract from a string like %env(FOO)% |
||
| 282 | * |
||
| 283 | * @param string $string |
||
| 284 | * |
||
| 285 | * @return mixed|null |
||
| 286 | */ |
||
| 287 | private static function getEnvKey($string) |
||
| 288 | { |
||
| 289 | preg_match('~%env\((.*?)\)%~', $string, $matches); |
||
| 290 | |||
| 291 | return (count($matches) > 0) ? $matches[1] : null; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param string $string |
||
| 296 | * |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | private static function getParamKey($string) |
||
| 302 | } |
||
| 303 | } |
||
| 304 |