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