| Total Complexity | 44 |
| Total Lines | 293 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AbstractObject 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 AbstractObject, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | abstract class AbstractObject |
||
| 23 | { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $typeCheck = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | protected $required = []; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $attributes = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $errors = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var null|UpDown |
||
| 47 | */ |
||
| 48 | protected $updown = null; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * AbstractObject constructor. |
||
| 52 | * |
||
| 53 | * @param array $args |
||
| 54 | */ |
||
| 55 | public function __construct($args = []) |
||
| 56 | { |
||
| 57 | |||
| 58 | $this->setUpDown(); |
||
| 59 | $this->setArgs($args); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Set UpDown instance |
||
| 64 | */ |
||
| 65 | protected function setUpDown() |
||
| 66 | { |
||
| 67 | |||
| 68 | $this->updown = static::getUpDownInstance(UpDown::instance()); |
||
| 69 | |||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param UpDown|null $instance |
||
| 74 | * |
||
| 75 | * @return UpDown |
||
| 76 | * @throws Exception |
||
| 77 | */ |
||
| 78 | protected static function getUpDownInstance(UpDown $instance = null): UpDown |
||
| 79 | { |
||
| 80 | |||
| 81 | if (!$instance instanceof UpDown) { |
||
| 82 | throw new Exception('Invalid UpDown instance'); |
||
| 83 | } |
||
| 84 | |||
| 85 | return $instance; |
||
| 86 | |||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param array|null $args |
||
| 91 | * |
||
| 92 | * @return AbstractObject |
||
| 93 | */ |
||
| 94 | protected function setArgs(?array $args = []): AbstractObject |
||
| 95 | { |
||
| 96 | |||
| 97 | if (is_null($args)) { |
||
|
|
|||
| 98 | $args = []; |
||
| 99 | } |
||
| 100 | |||
| 101 | foreach ($this->typeCheck as $field_name => $field_type) { |
||
| 102 | if (empty($args[$field_name]) || is_null($args[$field_name])) { |
||
| 103 | if ($this->isFieldRequired($field_name)) { |
||
| 104 | $this->addError('Missing "' . $field_name . '" in ' . static::class); |
||
| 105 | } |
||
| 106 | } else { |
||
| 107 | $this->attributes[$field_name] = $this->parseFieldValue($field_type, $args[$field_name]); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | $this->throwErrors(); |
||
| 111 | |||
| 112 | return $this; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param string $field_name |
||
| 117 | * |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | protected function isFieldRequired(string $field_name): bool |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param string $error |
||
| 128 | * |
||
| 129 | * @return array |
||
| 130 | */ |
||
| 131 | protected function addError(string $error): array |
||
| 132 | { |
||
| 133 | |||
| 134 | array_push($this->errors, $error); |
||
| 135 | |||
| 136 | return $this->errors; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param string $field_type |
||
| 141 | * @param string $field_value |
||
| 142 | * |
||
| 143 | * @return mixed |
||
| 144 | */ |
||
| 145 | protected function parseFieldValue(string $field_type, $field_value) |
||
| 146 | { |
||
| 147 | |||
| 148 | switch ($field_type) { |
||
| 149 | case UpDownFieldType::STRING: |
||
| 150 | case UpDownFieldType::INT: |
||
| 151 | case UpDownFieldType::FLOAT: |
||
| 152 | case UpDownFieldType::ARRAY: |
||
| 153 | case UpDownFieldType::BOOL: |
||
| 154 | return $field_value; |
||
| 155 | case UpDownFieldType::DATETIME: |
||
| 156 | return strtotime($field_value); |
||
| 157 | default: |
||
| 158 | return ($field_value instanceof $field_type) ? $field_value : new $field_type($field_value); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @throws Exception |
||
| 164 | */ |
||
| 165 | protected function throwErrors() |
||
| 166 | { |
||
| 167 | |||
| 168 | if (count($this->errors)) { |
||
| 169 | throw new Exception(implode(', ', $this->errors)); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param array|null $args |
||
| 175 | * |
||
| 176 | * @return AbstractObject |
||
| 177 | */ |
||
| 178 | public function setData(?array $args = []): AbstractObject |
||
| 179 | { |
||
| 180 | |||
| 181 | return $this->setArgs($args); |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function toJson(): string |
||
| 188 | { |
||
| 189 | |||
| 190 | return json_encode($this->toArray()); |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | public function toArray(): array |
||
| 197 | { |
||
| 198 | |||
| 199 | $fields = $this->attributes; |
||
| 200 | |||
| 201 | foreach ($fields as $field_name => $field_value) { |
||
| 202 | |||
| 203 | if (!is_scalar($field_value) && method_exists($field_value, 'toJson')) { |
||
| 204 | $fields[$field_name] = $field_value->toArray(); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | return $fields; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | public function __toString(): string |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param $name |
||
| 222 | * @param $arguments |
||
| 223 | * |
||
| 224 | * @return mixed |
||
| 225 | */ |
||
| 226 | public function __call($name, $arguments) |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param string $snake_field |
||
| 248 | * |
||
| 249 | * @return mixed|null |
||
| 250 | * @throws Exception |
||
| 251 | */ |
||
| 252 | public function __get(string $snake_field) |
||
| 261 | |||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param string $snake_field |
||
| 266 | * @param $value |
||
| 267 | * |
||
| 268 | * @return mixed |
||
| 269 | */ |
||
| 270 | public function __set(string $snake_field, $value) |
||
| 271 | { |
||
| 272 | |||
| 273 | $field_type = $this->getPropertyType($snake_field); |
||
| 274 | |||
| 275 | return $this->attributes[$snake_field] = $this->parseFieldValue($field_type, $value); |
||
| 276 | |||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $snake_field |
||
| 281 | * |
||
| 282 | * @return null|string |
||
| 283 | */ |
||
| 284 | protected function getPropertyType(string $snake_field): ?string |
||
| 285 | { |
||
| 286 | |||
| 287 | return (empty($this->typeCheck[$snake_field])) ? null : $this->typeCheck[$snake_field]; |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | public function getClassName() |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @return mixed |
||
| 301 | */ |
||
| 302 | public function getCleanClassName() |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | public function getNamespace() |
||
| 315 | } |
||
| 316 | |||
| 317 | } |