| Total Complexity | 43 |
| Total Lines | 176 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| Bugs | 0 | Features | 3 |
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 |
||
| 18 | class Rudra implements RudraInterface, ContainerInterface |
||
| 19 | { |
||
| 20 | use InstantiationsTrait; |
||
| 21 | |||
| 22 | public static ?RudraInterface $rudra = null; |
||
| 23 | private array $data = []; |
||
| 24 | |||
| 25 | public function binding(array $contracts = []): ContainerInterface |
||
| 26 | { |
||
| 27 | if (!$this->has("binding")) $this->set(["binding", new Container($contracts)]); |
||
| 28 | |||
| 29 | return $this->get("binding"); |
||
| 30 | } |
||
| 31 | |||
| 32 | public function services(array $services = []): ContainerInterface |
||
| 33 | { |
||
| 34 | if (!$this->has("services")) $this->set(["services", new Container($services)]); |
||
| 35 | |||
| 36 | return $this->get("services"); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function config(array $config = []): ContainerInterface |
||
| 40 | { |
||
| 41 | if (!$this->has("config")) $this->set(["config", new Container($config)]); |
||
| 42 | |||
| 43 | return $this->get("config"); |
||
| 44 | } |
||
| 45 | |||
| 46 | public function data(array $data = []): ContainerInterface |
||
| 47 | { |
||
| 48 | if (!$this->has("data")) $this->set(["data", new Container($data)]); |
||
| 49 | |||
| 50 | return $this->get("data"); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function request(): RequestInterface |
||
| 54 | { |
||
| 55 | return $this->containerize(Request::class); |
||
| 56 | } |
||
| 57 | |||
| 58 | public function response(): ResponseInterface |
||
| 59 | { |
||
| 60 | return $this->containerize(Response::class); |
||
| 61 | } |
||
| 62 | |||
| 63 | public function cookie(): Cookie |
||
| 64 | { |
||
| 65 | return $this->containerize(Cookie::class); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function session(): Session |
||
| 69 | { |
||
| 70 | return $this->containerize(Session::class); |
||
| 71 | } |
||
| 72 | |||
| 73 | /* |
||
| 74 | | Creates an object without adding to the container |
||
| 75 | */ |
||
| 76 | public function new($object, $params = null) |
||
| 77 | { |
||
| 78 | $reflection = new \ReflectionClass($object); |
||
| 79 | $constructor = $reflection->getConstructor(); |
||
| 80 | |||
| 81 | if ($constructor && $constructor->getNumberOfParameters()) { |
||
| 82 | $paramsIoC = $this->getParamsIoC($constructor, $params); |
||
| 83 | |||
| 84 | return $reflection->newInstanceArgs($paramsIoC); |
||
| 85 | } |
||
| 86 | |||
| 87 | return new $object(); |
||
| 88 | } |
||
| 89 | |||
| 90 | public static function run(): RudraInterface |
||
| 97 | } |
||
| 98 | |||
| 99 | public function get(string $key = null) |
||
| 100 | { |
||
| 101 | if (isset($key) && !$this->has($key)) { |
||
| 102 | if (!$this->services()->has($key)) { |
||
| 103 | throw new \InvalidArgumentException("Service '$key' is not installed"); |
||
| 104 | } |
||
| 105 | |||
| 106 | $this->set([$key, $this->services()->get($key)]); |
||
| 107 | } |
||
| 108 | |||
| 109 | if (empty($key)) { |
||
| 110 | return $this->data; |
||
| 111 | } |
||
| 112 | |||
| 113 | return ($this->data[$key] instanceof \Closure) ? $this->data[$key]() : $this->data[$key]; |
||
| 114 | } |
||
| 115 | |||
| 116 | public function set(array $data): void |
||
| 117 | { |
||
| 118 | list($key, $object) = $data; |
||
| 119 | |||
| 120 | if (is_array($object)) { |
||
| 121 | if (array_key_exists(1, $object) && !is_object($object[0])) { |
||
| 122 | $this->iOc($key, $object[0], $object[1]); |
||
| 123 | return; |
||
| 124 | } |
||
| 125 | |||
| 126 | $this->setObject($object[0], $key); |
||
| 127 | return; |
||
| 128 | } |
||
| 129 | |||
| 130 | $this->setObject($object, $key); |
||
| 131 | } |
||
| 132 | |||
| 133 | public function has(string $key): bool |
||
| 134 | { |
||
| 135 | return array_key_exists($key, $this->data); |
||
| 136 | } |
||
| 137 | |||
| 138 | private function setObject($object, $key): void |
||
| 139 | { |
||
| 140 | (is_object($object)) ? $this->mergeData($key, $object) : $this->iOc($key, $object); |
||
| 141 | } |
||
| 142 | |||
| 143 | private function mergeData(string $key, $object) |
||
| 146 | } |
||
| 147 | |||
| 148 | private function iOc(string $key, $object, $params = null): void |
||
| 149 | { |
||
| 150 | $reflection = new \ReflectionClass($object); |
||
| 151 | $constructor = $reflection->getConstructor(); |
||
| 152 | |||
| 153 | if ($constructor && $constructor->getNumberOfParameters()) { |
||
| 160 | } |
||
| 161 | |||
| 162 | private function getParamsIoC(\ReflectionMethod $constructor, $params): array |
||
| 194 | } |
||
| 195 | } |
||
| 196 |