Total Complexity | 46 |
Total Lines | 223 |
Duplicated Lines | 0 % |
Changes | 2 | ||
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 |
||
33 | } |
||
34 | |||
35 | # Основные магические методы |
||
36 | |||
37 | public function __get (string $name) |
||
88 | } |
||
89 | |||
90 | public function __set (string $name, $value): void |
||
99 | } |
||
100 | |||
101 | public function __call (string $name, array $args) |
||
102 | { |
||
103 | return EngineAdditions::coupleSelector ($this->callMethod ($name, array_map ([$this, 'formatArg'], $args))); |
||
104 | } |
||
105 | |||
106 | protected function formatArg ($item) |
||
107 | { |
||
108 | $item = EngineAdditions::uncoupleSelector ($item); |
||
109 | |||
110 | if (is_array ($item)) |
||
111 | $item = EngineAdditions::uncoupleSelector (dnArray (VoidCore::callMethod (VoidCore::callMethod ([current ($item), VC_OBJECT], 'GetType')), 'ToString', $item)); |
||
112 | |||
113 | return $item; |
||
114 | } |
||
115 | |||
116 | # Управление VoidCore |
||
117 | |||
118 | protected function getProperty ($name) |
||
119 | { |
||
120 | return VoidCore::getProperty ($this->selector, $name); |
||
121 | } |
||
122 | |||
123 | protected function setProperty (string $name, $value): void |
||
124 | { |
||
125 | VoidCore::setProperty ($this->selector, $name, $value); |
||
126 | } |
||
127 | |||
128 | protected function callMethod (string $name, array $args = []) |
||
129 | { |
||
130 | return VoidCore::callMethod ($this->selector, $name, ...$args); |
||
131 | } |
||
132 | |||
133 | # ArrayAccess |
||
134 | |||
135 | public function offsetSet ($index, $value) |
||
136 | { |
||
137 | try |
||
138 | { |
||
139 | $index === null ? |
||
140 | $this->callMethod ('Add', EngineAdditions::uncoupleSelector ($value)) : |
||
141 | $this->callMethod ('Insert', $index, EngineAdditions::uncoupleSelector ($value)); |
||
142 | } |
||
143 | |||
144 | catch (\Throwable $e) |
||
145 | { |
||
146 | $index === null ? |
||
147 | VoidCore::setArrayValue ($this->selector, $this->count, EngineAdditions::uncoupleSelector ($value)) : |
||
148 | VoidCore::setArrayValue ($this->selector, $index, EngineAdditions::uncoupleSelector ($value)); |
||
149 | } |
||
150 | } |
||
151 | |||
152 | public function offsetGet ($index) |
||
153 | { |
||
154 | return EngineAdditions::coupleSelector (VoidCore::getArrayValue ($this->selector, $index), $this->selector); |
||
155 | } |
||
156 | |||
157 | public function offsetUnset ($index): void |
||
158 | { |
||
159 | $this->callMethod ('RemoveAt', $index); |
||
160 | } |
||
161 | |||
162 | public function offsetExists ($index): bool |
||
163 | { |
||
164 | try |
||
165 | { |
||
166 | $this->offsetGet ($index); |
||
167 | } |
||
168 | |||
169 | catch (\WinformsException $e) |
||
170 | { |
||
171 | return false; |
||
172 | } |
||
173 | |||
174 | return true; |
||
175 | } |
||
176 | |||
177 | # Итерация массивов |
||
178 | |||
179 | public function foreach (callable $callback, string $type = null): void |
||
180 | { |
||
181 | $size = $this->count; |
||
182 | |||
183 | for ($i = 0; $i < $size; ++$i) |
||
184 | $callback (EngineAdditions::coupleSelector (VoidCore::getArrayValue ($this->selector, $type !== null ? [$i, $type] : $i), $this->selector), $i); |
||
185 | } |
||
186 | |||
187 | public function where (callable $comparator, string $type = null): array |
||
188 | { |
||
189 | $size = $this->count; |
||
190 | $return = []; |
||
191 | |||
192 | for ($i = 0; $i < $size; ++$i) |
||
193 | if ($comparator ($value = EngineAdditions::coupleSelector (VoidCore::getArrayValue ($this->selector, $type !== null ? [$i, $type] : $i), $this->selector), $i)) |
||
194 | $return[] = $value; |
||
195 | |||
196 | return $return; |
||
197 | } |
||
198 | |||
199 | # Магические методы |
||
200 | |||
201 | public function __destruct () |
||
202 | { |
||
203 | VoidCore::destructObject ($this->selector); |
||
204 | } |
||
205 | |||
206 | public function __toString (): string |
||
207 | { |
||
208 | return $this->selector; |
||
209 | } |
||
210 | |||
211 | public function __debugInfo (): array |
||
230 | } |
||
231 | } |
||
232 | |||
233 | class NetClass extends NetObject |
||
234 | { |
||
235 | public function __construct ($name, $assembly = false) |
||
236 | { |
||
237 | if (is_int ($name) && VoidCore::objectExists ($name)) |
||
238 | $this->selector = $name; |
||
239 | |||
240 | elseif (is_string ($name)) |
||
241 | $this->selector = VoidCore::getClass ($name, $assembly); |
||
242 | |||
243 | else throw new \Exception ('Incorrect params passed'); |
||
321 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.