Total Complexity | 41 |
Total Lines | 164 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 1 |
Complex classes like Application 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 Application, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Application extends Container implements ApplicationInterface |
||
17 | { |
||
18 | use InstantiationsTrait; |
||
19 | |||
20 | public static ?ApplicationInterface $application = null; |
||
21 | |||
22 | public function __construct() |
||
24 | } |
||
25 | |||
26 | public function setServices(array $services): void |
||
27 | { |
||
28 | ($this->has("binding")) ?: $this->set(["binding", new Container($services["contracts"])]); |
||
29 | ($this->has("services")) ?: $this->set(["services", new Container($services["services"])]); |
||
30 | ($this->has("config")) ?: $this->set(["config", new Container($services["config"])]); |
||
31 | } |
||
32 | |||
33 | public function cookie(): ContainerInterface |
||
34 | { |
||
35 | return $this->get("cookie"); |
||
36 | } |
||
37 | |||
38 | public function session(): ContainerInterface |
||
39 | { |
||
40 | return $this->get("session"); |
||
41 | } |
||
42 | |||
43 | public function binding(): ContainerInterface |
||
44 | { |
||
45 | return $this->get("binding"); |
||
46 | } |
||
47 | |||
48 | public function services(): ContainerInterface |
||
49 | { |
||
50 | return $this->get("services"); |
||
51 | } |
||
52 | |||
53 | public function config(): ContainerInterface |
||
54 | { |
||
55 | return $this->get("config"); |
||
56 | } |
||
57 | |||
58 | public function request(): RequestInterface |
||
59 | { |
||
60 | return $this->get("request"); |
||
61 | } |
||
62 | |||
63 | public function response(): ResponseInterface |
||
64 | { |
||
65 | return $this->instantiate("response", Response::class); |
||
66 | } |
||
67 | |||
68 | public static function run(): ApplicationInterface |
||
69 | { |
||
70 | if (!static::$application instanceof static) { |
||
71 | static::$application = new static(); |
||
72 | } |
||
73 | |||
74 | return static::$application; |
||
|
|||
75 | } |
||
76 | |||
77 | public function get(string $key = null) |
||
78 | { |
||
79 | if (isset($key) && !$this->has($key)) { |
||
80 | if (!$this->services()->has($key)) { |
||
81 | throw new InvalidArgumentException("Service is not installed"); |
||
82 | } |
||
83 | |||
84 | $this->set([$key, $this->services()->get($key)]); |
||
85 | } |
||
86 | |||
87 | return empty($key) ? $this->data : $this->data[$key]; |
||
88 | } |
||
89 | |||
90 | public function set(array $data): void |
||
91 | { |
||
92 | list($key, $object) = $data; |
||
93 | |||
94 | if (is_array($object)) { |
||
95 | if (array_key_exists(1, $object) && !is_object($object[0])) { |
||
96 | $this->iOc($key, $object[0], $object[1]); |
||
97 | return; |
||
98 | } |
||
99 | |||
100 | $this->setObject($object[0], $key); |
||
101 | return; |
||
102 | } |
||
103 | |||
104 | $this->setObject($object, $key); |
||
105 | } |
||
106 | |||
107 | private function setObject($object, $key): void |
||
108 | { |
||
109 | (is_object($object)) ? $this->mergeData($key, $object) : $this->iOc($key, $object); |
||
110 | } |
||
111 | |||
112 | private function mergeData(string $key, $object) |
||
113 | { |
||
114 | $this->data = array_merge([$key => $object], $this->data); |
||
115 | } |
||
116 | |||
117 | private function iOc(string $key, $object, $params = null): void |
||
118 | { |
||
119 | $reflection = new \ReflectionClass($object); |
||
120 | $constructor = $reflection->getConstructor(); |
||
121 | |||
122 | if ($constructor && $constructor->getNumberOfParameters()) { |
||
123 | $paramsIoC = $this->getParamsIoC($constructor, $params); |
||
124 | $this->mergeData($key, $reflection->newInstanceArgs($paramsIoC)); |
||
125 | return; |
||
126 | } |
||
127 | |||
128 | $this->mergeData($key, new $object()); |
||
129 | } |
||
130 | |||
131 | private function getParamsIoC(\ReflectionMethod $constructor, $params): array |
||
163 | } |
||
164 | |||
165 | /* |
||
166 | | Creates an object without adding to the container |
||
167 | */ |
||
168 | public function new($object, $params = null) |
||
169 | { |
||
170 | $reflection = new \ReflectionClass($object); |
||
171 | $constructor = $reflection->getConstructor(); |
||
172 | |||
173 | if ($constructor && $constructor->getNumberOfParameters()) { |
||
174 | $paramsIoC = $this->getParamsIoC($constructor, $params); |
||
180 | } |
||
181 | } |
||
182 |