| Total Complexity | 42 |
| Total Lines | 265 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| 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 |
||
| 8 | class DIC |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var Container |
||
| 12 | */ |
||
| 13 | private static $container; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * DIC constructor. |
||
| 17 | * |
||
| 18 | * @param array $config |
||
| 19 | */ |
||
| 20 | private function __construct(array $config = []) |
||
| 21 | { |
||
| 22 | self::$container = new Container(); |
||
| 23 | |||
| 24 | $this->resolveDependencies($config); |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @return int |
||
| 29 | */ |
||
| 30 | public static function count() |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Initialise the DIC |
||
| 37 | * |
||
| 38 | * @param array $config |
||
| 39 | * |
||
| 40 | * @return DIC |
||
| 41 | */ |
||
| 42 | public static function init(array $config = []) |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param string $filename |
||
| 49 | * |
||
| 50 | * @return DIC |
||
| 51 | * @throws \Exception |
||
| 52 | */ |
||
| 53 | public static function initFromFile($filename) |
||
| 54 | { |
||
| 55 | return new self(Parser::parse($filename)); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @return array |
||
| 60 | */ |
||
| 61 | public static function keys() |
||
| 62 | { |
||
| 63 | return self::$container->keys(); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Resolve the dependencies and register into the DIC |
||
| 68 | * |
||
| 69 | * @param array $config |
||
| 70 | */ |
||
| 71 | private function resolveDependencies(array $config = []) |
||
| 72 | { |
||
| 73 | foreach ($config as $key => $content) { |
||
| 74 | self::set($key, $content); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * |
||
| 80 | * Check for an entry existence within the container |
||
| 81 | * |
||
| 82 | * @param string $dependency |
||
| 83 | * |
||
| 84 | * @return bool |
||
| 85 | */ |
||
| 86 | public static function has($dependency) |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get an entry from the container. |
||
| 93 | * |
||
| 94 | * The method returns: |
||
| 95 | * - false if the entry has a wrong configuration |
||
| 96 | * - NULL if the entry does not exists |
||
| 97 | * |
||
| 98 | * @param string $dependency |
||
| 99 | * |
||
| 100 | * @return mixed |
||
| 101 | */ |
||
| 102 | public static function get($dependency) |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Set an entry in the container. |
||
| 111 | * |
||
| 112 | * @param string $key |
||
| 113 | * @param array|mixed $content |
||
| 114 | * |
||
| 115 | * @return mixed|bool|null |
||
| 116 | */ |
||
| 117 | private static function set($key, $content) |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param string $class |
||
| 177 | * @param string $method |
||
| 178 | * |
||
| 179 | * @return bool|mixed |
||
| 180 | * @throws \ReflectionException |
||
| 181 | */ |
||
| 182 | private static function callClassMethod($class, $method) |
||
| 183 | { |
||
| 184 | $reflected = new \ReflectionClass($class); |
||
| 185 | |||
| 186 | if (false === $reflected->hasMethod($method)) { |
||
| 187 | return false; |
||
| 188 | } |
||
| 189 | |||
| 190 | if ($reflected->getMethod($method)->isStatic()) { |
||
| 191 | return call_user_func([$class, $method]); |
||
| 192 | } |
||
| 193 | |||
| 194 | return (new $class)->$method(); |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Get the arguments to inject into the class to instantiate within DIC. |
||
| 199 | * |
||
| 200 | * @param Container $c |
||
| 201 | * @param null $providedArguments |
||
|
|
|||
| 202 | * |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | private static function getArgumentsToInject(Container $c, $providedArguments = null) |
||
| 206 | { |
||
| 207 | $returnArguments = []; |
||
| 208 | |||
| 209 | if (null != $providedArguments) { |
||
| 210 | foreach ($providedArguments as $argument) { |
||
| 211 | $returnArguments[] = self::getArgumentToInject($argument, $c); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | return $returnArguments; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param string $argument |
||
| 220 | * @param Container $c |
||
| 221 | * |
||
| 222 | * @return mixed|string|null |
||
| 223 | */ |
||
| 224 | private static function getArgumentToInject($argument, Container $c) |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param string $parameter |
||
| 235 | * |
||
| 236 | * @return mixed|string|null |
||
| 237 | */ |
||
| 238 | private static function getFromEnvOrDICParams($parameter) |
||
| 239 | { |
||
| 240 | if (is_string($parameter)) { |
||
| 241 | if (null !== self::getEnvKey($parameter)) { |
||
| 242 | return (getenv(self::getEnvKey($parameter))) ? getenv(self::getEnvKey($parameter)) : $parameter; |
||
| 243 | } |
||
| 244 | |||
| 245 | return (DICParams::has(self::getParamKey($parameter))) ? DICParams::get(self::getParamKey($parameter)) : $parameter; |
||
| 246 | } |
||
| 247 | |||
| 248 | return $parameter; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Extract from a string like %env(FOO)% |
||
| 253 | * |
||
| 254 | * @param string $string |
||
| 255 | * |
||
| 256 | * @return mixed|null |
||
| 257 | */ |
||
| 258 | private static function getEnvKey($string) |
||
| 259 | { |
||
| 260 | preg_match('~%env\((.*?)\)%~', $string, $matches); |
||
| 261 | |||
| 262 | return (count($matches) > 0) ? $matches[1] : null; |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param string $string |
||
| 267 | * |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | private static function getParamKey($string) |
||
| 273 | } |
||
| 274 | } |
||
| 275 |