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