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