| Total Complexity | 96 |
| Total Lines | 467 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like TArraySubscription 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 TArraySubscription, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 69 | class TArraySubscription |
||
| 70 | { |
||
| 71 | use TPriorityPropertyTrait; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var null|array|ArrayAccess The array reference or ArrayAccess being subscribed to. |
||
| 75 | */ |
||
| 76 | private mixed $_array = null; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var null|int|string The key that the item is added to the array. |
||
| 80 | */ |
||
| 81 | private null|int|string $_key = null; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var mixed The item subscribing to the array. |
||
| 85 | */ |
||
| 86 | protected mixed $_item = null; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var ?string The class filtering the item for storage. This is populated when |
||
| 90 | * the {@see self::getArray()} is an instanceOf {@see ICollectionFilter}. |
||
| 91 | */ |
||
| 92 | protected ?string $_filterClass = null; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var null|bool|int Is the array an associative array. False for a "list" style array. |
||
| 96 | * null for discovery of the style of array on subscribe. default true. |
||
| 97 | */ |
||
| 98 | protected null|bool|int $_isAssoc = true; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var bool Is the item inserted into the collection. |
||
| 102 | */ |
||
| 103 | private bool $_isSubscribed = false; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var ?bool Is the subscription replacing an item. |
||
| 107 | */ |
||
| 108 | private ?bool $_isReplacing = null; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var mixed The original item. |
||
| 112 | */ |
||
| 113 | private mixed $_originalItem = null; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var ?float The priority of the original item. |
||
| 117 | */ |
||
| 118 | private ?float $_originalPriority = null; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Constructs the TArraySubscription. If there is an key or item then the item is |
||
| 122 | * automatically subscribed to the array when $autoSubscribe is the default null. |
||
| 123 | * When $autoSubscribe = true, the item is added regardless of a key and/or item |
||
| 124 | * has a value. |
||
| 125 | * @param mixed &$array The array to subscribe to, passed by reference. |
||
| 126 | * Default null. |
||
| 127 | * @param mixed $key The key of the subscribed item, default null for append the |
||
| 128 | * item. Default null. |
||
| 129 | * @param mixed $item The item to insert into the array at $key with $priority. |
||
| 130 | * Default null. |
||
| 131 | * @param null|float|int $priority The priority of the item for IPriorityCollection. |
||
| 132 | * Default null. |
||
| 133 | * @param null|bool|int $isAssociative Is the array an associative array. false is |
||
| 134 | * the list from (0...n-1). When false, this will use `array_splice` to insert |
||
| 135 | * the item. Default 1 for true except for TList. |
||
| 136 | * @param bool $autoSubscribe Should the |
||
| 137 | */ |
||
| 138 | public function __construct(mixed &$array = null, mixed $key = null, mixed $item = null, null|int|float $priority = null, null|bool|int $isAssociative = 1, ?bool $autoSubscribe = null) |
||
| 139 | { |
||
| 140 | $this->setArray($array); |
||
| 141 | $this->setKey($key); |
||
| 142 | $this->setItem($item); |
||
| 143 | $this->setPriority($priority); |
||
|
|
|||
| 144 | $this->setIsAssociative($isAssociative); |
||
| 145 | |||
| 146 | //parent::__construct(); |
||
| 147 | |||
| 148 | if (($autoSubscribe === null && ($key !== null || $item !== null) || $autoSubscribe === true) && $this->getArray() !== null) { |
||
| 149 | $this->subscribe(); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Cleans up the instance on destruction. |
||
| 155 | * If the item is subscribed to the array, the item is removed. |
||
| 156 | */ |
||
| 157 | public function __destruct() |
||
| 158 | { |
||
| 159 | $this->unsubscribe(); |
||
| 160 | //parent::__destruct(); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Returns the collection to which the item is subscribed. |
||
| 165 | * Be very careful with the returnad array as it is passed by reference and could change |
||
| 166 | * the original variable to something other than an array. |
||
| 167 | * ```php |
||
| 168 | * $array = & $subscription->getArray(); |
||
| 169 | * ... //use $array |
||
| 170 | * // $array = null; // This will destroy the array being used originally. Avoid this. |
||
| 171 | * unset($array); // This dereferences. |
||
| 172 | * |
||
| 173 | * // or, use a non pass-by-reference |
||
| 174 | * $array = $subscription->getArray(); |
||
| 175 | * $array = null; // ok. |
||
| 176 | * ``` |
||
| 177 | * @param bool $weak |
||
| 178 | * @return array|ArrayAccess The subscribed array-collection, passed by reference. |
||
| 179 | */ |
||
| 180 | public function &getArray(bool $weak = false): array|ArrayAccess|WeakReference|null |
||
| 181 | { |
||
| 182 | if ($this->_array instanceof WeakReference) { |
||
| 183 | if ($weak) { |
||
| 184 | $collection = $this->_array; |
||
| 185 | } else { |
||
| 186 | $collection = $this->_array->get(); |
||
| 187 | } |
||
| 188 | } elseif (is_array($this->_array)) { |
||
| 189 | $collection = &$this->_array; |
||
| 190 | } elseif ($this->_array instanceof ArrayAccess) { |
||
| 191 | $collection = $this->_array; |
||
| 192 | } else { |
||
| 193 | $collection = null; |
||
| 194 | } |
||
| 195 | return $collection; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Sets the array or ArrayAccess object. |
||
| 200 | * @param null|array|ArrayAccess $value The array, passed by reference. |
||
| 201 | * @throws TInvalidOperationException When setting during a subscription. |
||
| 202 | * @throws TInvalidOperationException If the item is already subscribed. |
||
| 203 | * @return static The current object. |
||
| 204 | */ |
||
| 205 | public function setArray(null|array|ArrayAccess &$value): static |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * If the item is subscribed and the key is null, the item key will be discovered |
||
| 223 | * with the TList (by {@see TList::indexOf()}) or array (by array_search). |
||
| 224 | * @return null|int|string The key for the item subscription to the array. |
||
| 225 | */ |
||
| 226 | public function getKey(): null|int|string |
||
| 227 | { |
||
| 228 | $collection = &$this->getArray(); |
||
| 229 | if ($this->_isSubscribed && $this->_key === null) { |
||
| 230 | if ($collection instanceof TList) { |
||
| 231 | $args = [$this->getItem()]; |
||
| 232 | if ($collection instanceof IPriorityCollection) { |
||
| 233 | $args[] = $this->getPriority(); |
||
| 234 | } |
||
| 235 | return ($index = $collection->indexOf(...$args)) === -1 ? null : $index; |
||
| 236 | } elseif (is_array($collection)) { |
||
| 237 | if (($key = array_search($this->getItem(), $collection, true)) === false) { |
||
| 238 | return null; |
||
| 239 | } |
||
| 240 | return $key; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | return $this->_key; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param mixed $value The key for the item subscription to the array. |
||
| 248 | * @throws TInvalidOperationException If the item is already subscribed. |
||
| 249 | */ |
||
| 250 | public function setKey(mixed $value): static |
||
| 251 | { |
||
| 252 | if ($this->_isSubscribed) { |
||
| 253 | throw new TInvalidOperationException('arraysubscription_no_change', 'Key'); |
||
| 254 | } |
||
| 255 | |||
| 256 | if (is_bool($value) || is_float($value)) { |
||
| 257 | $value = (int) $value; |
||
| 258 | } |
||
| 259 | $this->_key = $value; |
||
| 260 | |||
| 261 | return $this; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param bool $unfiltered Should the item be unfiltered back from the stored format. |
||
| 266 | * default false. |
||
| 267 | * @return mixed The item subscribing to the array. |
||
| 268 | */ |
||
| 269 | public function getItem(bool $unfiltered = false): mixed |
||
| 270 | { |
||
| 271 | $item = $this->_item; |
||
| 272 | |||
| 273 | if (!$unfiltered && $this->_filterClass !== null) { |
||
| 274 | $this->_filterClass::filterItemForOutput($item); |
||
| 275 | } |
||
| 276 | |||
| 277 | return $item; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param mixed $value The item subscribing to the array. |
||
| 282 | * @throws TInvalidOperationException If the item is already subscribed. |
||
| 283 | * @return static The current object. |
||
| 284 | */ |
||
| 285 | public function setItem(mixed $value): static |
||
| 286 | { |
||
| 287 | if ($this->_isSubscribed) { |
||
| 288 | throw new TInvalidOperationException('arraysubscription_no_change', 'Item'); |
||
| 289 | } |
||
| 290 | |||
| 291 | $this->_item = $value; |
||
| 292 | |||
| 293 | return $this; |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * This is on applicable to {@see IPriorityCollection}. |
||
| 298 | * @param ?numeric $value The priority of the item. |
||
| 299 | * @throws TInvalidOperationException If the item is already subscribed. |
||
| 300 | * @return static The current object. |
||
| 301 | */ |
||
| 302 | public function setPriority($value): static |
||
| 303 | { |
||
| 304 | if ($this->_isSubscribed) { |
||
| 305 | throw new TInvalidOperationException('arraysubscription_no_change', 'Priority'); |
||
| 306 | } |
||
| 307 | |||
| 308 | if ($value === '') { |
||
| 309 | $value = null; |
||
| 310 | } |
||
| 311 | if ($value !== null) { |
||
| 312 | $value = TPropertyValue::ensureFloat($value); |
||
| 313 | } |
||
| 314 | $this->_priority = $value; |
||
| 315 | |||
| 316 | return $this; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Whether to add to the array by association or by splicing (for an ordered list). |
||
| 321 | * @return null|null|bool Is the array associative; default true. false will treat the array |
||
| 322 | * as a list from (0, ..., count() - 1). if null, where needed, the "list"ness |
||
| 323 | * of the array will be determined by {@see TArrayHelper::array_is_list()}. |
||
| 324 | */ |
||
| 325 | public function getIsAssociative(): null|bool|int |
||
| 326 | { |
||
| 327 | return $this->_isAssoc; |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param null|bool|int $value Is the array associative; default null. false will treat the array |
||
| 332 | * as a list from (0, ..., count() - 1). if null, where needed, the "list"ness |
||
| 333 | * of the array will be determined by {@see TArrayHelper::array_is_list()}. |
||
| 334 | * @throws TInvalidOperationException If the item is already subscribed. |
||
| 335 | * @return static The current object. |
||
| 336 | */ |
||
| 337 | public function setIsAssociative(null|bool|int $value = null): static |
||
| 338 | { |
||
| 339 | if ($this->_isSubscribed) { |
||
| 340 | throw new TInvalidOperationException('arraysubscription_no_change', 'Key'); |
||
| 341 | } |
||
| 342 | |||
| 343 | $this->_isAssoc = $value; |
||
| 344 | |||
| 345 | return $this; |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @return bool Is the item subscribed to the array. |
||
| 350 | */ |
||
| 351 | public function getIsSubscribed(): bool |
||
| 352 | { |
||
| 353 | return $this->_isSubscribed; |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Places the item in the array at the key (and priority, where possible). |
||
| 358 | * List based ArrayAccess must also implement \Countable as well. |
||
| 359 | * @throws TInvalidDataValueException When the Array is an ArrayAccess (but not |
||
| 360 | * TMap nor TList), the Array isAssociative, and key is null. |
||
| 361 | * @return ?bool Was the subscription successful. If the item is already subscribed |
||
| 362 | * this will return false. If the array is not an array, this returns null; |
||
| 363 | */ |
||
| 364 | public function subscribe(): ?bool |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Removes the item from the array at the key (and priority, where possible). |
||
| 472 | * @return ?bool Was the unsubscribe successful. null when the array is not the proper |
||
| 473 | * type, and false if already unsubscribe. |
||
| 474 | */ |
||
| 475 | public function unsubscribe(): ?bool |
||
| 476 | { |
||
| 477 | if (!$this->_isSubscribed) { |
||
| 478 | return false; |
||
| 536 | } |
||
| 537 | |||
| 539 |