| Total Complexity | 44 |
| Total Lines | 170 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 1 |
Complex classes like Rudra 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 Rudra, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Rudra extends AbstractRudra implements RudraInterface, ContainerInterface |
||
| 20 | { |
||
| 21 | use InstantiationsTrait; |
||
| 22 | use PublicApplicationTrait; |
||
|
|
|||
| 23 | |||
| 24 | public static ?AbstractRudra $rudra = null; |
||
| 25 | private array $data = []; |
||
| 26 | |||
| 27 | protected function _setServices(array $services): void |
||
| 28 | { |
||
| 29 | ($this->_has("binding")) ?: $this->_set(["binding", new Container($services["contracts"])]); |
||
| 30 | ($this->_has("services")) ?: $this->_set(["services", new Container($services["services"])]); |
||
| 31 | ($this->_has("config")) ?: $this->_set(["config", new Container($services["config"])]); |
||
| 32 | } |
||
| 33 | |||
| 34 | protected function _binding(): ContainerInterface |
||
| 35 | { |
||
| 36 | if ($this->_has("binding")) return $this->_get("binding"); |
||
| 37 | throw new \InvalidArgumentException("Service not preinstalled"); |
||
| 38 | } |
||
| 39 | |||
| 40 | protected function _services(): ContainerInterface |
||
| 41 | { |
||
| 42 | if ($this->_has("services")) return $this->_get("services"); |
||
| 43 | throw new \InvalidArgumentException("Service not preinstalled"); |
||
| 44 | } |
||
| 45 | |||
| 46 | protected function _config(): ContainerInterface |
||
| 47 | { |
||
| 48 | if ($this->_has("config")) return $this->_get("config"); |
||
| 49 | throw new \InvalidArgumentException("Service not preinstalled"); |
||
| 50 | } |
||
| 51 | |||
| 52 | protected function _request(): AbstractRequest |
||
| 53 | { |
||
| 54 | return $this->containerize(Request::class); |
||
| 55 | } |
||
| 56 | |||
| 57 | protected function _response(): AbstractResponse |
||
| 58 | { |
||
| 59 | return $this->containerize(Response::class); |
||
| 60 | } |
||
| 61 | |||
| 62 | protected function _cookie(): Cookie |
||
| 63 | { |
||
| 64 | return $this->containerize(Cookie::class); |
||
| 65 | } |
||
| 66 | |||
| 67 | protected function _session(): Session |
||
| 68 | { |
||
| 69 | return $this->containerize(Session::class); |
||
| 70 | } |
||
| 71 | |||
| 72 | /* |
||
| 73 | | Creates an object without adding to the container |
||
| 74 | */ |
||
| 75 | protected function _new($object, $params = null) |
||
| 76 | { |
||
| 77 | $reflection = new \ReflectionClass($object); |
||
| 78 | $constructor = $reflection->getConstructor(); |
||
| 79 | |||
| 80 | if ($constructor && $constructor->getNumberOfParameters()) { |
||
| 81 | $paramsIoC = $this->getParamsIoC($constructor, $params); |
||
| 82 | |||
| 83 | return $reflection->newInstanceArgs($paramsIoC); |
||
| 84 | } |
||
| 85 | |||
| 86 | return new $object(); |
||
| 87 | } |
||
| 88 | |||
| 89 | public static function run(): AbstractRudra |
||
| 96 | } |
||
| 97 | |||
| 98 | protected function _get(string $key = null) |
||
| 99 | { |
||
| 100 | if (isset($key) && !$this->_has($key)) { |
||
| 101 | if (!$this->_services()->has($key)) { |
||
| 102 | throw new \InvalidArgumentException("Service is not installed"); |
||
| 103 | } |
||
| 104 | |||
| 105 | $this->_set([$key, $this->_services()->get($key)]); |
||
| 106 | } |
||
| 107 | |||
| 108 | return empty($key) ? $this->data : $this->data[$key]; |
||
| 109 | } |
||
| 110 | |||
| 111 | protected function _set(array $data): void |
||
| 112 | { |
||
| 113 | list($key, $object) = $data; |
||
| 114 | |||
| 115 | if (is_array($object)) { |
||
| 116 | if (array_key_exists(1, $object) && !is_object($object[0])) { |
||
| 117 | $this->iOc($key, $object[0], $object[1]); |
||
| 118 | return; |
||
| 119 | } |
||
| 120 | |||
| 121 | $this->setObject($object[0], $key); |
||
| 122 | return; |
||
| 123 | } |
||
| 124 | |||
| 125 | $this->setObject($object, $key); |
||
| 126 | } |
||
| 127 | |||
| 128 | protected function _has(string $key): bool |
||
| 129 | { |
||
| 130 | return array_key_exists($key, $this->data); |
||
| 131 | } |
||
| 132 | |||
| 133 | private function setObject($object, $key): void |
||
| 134 | { |
||
| 135 | (is_object($object)) ? $this->mergeData($key, $object) : $this->iOc($key, $object); |
||
| 136 | } |
||
| 137 | |||
| 138 | private function mergeData(string $key, $object) |
||
| 141 | } |
||
| 142 | |||
| 143 | private function iOc(string $key, $object, $params = null): void |
||
| 144 | { |
||
| 145 | $reflection = new \ReflectionClass($object); |
||
| 155 | } |
||
| 156 | |||
| 157 | private function getParamsIoC(\ReflectionMethod $constructor, $params): array |
||
| 189 | } |
||
| 190 | } |
||
| 191 |