| Total Complexity | 41 |
| Total Lines | 170 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 1 |
Complex classes like Application 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 Application, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Application extends Container implements ApplicationInterface |
||
| 17 | { |
||
| 18 | use InstantiationsTrait; |
||
| 19 | |||
| 20 | public static ?ApplicationInterface $application = null; |
||
| 21 | |||
| 22 | public function __construct() |
||
| 23 | { |
||
| 24 | } |
||
| 25 | |||
| 26 | public function setServices(array $services): void |
||
| 27 | { |
||
| 28 | if (!$this->has("binding") && !$this->has("services")) { |
||
| 29 | $this->set(["binding", new Container($services["contracts"])]); |
||
| 30 | $this->set(["services", new Container($services["services"])]); |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | public function objects(): ContainerInterface |
||
| 35 | { |
||
| 36 | return $this->instantiate("objects", Objects::class, $this->binding()); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function cookie(): ContainerInterface |
||
| 40 | { |
||
| 41 | return $this->instantiate("cookie", Cookie::class); |
||
| 42 | } |
||
| 43 | |||
| 44 | public function session(): ContainerInterface |
||
| 45 | { |
||
| 46 | return $this->instantiate("session", Session::class); |
||
| 47 | } |
||
| 48 | |||
| 49 | public function binding(): ContainerInterface |
||
| 50 | { |
||
| 51 | return $this->get("binding"); |
||
| 52 | } |
||
| 53 | |||
| 54 | public function services(): ContainerInterface |
||
| 55 | { |
||
| 56 | return $this->get("services"); |
||
| 57 | } |
||
| 58 | |||
| 59 | public function config(): ContainerInterface |
||
| 60 | { |
||
| 61 | return $this->instantiate("config", Container::class); |
||
| 62 | } |
||
| 63 | |||
| 64 | public function request(): RequestInterface |
||
| 67 | } |
||
| 68 | |||
| 69 | public function response(): ResponseInterface |
||
| 70 | { |
||
| 71 | return $this->instantiate("response", Response::class); |
||
| 72 | } |
||
| 73 | |||
| 74 | public static function run(): ApplicationInterface |
||
| 75 | { |
||
| 76 | if (!static::$application instanceof static) { |
||
| 77 | static::$application = new static(); |
||
| 78 | } |
||
| 79 | |||
| 80 | return static::$application; |
||
|
|
|||
| 81 | } |
||
| 82 | |||
| 83 | public function get(string $key = null) |
||
| 84 | { |
||
| 85 | if (isset($key) && !$this->has($key)) { |
||
| 86 | if (!$this->services()->has($key)) { |
||
| 87 | throw new InvalidArgumentException("Service is not installed"); |
||
| 88 | } |
||
| 89 | |||
| 90 | $this->set([$key, $this->services()->get($key)]); |
||
| 91 | } |
||
| 92 | |||
| 93 | return empty($key) ? $this->data : $this->data[$key]; |
||
| 94 | } |
||
| 95 | |||
| 96 | public function set(array $data): void |
||
| 97 | { |
||
| 98 | list($key, $object) = $data; |
||
| 99 | |||
| 100 | if (is_array($object)) { |
||
| 101 | if (array_key_exists(1, $object) && !is_object($object[0])) { |
||
| 102 | $this->iOc($key, $object[0], $object[1]); |
||
| 103 | return; |
||
| 104 | } |
||
| 105 | |||
| 106 | $this->setObject($object[0], $key); |
||
| 107 | return; |
||
| 108 | } |
||
| 109 | |||
| 110 | $this->setObject($object, $key); |
||
| 111 | } |
||
| 112 | |||
| 113 | private function setObject($object, $key): void |
||
| 114 | { |
||
| 115 | (is_object($object)) ? $this->mergeData($key, $object) : $this->iOc($key, $object); |
||
| 116 | } |
||
| 117 | |||
| 118 | private function mergeData(string $key, $object) |
||
| 119 | { |
||
| 120 | $this->data = array_merge([$key => $object], $this->data); |
||
| 121 | } |
||
| 122 | |||
| 123 | private function iOc(string $key, $object, $params = null): void |
||
| 124 | { |
||
| 125 | $reflection = new \ReflectionClass($object); |
||
| 126 | $constructor = $reflection->getConstructor(); |
||
| 127 | |||
| 128 | if ($constructor && $constructor->getNumberOfParameters()) { |
||
| 129 | $paramsIoC = $this->getParamsIoC($constructor, $params); |
||
| 130 | $this->mergeData($key, $reflection->newInstanceArgs($paramsIoC)); |
||
| 131 | return; |
||
| 132 | } |
||
| 133 | |||
| 134 | $this->mergeData($key, new $object()); |
||
| 135 | } |
||
| 136 | |||
| 137 | private function getParamsIoC(\ReflectionMethod $constructor, $params): array |
||
| 169 | } |
||
| 170 | |||
| 171 | /* |
||
| 172 | | Creates an object without adding to the container |
||
| 173 | */ |
||
| 174 | public function new($object, $params = null) |
||
| 175 | { |
||
| 176 | $reflection = new \ReflectionClass($object); |
||
| 177 | $constructor = $reflection->getConstructor(); |
||
| 178 | |||
| 179 | if ($constructor && $constructor->getNumberOfParameters()) { |
||
| 180 | $paramsIoC = $this->getParamsIoC($constructor, $params); |
||
| 181 | |||
| 186 | } |
||
| 187 | } |
||
| 188 |