| Total Complexity | 49 |
| Total Lines | 278 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like TPriorityCollectionTrait 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 TPriorityCollectionTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 62 | trait TPriorityCollectionTrait |
||
| 63 | { |
||
| 64 | /** |
||
| 65 | * @var bool indicates if the _d is currently ordered. |
||
| 66 | */ |
||
| 67 | protected bool $_o = false; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var null|array cached flattened internal data storage |
||
| 71 | */ |
||
| 72 | protected ?array $_fd = null; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var ?string the default priority of items without specified priorities |
||
| 76 | */ |
||
| 77 | private ?string $_dp = null; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var ?int the precision of the numeric priorities within this priority list. |
||
| 81 | */ |
||
| 82 | private ?int $_p = null; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return numeric gets the default priority of inserted items without a specified priority |
||
|
|
|||
| 86 | */ |
||
| 87 | public function getDefaultPriority() |
||
| 88 | { |
||
| 89 | if ($this->_dp === null) { |
||
| 90 | $this->_dp = '10'; |
||
| 91 | } |
||
| 92 | return $this->_dp; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * This must be called internally or when instantiated. |
||
| 97 | * @param numeric $value sets the default priority of inserted items without a specified priority |
||
| 98 | */ |
||
| 99 | public function setDefaultPriority($value) |
||
| 100 | { |
||
| 101 | if ($value === $this->_dp) { |
||
| 102 | return; |
||
| 103 | } |
||
| 104 | if($this->_dp === null || Prado::isCallingSelf()) { |
||
| 105 | $this->_dp = (string) round(TPropertyValue::ensureFloat($value), $this->getPrecision()); |
||
| 106 | } else { |
||
| 107 | throw new TInvalidOperationException('prioritytrait_no_set_default_priority'); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @return int The precision of numeric priorities, defaults to 8 |
||
| 113 | */ |
||
| 114 | public function getPrecision(): int |
||
| 115 | { |
||
| 116 | if ($this->_p === null) { |
||
| 117 | $this->_p = 8; |
||
| 118 | } |
||
| 119 | return $this->_p; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * This must be called internally or when instantiated. |
||
| 124 | * This resets the array priorities to the new precision and adjusts |
||
| 125 | * the DefaultPriority to the new precision as well. |
||
| 126 | * @param int $value The precision of numeric priorities. |
||
| 127 | */ |
||
| 128 | public function setPrecision($value): void |
||
| 129 | { |
||
| 130 | if ($value === $this->_p) { |
||
| 131 | return; |
||
| 132 | } |
||
| 133 | if($this->_p !== null && !Prado::isCallingSelf()) { |
||
| 134 | throw new TInvalidOperationException('prioritytrait_no_set_precision'); |
||
| 135 | } |
||
| 136 | $this->_p = TPropertyValue::ensureInteger($value); |
||
| 137 | $this->setDefaultPriority($this->_dp); |
||
| 138 | $_d = []; |
||
| 139 | foreach(array_keys($this->_d) as $priority) { |
||
| 140 | $newPriority = $this->ensurePriority($priority); |
||
| 141 | if (array_key_exists($newPriority, $_d)) { |
||
| 142 | if ($this->getPriorityCombineStyle()) { |
||
| 143 | $_d[$newPriority] = array_merge($_d[$newPriority], $this->_d[$priority]); |
||
| 144 | } else { |
||
| 145 | $_d[$newPriority] = array_replace($_d[$newPriority], $this->_d[$priority]); |
||
| 146 | } |
||
| 147 | } else { |
||
| 148 | $_d[$newPriority] = $this->_d[$priority]; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | $this->_d = $_d; |
||
| 152 | $this->_fd = null; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Taken an input Priority and ensures its value. |
||
| 157 | * Sets the default $priority when none is set, |
||
| 158 | * then rounds to the proper precision and makes |
||
| 159 | * into a string. |
||
| 160 | * @param mixed $priority |
||
| 161 | * @return string the priority in string format |
||
| 162 | */ |
||
| 163 | protected function ensurePriority($priority): string |
||
| 164 | { |
||
| 165 | if ($priority === null || !is_numeric($priority)) { |
||
| 166 | $priority = $this->getDefaultPriority(); |
||
| 167 | } |
||
| 168 | return (string) round((float) $priority, $this->getPrecision()); |
||
| 169 | } |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * This orders the priority list internally. |
||
| 174 | */ |
||
| 175 | protected function sortPriorities(): void |
||
| 176 | { |
||
| 177 | if (!$this->_o) { |
||
| 178 | ksort($this->_d, SORT_NUMERIC); |
||
| 179 | $this->_o = true; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * This flattens the priority list into a flat array [0,...,n-1] (with array_merge) |
||
| 185 | * and a priority map into a single flat map of keys and values (with array_replace). |
||
| 186 | */ |
||
| 187 | protected function flattenPriorities(): void |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * This returns a list of the priorities within this list, ordered lowest to highest. |
||
| 206 | * @return array the array of priority numerics in decreasing priority order |
||
| 207 | */ |
||
| 208 | public function getPriorities(): array |
||
| 209 | { |
||
| 210 | $this->sortPriorities(); |
||
| 211 | return array_keys($this->_d); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Gets the number of items at a priority within the list |
||
| 216 | * @param null|numeric $priority optional priority at which to count items. if no parameter, it will be set to the default {@link getDefaultPriority} |
||
| 217 | * @return int the number of items in the list at the specified priority |
||
| 218 | */ |
||
| 219 | public function getPriorityCount($priority = null) |
||
| 220 | { |
||
| 221 | $priority = $this->ensurePriority($priority); |
||
| 222 | if (empty($this->_d[$priority])) { |
||
| 223 | return 0; |
||
| 224 | } |
||
| 225 | return count($this->_d[$priority]); |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Returns an iterator for traversing the items in the list. |
||
| 230 | * This method is required by the interface \IteratorAggregate. |
||
| 231 | * @return \Iterator an iterator for traversing the items in the list. |
||
| 232 | */ |
||
| 233 | public function getIterator(): \Iterator |
||
| 234 | { |
||
| 235 | $this->flattenPriorities(); |
||
| 236 | return new \ArrayIterator($this->_fd); |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Gets all the items at a specific priority. |
||
| 241 | * @param null|numeric $priority priority of the items to get. Defaults to null, filled in with the default priority, if left blank. |
||
| 242 | * @return ?array all items at priority in index order, null if there are no items at that priority |
||
| 243 | */ |
||
| 244 | public function itemsAtPriority($priority = null): ?array |
||
| 245 | { |
||
| 246 | $priority = $this->ensurePriority($priority); |
||
| 247 | return $this->_d[$priority] ?? null; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return array the priority list of items in array |
||
| 252 | */ |
||
| 253 | public function toArray(): array |
||
| 254 | { |
||
| 255 | $this->flattenPriorities(); |
||
| 256 | return $this->_fd; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @return array the array of priorities keys with values of arrays of items. The priorities are sorted so important priorities, lower numerics, are first. |
||
| 261 | */ |
||
| 262 | public function toPriorityArray(): array |
||
| 263 | { |
||
| 264 | $this->sortPriorities(); |
||
| 265 | return $this->_d; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Combines the map elements which have a priority below the parameter value |
||
| 270 | * @param numeric $priority the cut-off priority. All items of priority less than this are returned. |
||
| 271 | * @param bool $inclusive whether or not the input cut-off priority is inclusive. Default: false, not inclusive. |
||
| 272 | * @return array the array of priorities keys with values of arrays of items that are below a specified priority. |
||
| 273 | * The priorities are sorted so important priorities, lower numerics, are first. |
||
| 274 | */ |
||
| 275 | public function toArrayBelowPriority($priority, bool $inclusive = false): array |
||
| 276 | { |
||
| 277 | $this->sortPriorities(); |
||
| 278 | $items = []; |
||
| 279 | foreach ($this->_d as $itemspriority => $itemsatpriority) { |
||
| 280 | if ((!$inclusive && $itemspriority >= $priority) || $itemspriority > $priority) { |
||
| 281 | break; |
||
| 282 | } |
||
| 283 | $items[] = $itemsatpriority; |
||
| 284 | } |
||
| 285 | if(empty($items)) { |
||
| 286 | return []; |
||
| 287 | } |
||
| 288 | if ($this->getPriorityCombineStyle()) { |
||
| 289 | return array_merge(...$items); |
||
| 290 | } else { |
||
| 291 | return array_replace(...$items); |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Combines the map elements which have a priority above the parameter value |
||
| 297 | * @param numeric $priority the cut-off priority. All items of priority greater than this are returned. |
||
| 298 | * @param bool $inclusive whether or not the input cut-off priority is inclusive. Default: true, inclusive. |
||
| 299 | * @return array the array of priorities keys with values of arrays of items that are above a specified priority. |
||
| 300 | * The priorities are sorted so important priorities, lower numerics, are first. |
||
| 301 | */ |
||
| 302 | public function toArrayAbovePriority($priority, bool $inclusive = true): array |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Returns an array with the names of all variables of this object that should NOT be serialized |
||
| 324 | * because their value is the default one or useless to be cached for the next page loads. |
||
| 325 | * Reimplement in derived classes to add new variables, but remember to also to call the parent |
||
| 326 | * implementation first. |
||
| 327 | * @param array $exprops by reference |
||
| 328 | */ |
||
| 329 | protected function _priorityZappableSleepProps(&$exprops) |
||
| 340 | } |
||
| 341 | } |
||
| 342 | } |
||
| 343 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths