Total Complexity | 44 |
Total Lines | 214 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like NetObject 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 NetObject, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class NetObject implements \ArrayAccess |
||
8 | { |
||
9 | protected int $selector = 0; |
||
10 | protected ?string $name = null; |
||
11 | // protected bool $isCollection = false; |
||
12 | |||
13 | public function __construct ($name, $assembly = false, ...$args) |
||
14 | { |
||
15 | foreach ($args as $id => $arg) |
||
16 | $args[$id] = EngineAdditions::uncoupleSelector ($arg); |
||
17 | |||
18 | if (is_int ($name) && VoidCore::objectExists ($name)) |
||
19 | $this->selector = $name; |
||
20 | |||
21 | elseif (is_string ($name)) |
||
22 | $this->selector = VoidCore::createObject ($name, $assembly, ...$args); |
||
23 | |||
24 | else throw new \Exception ('Incorrect params passed'); |
||
25 | |||
26 | /*$this->isCollection = $this->getType () |
||
27 | ->isSubclassOf (VoidCore::typeof ('System.Collectons.Generic.ICollection'));*/ |
||
28 | } |
||
29 | |||
30 | public function dispose (): void |
||
31 | { |
||
32 | VoidCore::removeObjects ($this->selector); |
||
33 | } |
||
34 | |||
35 | # Основные магические методы |
||
36 | |||
37 | public function __get (string $name) |
||
38 | { |
||
39 | switch (strtolower ($name)) |
||
40 | { |
||
41 | case 'count': |
||
42 | case 'length': |
||
43 | try |
||
44 | { |
||
45 | return $this->getProperty ('Count'); |
||
46 | } |
||
47 | |||
48 | catch (\WinformsException $e) |
||
49 | { |
||
50 | return $this->getProperty ('Length'); |
||
51 | } |
||
52 | break; |
||
53 | |||
54 | case 'list': |
||
55 | $size = $this->count; |
||
56 | $list = []; |
||
57 | |||
58 | for ($i = 0; $i < $size; ++$i) |
||
59 | $list[] = EngineAdditions::coupleSelector (VoidCore::getArrayValue ($this->selector, $i)); |
||
60 | |||
61 | return $list; |
||
62 | break; |
||
63 | |||
64 | case 'names': |
||
65 | $size = $this->count; |
||
66 | $names = []; |
||
67 | |||
68 | for ($i = 0; $i < $size; ++$i) |
||
69 | try |
||
70 | { |
||
71 | $names[] = VoidCore::getProperty (VoidCore::getArrayValue ($this->selector, [$i, VC_OBJECT]), 'Text'); |
||
72 | } |
||
73 | |||
74 | catch (\WinformsException $e) |
||
75 | { |
||
76 | $names[] = VoidCore::getArrayValue ($this->selector, [$i, VC_STRING]); |
||
77 | } |
||
78 | |||
79 | return $names; |
||
80 | break; |
||
81 | } |
||
82 | |||
83 | if (method_exists ($this, $method = 'get_'. $name)) |
||
84 | return $this->$method (); |
||
85 | |||
86 | return isset ($this->$name) ? |
||
87 | $this->$name : EngineAdditions::coupleSelector ($this->getProperty ($name)); |
||
88 | } |
||
89 | |||
90 | public function __set (string $name, $value): void |
||
91 | { |
||
92 | if (substr ($name, -5) == 'Event') |
||
93 | Events::setEvent ($this->selector, substr ($name, 0, -5), $value); |
||
94 | |||
95 | elseif (method_exists ($this, $method = 'set_'. $name)) |
||
96 | $this->$method ($value); |
||
97 | |||
98 | else $this->setProperty ($name, EngineAdditions::uncoupleSelector ($value)); |
||
99 | } |
||
100 | |||
101 | public function __call (string $name, array $args) |
||
102 | { |
||
103 | return EngineAdditions::coupleSelector ($this->callMethod ($name, |
||
104 | array_map ('VoidEngine\\EngineAdditions::uncoupleSelector', $args))); |
||
105 | } |
||
106 | |||
107 | # Управление VoidCore |
||
108 | |||
109 | protected function getProperty ($name) |
||
110 | { |
||
111 | return VoidCore::getProperty ($this->selector, $name); |
||
112 | } |
||
113 | |||
114 | protected function setProperty (string $name, $value): void |
||
115 | { |
||
116 | VoidCore::setProperty ($this->selector, $name, $value); |
||
117 | } |
||
118 | |||
119 | protected function callMethod (string $name, array $args = []) |
||
120 | { |
||
121 | return VoidCore::callMethod ($this->selector, $name, ...$args); |
||
122 | } |
||
123 | |||
124 | # ArrayAccess |
||
125 | |||
126 | public function offsetSet ($index, $value) |
||
127 | { |
||
128 | try |
||
129 | { |
||
130 | $index === null ? |
||
131 | $this->callMethod ('Add', EngineAdditions::uncoupleSelector ($value)) : |
||
|
|||
132 | $this->callMethod ('Insert', $index, EngineAdditions::uncoupleSelector ($value)); |
||
133 | } |
||
134 | |||
135 | catch (\Throwable $e) |
||
136 | { |
||
137 | $index === null ? |
||
138 | VoidCore::setArrayValue ($this->selector, $this->count, EngineAdditions::uncoupleSelector ($value)) : |
||
139 | VoidCore::setArrayValue ($this->selector, $index, EngineAdditions::uncoupleSelector ($value)); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | public function offsetGet ($index) |
||
144 | { |
||
145 | return EngineAdditions::coupleSelector (VoidCore::getArrayValue ($this->selector, $index), $this->selector); |
||
146 | } |
||
147 | |||
148 | public function offsetUnset ($index): void |
||
149 | { |
||
150 | $this->callMethod ('RemoveAt', $index); |
||
151 | } |
||
152 | |||
153 | public function offsetExists ($index): bool |
||
154 | { |
||
155 | try |
||
156 | { |
||
157 | $this->offsetGet ($index); |
||
158 | } |
||
159 | |||
160 | catch (\WinformsException $e) |
||
161 | { |
||
162 | return false; |
||
163 | } |
||
164 | |||
165 | return true; |
||
166 | } |
||
167 | |||
168 | # Итерация массивов |
||
169 | |||
170 | public function foreach (callable $callback, string $type = null): void |
||
171 | { |
||
172 | $size = $this->count; |
||
173 | |||
174 | for ($i = 0; $i < $size; ++$i) |
||
175 | $callback (EngineAdditions::coupleSelector (VoidCore::getArrayValue ($this->selector, $type !== null ? [$i, $type] : $i), $this->selector), $i); |
||
176 | } |
||
177 | |||
178 | public function where (callable $comparator, string $type = null): array |
||
179 | { |
||
180 | $size = $this->count; |
||
181 | $return = []; |
||
182 | |||
183 | for ($i = 0; $i < $size; ++$i) |
||
184 | if ($comparator ($value = EngineAdditions::coupleSelector (VoidCore::getArrayValue ($this->selector, $type !== null ? [$i, $type] : $i), $this->selector), $i)) |
||
185 | $return[] = $value; |
||
186 | |||
187 | return $return; |
||
188 | } |
||
189 | |||
190 | # Магические методы |
||
191 | |||
192 | public function __destruct () |
||
193 | { |
||
194 | VoidCore::destructObject ($this->selector); |
||
195 | } |
||
196 | |||
197 | public function __toString (): string |
||
198 | { |
||
199 | return $this->selector; |
||
200 | } |
||
201 | |||
202 | public function __debugInfo (): array |
||
221 | } |
||
222 | } |
||
223 | |||
224 | class NetClass extends NetObject |
||
225 | { |
||
226 | public function __construct ($name, $assembly = false) |
||
227 | { |
||
228 | if (is_int ($name) && VoidCore::objectExists ($name)) |
||
229 | $this->selector = $name; |
||
230 | |||
231 | elseif (is_string ($name)) |
||
232 | $this->selector = VoidCore::getClass ($name, $assembly); |
||
233 | |||
234 | else throw new \Exception ('Incorrect params passed'); |
||
235 | } |
||
236 | } |
||
237 | |||
312 |