| Total Complexity | 57 |
| Total Lines | 367 |
| Duplicated Lines | 1.36 % |
| Coverage | 88.16% |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AbstractArrayBackedDaftObject 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 AbstractArrayBackedDaftObject, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | abstract class AbstractArrayBackedDaftObject extends AbstractDaftObject implements DaftObjectCreatedByArray |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * data for this instance. |
||
| 18 | * |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | private $data = []; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * List of changed properties. |
||
| 25 | * |
||
| 26 | * @var bool[] |
||
| 27 | */ |
||
| 28 | private $changedProperties = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * List of changed properties, for write-once read-many. |
||
| 32 | * |
||
| 33 | * @var bool[] |
||
| 34 | */ |
||
| 35 | private $wormProperties = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * {@inheritdoc} |
||
| 39 | */ |
||
| 40 | 189 | public function __construct(array $data = [], bool $writeAll = false) |
|
| 41 | { |
||
| 42 | 189 | parent::__construct(); |
|
| 43 | |||
| 44 | 187 | if (true === $writeAll) { |
|
| 45 | 60 | foreach ($data as $k => $v) { |
|
| 46 | 60 | $this->__set($k, $v); |
|
| 47 | } |
||
| 48 | } else { |
||
| 49 | 129 | foreach ($data as $k => $v) { |
|
| 50 | 68 | $this->data[$k] = $v; |
|
| 51 | } |
||
| 52 | } |
||
| 53 | 185 | } |
|
| 54 | |||
| 55 | /** |
||
| 56 | * {@inheritdoc} |
||
| 57 | */ |
||
| 58 | 33 | public function __isset(string $property) : bool |
|
| 59 | { |
||
| 60 | return |
||
| 61 | 33 | in_array($property, static::PROPERTIES, true) && |
|
| 62 | 33 | isset($this->data, $this->data[$property]); |
|
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritdoc} |
||
| 67 | */ |
||
| 68 | 29 | public function ChangedProperties() : array |
|
| 69 | { |
||
| 70 | /** |
||
| 71 | * @var string[] $out |
||
| 72 | */ |
||
| 73 | 29 | $out = array_keys($this->changedProperties); |
|
| 74 | |||
| 75 | 29 | return $out; |
|
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * {@inheritdoc} |
||
| 80 | */ |
||
| 81 | 33 | public function MakePropertiesUnchanged(string ...$properties) : void |
|
| 85 | } |
||
| 86 | 33 | } |
|
| 87 | |||
| 88 | /** |
||
| 89 | * {@inheritdoc} |
||
| 90 | */ |
||
| 91 | 82 | public function HasPropertyChanged(string $property) : bool |
|
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | */ |
||
| 101 | 22 | public function jsonSerialize() : array |
|
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * {@inheritdoc} |
||
| 130 | */ |
||
| 131 | 22 | final public static function DaftObjectFromJsonArray( |
|
| 132 | array $array, |
||
| 133 | bool $writeAll = false |
||
| 134 | ) : DaftJson { |
||
| 135 | 22 | static::ThrowIfNotDaftJson(); |
|
| 136 | 6 | $array = static::ThrowIfJsonDefNotValid($array); |
|
| 137 | 6 | $in = []; |
|
| 138 | |||
| 139 | 6 | $jsonDef = static::DaftObjectJsonProperties(); |
|
| 140 | |||
| 141 | 6 | foreach (array_keys($array) as $prop) { |
|
| 142 | 6 | if (isset($jsonDef[$prop])) { |
|
| 143 | 3 | $jsonType = $jsonDef[$prop]; |
|
| 144 | |||
| 145 | 3 | if ('[]' === mb_substr($jsonType, -2)) { |
|
| 146 | 1 | $in[$prop] = static::DaftObjectFromJsonTypeArray( |
|
| 147 | 1 | mb_substr($jsonType, 0, -2), |
|
| 148 | 1 | $prop, |
|
| 149 | 1 | $array[$prop], |
|
| 150 | 1 | $writeAll |
|
| 151 | ); |
||
| 152 | } else { |
||
| 153 | 2 | $in[$prop] = static::DaftObjectFromJsonType( |
|
| 154 | 2 | $jsonType, |
|
| 155 | 2 | $array[$prop], |
|
| 156 | 2 | $writeAll |
|
| 157 | ); |
||
| 158 | } |
||
| 159 | } else { |
||
| 160 | 6 | $in[$prop] = $array[$prop]; |
|
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | 6 | $out = new static($in, $writeAll); |
|
| 165 | |||
| 166 | 6 | if ( ! ($out instanceof DaftJson)) { // here to trick phpstan |
|
| 167 | exit; |
||
| 168 | } |
||
| 169 | |||
| 170 | 6 | return $out; |
|
| 171 | } |
||
| 172 | |||
| 173 | 22 | public static function DaftObjectFromJsonString(string $string) : DaftJson |
|
| 174 | { |
||
| 175 | 22 | static::ThrowIfNotDaftJson(); |
|
| 176 | |||
| 177 | 6 | return static::DaftObjectFromJsonArray(json_decode($string, true)); |
|
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param mixed $propVal |
||
| 182 | * |
||
| 183 | * @return DaftJson |
||
| 184 | */ |
||
| 185 | 2 | protected static function DaftObjectFromJsonType( |
|
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @return DaftJson[] |
||
| 197 | */ |
||
| 198 | 1 | protected static function DaftObjectFromJsonTypeArray( |
|
| 199 | string $jsonType, |
||
| 200 | string $prop, |
||
| 201 | array $propVal, |
||
| 202 | bool $writeAll |
||
| 203 | ) : array { |
||
| 204 | 1 | static::ThrowIfNotJsonType($jsonType); |
|
| 205 | |||
| 206 | 1 | $out = []; |
|
| 207 | |||
| 208 | 1 | foreach ($propVal as $val) { |
|
| 209 | 1 | if (false === is_array($val)) { |
|
| 210 | throw new PropertyNotJsonDecodableShouldBeArrayException( |
||
| 211 | $jsonType, |
||
| 212 | $prop |
||
| 213 | ); |
||
| 214 | } |
||
| 215 | 1 | $out[] = static::ArrayToJsonType( |
|
| 216 | 1 | $jsonType, |
|
| 217 | 1 | $val, |
|
| 218 | 1 | $writeAll |
|
| 219 | ); |
||
| 220 | } |
||
| 221 | |||
| 222 | 1 | return $out; |
|
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Retrieve a property from data. |
||
| 227 | * |
||
| 228 | * @param string $property the property being retrieved |
||
| 229 | * |
||
| 230 | * @throws PropertyNotNullableException if value is not set and $property is not listed as nullabe |
||
| 231 | * |
||
| 232 | * @return mixed the property value |
||
| 233 | */ |
||
| 234 | 44 | protected function RetrievePropertyValueFromData(string $property) |
|
| 235 | { |
||
| 236 | if ( |
||
| 237 | 44 | false === array_key_exists($property, $this->data) && |
|
| 238 | 5 | false === in_array($property, static::NULLABLE_PROPERTIES, true) |
|
| 239 | ) { |
||
| 240 | 1 | throw new PropertyNotNullableException(static::class, $property); |
|
| 241 | } elseif ( |
||
| 242 | 43 | in_array($property, static::NULLABLE_PROPERTIES, true) |
|
| 243 | ) { |
||
| 244 | 33 | return $this->data[$property] ?? null; |
|
| 245 | } |
||
| 246 | |||
| 247 | 43 | return $this->data[$property]; |
|
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * {@inheritdoc} |
||
| 252 | */ |
||
| 253 | 134 | protected function NudgePropertyValue(string $property, $value) : void |
|
| 288 | } |
||
| 289 | 75 | } |
|
| 290 | |||
| 291 | 6 | private static function ThrowIfJsonDefNotValid(array $array) : array |
|
| 292 | { |
||
| 293 | 6 | $jsonProps = []; |
|
| 294 | |||
| 295 | 6 | $jsonDef = static::DaftObjectJsonProperties(); |
|
| 296 | 6 | $nullableProps = static::DaftObjectNullableProperties(); |
|
| 297 | |||
| 298 | 6 | foreach ($jsonDef as $k => $v) { |
|
| 299 | 6 | $prop = $v; |
|
| 300 | 6 | if (is_string($k)) { |
|
| 301 | 3 | $prop = $k; |
|
| 302 | } |
||
| 303 | if ( |
||
| 304 | ( |
||
| 305 | 6 | false === isset($array[$prop]) || |
|
| 306 | 6 | is_null($array[$prop]) |
|
| 307 | ) && |
||
| 308 | 2 | false === in_array($prop, $nullableProps, true) |
|
| 309 | ) { |
||
| 310 | throw new PropertyNotNullableException(static::class, $prop); |
||
| 311 | } |
||
| 312 | |||
| 313 | 6 | $jsonProps[] = $prop; |
|
| 314 | } |
||
| 315 | |||
| 316 | 6 | $out = []; |
|
| 317 | |||
| 318 | 6 | foreach ($array as $prop => $propVal) { |
|
| 319 | if ( |
||
| 320 | 6 | false === in_array($prop, $jsonProps, true) |
|
| 321 | ) { |
||
| 322 | throw new PropertyNotJsonDecodableException( |
||
| 323 | static::class, |
||
| 324 | $prop |
||
| 325 | ); |
||
| 326 | 6 | } elseif (false === is_null($propVal)) { |
|
| 327 | 6 | if (isset($jsonDef[$prop])) { |
|
| 328 | 3 | $jsonType = $jsonDef[$prop]; |
|
| 329 | |||
| 330 | 3 | if (false === is_array($propVal)) { |
|
| 331 | if ('[]' === mb_substr($jsonType, -2)) { |
||
| 332 | throw new PropertyNotJsonDecodableShouldBeArrayException( |
||
| 333 | static::class, |
||
| 334 | $prop |
||
| 335 | ); |
||
| 336 | } |
||
| 337 | throw new PropertyNotJsonDecodableShouldBeArrayException( |
||
| 338 | $jsonType, |
||
| 339 | $prop |
||
| 340 | ); |
||
| 341 | } |
||
| 342 | } |
||
| 343 | 6 | $out[$prop] = $propVal; |
|
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | 6 | return $out; |
|
| 348 | } |
||
| 349 | |||
| 350 | 3 | private static function ThrowIfNotJsonType(string $jsonType) : void |
|
| 351 | { |
||
| 352 | 3 | if (false === is_a($jsonType, DaftJson::class, true)) { |
|
| 353 | throw new ClassDoesNotImplementClassException( |
||
| 354 | $jsonType, |
||
| 355 | DaftJson::class |
||
| 356 | ); |
||
| 357 | } |
||
| 358 | 3 | } |
|
| 359 | |||
| 360 | 3 | private static function ArrayToJsonType( |
|
| 361 | string $jsonType, |
||
| 362 | array $propVal, |
||
| 363 | bool $writeAll |
||
| 364 | ) : DaftJson { |
||
| 365 | /** |
||
| 366 | * @var DaftJson $jsonType |
||
| 367 | */ |
||
| 368 | 3 | $jsonType = $jsonType; |
|
| 369 | |||
| 370 | 3 | return $jsonType::DaftObjectFromJsonArray( |
|
| 371 | 3 | $propVal, |
|
| 372 | 3 | $writeAll |
|
| 373 | ); |
||
| 374 | } |
||
| 375 | |||
| 376 | 54 | View Code Duplication | private static function ThrowIfNotDaftJson() : void |
| 381 | ); |
||
| 382 | } |
||
| 383 | 6 | } |
|
| 384 | } |
||
| 385 |