| Total Complexity | 48 |
| Total Lines | 239 |
| 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 |
||
| 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; |
||
|
1 ignored issue
–
show
|
|||
| 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 | case 'name': |
||
| 83 | try |
||
| 84 | { |
||
| 85 | return $this->getProperty ('Name'); |
||
| 86 | } |
||
| 87 | |||
| 88 | catch (\WinformsException $e) |
||
| 89 | { |
||
| 90 | return $this->name; |
||
| 91 | } |
||
| 92 | break; |
||
| 93 | } |
||
| 94 | |||
| 95 | if (method_exists ($this, $method = 'get_'. $name)) |
||
| 96 | return $this->$method (); |
||
| 97 | |||
| 98 | return isset ($this->$name) ? |
||
| 99 | $this->$name : EngineAdditions::coupleSelector ($this->getProperty ($name)); |
||
| 100 | } |
||
| 101 | |||
| 102 | public function __set (string $name, $value): void |
||
| 103 | { |
||
| 104 | if (substr ($name, -5) == 'Event') |
||
| 105 | Events::setEvent ($this->selector, substr ($name, 0, -5), $value); |
||
| 106 | |||
| 107 | elseif (method_exists ($this, $method = 'set_'. $name)) |
||
| 108 | $this->$method ($value); |
||
| 109 | |||
| 110 | elseif (strtolower ($name) == 'name') |
||
| 111 | { |
||
| 112 | try |
||
| 113 | { |
||
| 114 | $this->setProperty ($name, $value); |
||
| 115 | } |
||
| 116 | |||
| 117 | catch (\WinformsException $e) |
||
| 118 | { |
||
| 119 | $this->name = $value; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | else $this->setProperty ($name, EngineAdditions::uncoupleSelector ($value)); |
||
| 124 | } |
||
| 125 | |||
| 126 | public function __call (string $name, array $args) |
||
| 127 | { |
||
| 128 | return EngineAdditions::coupleSelector ($this->callMethod ($name, |
||
| 129 | array_map ('VoidEngine\\EngineAdditions::uncoupleSelector', $args))); |
||
| 130 | } |
||
| 131 | |||
| 132 | # Управление VoidCore |
||
| 133 | |||
| 134 | protected function getProperty ($name) |
||
| 135 | { |
||
| 136 | return VoidCore::getProperty ($this->selector, $name); |
||
| 137 | } |
||
| 138 | |||
| 139 | protected function setProperty (string $name, $value): void |
||
| 140 | { |
||
| 141 | VoidCore::setProperty ($this->selector, $name, $value); |
||
| 142 | } |
||
| 143 | |||
| 144 | protected function callMethod (string $name, array $args = []) |
||
| 145 | { |
||
| 146 | return VoidCore::callMethod ($this->selector, $name, ...$args); |
||
| 147 | } |
||
| 148 | |||
| 149 | # ArrayAccess |
||
| 150 | |||
| 151 | public function offsetSet ($index, $value) |
||
| 152 | { |
||
| 153 | try |
||
| 154 | { |
||
| 155 | $index === null ? |
||
| 156 | $this->callMethod ('Add', EngineAdditions::uncoupleSelector ($value)) : |
||
| 157 | $this->callMethod ('Insert', $index, EngineAdditions::uncoupleSelector ($value)); |
||
| 158 | } |
||
| 159 | |||
| 160 | catch (\Throwable $e) |
||
| 161 | { |
||
| 162 | $index === null ? |
||
| 163 | VoidCore::setArrayValue ($this->selector, $this->count, EngineAdditions::uncoupleSelector ($value)) : |
||
| 164 | VoidCore::setArrayValue ($this->selector, $index, EngineAdditions::uncoupleSelector ($value)); |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | public function offsetGet ($index) |
||
| 169 | { |
||
| 170 | return EngineAdditions::coupleSelector (VoidCore::getArrayValue ($this->selector, $index), $this->selector); |
||
| 171 | } |
||
| 172 | |||
| 173 | public function offsetUnset ($index): void |
||
| 174 | { |
||
| 175 | $this->callMethod ('RemoveAt', $index); |
||
| 176 | } |
||
| 177 | |||
| 178 | public function offsetExists ($index): bool |
||
| 179 | { |
||
| 180 | try |
||
| 181 | { |
||
| 182 | $this->offsetGet ($index); |
||
| 183 | } |
||
| 184 | |||
| 185 | catch (\WinformsException $e) |
||
| 186 | { |
||
| 187 | return false; |
||
| 188 | } |
||
| 189 | |||
| 190 | return true; |
||
| 191 | } |
||
| 192 | |||
| 193 | # Итерация массивов |
||
| 194 | |||
| 195 | public function foreach (callable $callback, string $type = null): void |
||
| 196 | { |
||
| 197 | $size = $this->count; |
||
| 198 | |||
| 199 | for ($i = 0; $i < $size; ++$i) |
||
| 200 | $callback (EngineAdditions::coupleSelector (VoidCore::getArrayValue ($this->selector, $type !== null ? [$i, $type] : $i), $this->selector), $i); |
||
| 201 | } |
||
| 202 | |||
| 203 | public function where (callable $comparator, string $type = null): array |
||
| 204 | { |
||
| 205 | $size = $this->count; |
||
| 206 | $return = []; |
||
| 207 | |||
| 208 | for ($i = 0; $i < $size; ++$i) |
||
| 209 | if ($comparator ($value = EngineAdditions::coupleSelector (VoidCore::getArrayValue ($this->selector, $type !== null ? [$i, $type] : $i), $this->selector), $i)) |
||
| 210 | $return[] = $value; |
||
| 211 | |||
| 212 | return $return; |
||
| 213 | } |
||
| 214 | |||
| 215 | # Магические методы |
||
| 216 | |||
| 217 | public function __destruct () |
||
| 218 | { |
||
| 219 | VoidCore::destructObject ($this->selector); |
||
| 220 | } |
||
| 221 | |||
| 222 | public function __toString (): string |
||
| 223 | { |
||
| 224 | return $this->selector; |
||
| 225 | } |
||
| 226 | |||
| 227 | public function __debugInfo (): array |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | class NetClass extends NetObject |
||
| 250 | { |
||
| 251 | public function __construct ($name, $assembly = false) |
||
| 252 | { |
||
| 253 | if (is_int ($name) && VoidCore::objectExists ($name)) |
||
| 254 | $this->selector = $name; |
||
| 255 | |||
| 256 | elseif (is_string ($name)) |
||
| 257 | $this->selector = VoidCore::getClass ($name, $assembly); |
||
| 258 | |||
| 259 | else throw new \Exception ('Incorrect params passed'); |
||
| 260 | } |
||
| 277 |
The
breakstatement is not necessary if it is preceded for example by areturnstatement:If you would like to keep this construct to be consistent with other
casestatements, you can safely mark this issue as a false-positive.