Complex classes like SortedSets 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 SortedSets, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | trait SortedSets |
||
| 9 | { |
||
| 10 | /* |
||
| 11 | * Available Set Operations |
||
| 12 | */ |
||
| 13 | protected $SET_OPERATIONS = ['SUM', 'MIN', 'MAX']; |
||
| 14 | |||
| 15 | public function bzPop(): bool |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Add one or more members to a sorted set or update its score if it |
||
| 22 | * already exists. |
||
| 23 | * |
||
| 24 | * See: https://redis.io/commands/zadd. |
||
| 25 | * |
||
| 26 | * @param string $key |
||
| 27 | * @param float $score |
||
| 28 | * @param mixed] $member |
||
|
|
|||
| 29 | * |
||
| 30 | * @return int 1 if the element is added. 0 otherwise. |
||
| 31 | */ |
||
| 32 | public function zAdd(string $key, float $score, $member): int |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Get the number of members in a sorted set. |
||
| 39 | * See: https://redis.io/commands/zcard. |
||
| 40 | * |
||
| 41 | * @param string $key |
||
| 42 | * |
||
| 43 | * @return int The Set's cardinality |
||
| 44 | */ |
||
| 45 | public function zCard(string $key): int |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get the number of members in a sorted set. |
||
| 52 | * |
||
| 53 | * Note: zSize is an alias for zCard and will be removed in future |
||
| 54 | * versions of phpredis. |
||
| 55 | * |
||
| 56 | * See: https://redis.io/commands/zcard. |
||
| 57 | * |
||
| 58 | * @param string $key |
||
| 59 | * |
||
| 60 | * @return int The Set's cardinality |
||
| 61 | */ |
||
| 62 | public function zSize(string $key): int |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Returns the number of elements of the sorted set stored at the specified |
||
| 69 | * key which have scores in the range [start, end]. Adding a parenthesis |
||
| 70 | * before start or end excludes it from the range. +inf and -inf are also |
||
| 71 | * valid limits. |
||
| 72 | * |
||
| 73 | * See: https://redis.io/commands/zcount. |
||
| 74 | * |
||
| 75 | * @param string $key |
||
| 76 | * @param mixed|int|string $start |
||
| 77 | * @param mixed|int|string $end |
||
| 78 | * |
||
| 79 | * @return int the size of a corresponding zRangeByScore. |
||
| 80 | */ |
||
| 81 | public function zCount(string $key, $start, $end): int |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Increments the score of a member from a sorted set by a given amount. |
||
| 88 | * |
||
| 89 | * See: https://redis.io/commands/zincrby. |
||
| 90 | * |
||
| 91 | * @param string $key |
||
| 92 | * @param float $value (double) value that will be added to the |
||
| 93 | * member's score). |
||
| 94 | * @param string $member |
||
| 95 | * |
||
| 96 | * @return float the new value |
||
| 97 | */ |
||
| 98 | public function zIncrBy(string $key, float $value, $member): float |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Creates an intersection of sorted sets given in second argument. |
||
| 105 | * The result of the union will be stored in the sorted set defined by the |
||
| 106 | * first argument. |
||
| 107 | * |
||
| 108 | * The third optional argument defines weights to apply to the sorted sets |
||
| 109 | * in input. In this case, the weights will be multiplied by the score of |
||
| 110 | * each element in the sorted set before applying the aggregation. The |
||
| 111 | * forth argument defines the AGGREGATE option which specify how the |
||
| 112 | * results of the union are aggregated. |
||
| 113 | * |
||
| 114 | * See: https://redis.io/commands/zinterstore. |
||
| 115 | * |
||
| 116 | * @param string $keyOutput |
||
| 117 | * @param array $arrayZSetKeys |
||
| 118 | * @param array $arrayWeights |
||
| 119 | * @param string $aggregateFunction Either "SUM", "MIN", or "MAX": |
||
| 120 | * defines the behaviour to use on |
||
| 121 | * duplicate entries during the |
||
| 122 | * zInterStore. |
||
| 123 | * |
||
| 124 | * @return int The number of values in the new |
||
| 125 | * sorted set. |
||
| 126 | */ |
||
| 127 | public function zInterStore( |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Creates an intersection of sorted sets given in second argument. |
||
| 155 | * The result of the union will be stored in the sorted set defined by the |
||
| 156 | * first argument. |
||
| 157 | * |
||
| 158 | * The third optional argument defines weights to apply to the sorted sets |
||
| 159 | * in input. In this case, the weights will be multiplied by the score of |
||
| 160 | * each element in the sorted set before applying the aggregation. The |
||
| 161 | * forth argument defines the AGGREGATE option which specify how the |
||
| 162 | * results of the union are aggregated. |
||
| 163 | * |
||
| 164 | * Note: zInter is an alias for zinterstore and will be removed in future |
||
| 165 | * versions of phpredis. |
||
| 166 | * |
||
| 167 | * See: https://redis.io/commands/zinterstore. |
||
| 168 | * |
||
| 169 | * @param string $keyOutput |
||
| 170 | * @param array $arrayZSetKeys |
||
| 171 | * @param array $arrayWeights |
||
| 172 | * @param string $aggregateFunction Either "SUM", "MIN", or "MAX": |
||
| 173 | * defines the behaviour to use on |
||
| 174 | * duplicate entries during the |
||
| 175 | * zInterStore. |
||
| 176 | * |
||
| 177 | * @return int The number of values in the new |
||
| 178 | * sorted set. |
||
| 179 | */ |
||
| 180 | public function zInter( |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Can pop the highest or lowest scoring members from one ZSETs. |
||
| 208 | * There are two commands (ZPOPMIN and ZPOPMAX for popping the lowest and |
||
| 209 | * highest scoring elements respectively.). |
||
| 210 | * |
||
| 211 | * See: https://redis.io/commands/zpopmin. |
||
| 212 | * See: https://redis.io/commands/zpopmax. |
||
| 213 | * |
||
| 214 | * @param string $key |
||
| 215 | * @param int|integer $count |
||
| 216 | * @param bool|boolean $max |
||
| 217 | * |
||
| 218 | * @return array Either an array with the key member and |
||
| 219 | * score of the highest or lowest element |
||
| 220 | * or an empty array if there is no element |
||
| 221 | * available. |
||
| 222 | */ |
||
| 223 | public function zPop(string $key, int $count = 1, bool $max = true): array |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Can pop the lowest scoring members from one ZSETs. |
||
| 230 | * |
||
| 231 | * See: https://redis.io/commands/zpopmin. |
||
| 232 | * |
||
| 233 | * @param string $key |
||
| 234 | * @param int|integer $count |
||
| 235 | * |
||
| 236 | * @return array Either an array with the key member and |
||
| 237 | * score of the highest or lowest element |
||
| 238 | * or an empty array if there is no element |
||
| 239 | * available. |
||
| 240 | */ |
||
| 241 | public function zPopMin(string $key, int $count = 1): array |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Can pop the highest scoring members from one ZSETs. |
||
| 248 | * |
||
| 249 | * See: https://redis.io/commands/zpopmax. |
||
| 250 | * |
||
| 251 | * @param string $key |
||
| 252 | * @param int|integer $count |
||
| 253 | * |
||
| 254 | * @return array Either an array with the key member and |
||
| 255 | * score of the highest or lowest element |
||
| 256 | * or an empty array if there is no element |
||
| 257 | * available. |
||
| 258 | */ |
||
| 259 | public function zPopMax(string $key, int $count = 1): array |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Returns a range of elements from the ordered set stored at the specified |
||
| 266 | * key, with values in the range [start, end]. |
||
| 267 | * |
||
| 268 | * Start and stop are interpreted as zero-based indices: |
||
| 269 | * 0 the first element, 1 the second ... |
||
| 270 | * -1 the last element, -2 the penultimate ... |
||
| 271 | * |
||
| 272 | * See: https://redis.io/commands/zrange. |
||
| 273 | * |
||
| 274 | * @param string $key |
||
| 275 | * @param int|integer $start |
||
| 276 | * @param int|integer $end |
||
| 277 | * @param bool|boolean $withScores |
||
| 278 | * |
||
| 279 | * @return array Array containing the values in specified range. |
||
| 280 | */ |
||
| 281 | public function zRange(string $key, int $start = 0, int $end = -1, bool $withScores = false): array |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Returns the elements of the sorted set stored at the specified key which |
||
| 288 | * have scores in the range [start,end]. Adding a parenthesis before start |
||
| 289 | * or end excludes it from the range. +inf and -inf are also valid limits. |
||
| 290 | * |
||
| 291 | * See: https://redis.io/commands/zrangebyscore. |
||
| 292 | * |
||
| 293 | * @param string $key |
||
| 294 | * @param mixed|int|string $start |
||
| 295 | * @param mixed|int|string $end |
||
| 296 | * @param array|null $options Two options are available: |
||
| 297 | * - withscores => TRUE, |
||
| 298 | * and limit => [$offset, $count] |
||
| 299 | * |
||
| 300 | * @return array Array containing the values in |
||
| 301 | * specified range. |
||
| 302 | */ |
||
| 303 | public function zRangeByScore(string $key, $start, $end, ?array $options = null): array |
||
| 317 | |||
| 318 | public function zRevRangeByScore(): bool |
||
| 322 | |||
| 323 | public function zRangeByLex(): bool |
||
| 327 | |||
| 328 | public function zRank(): bool |
||
| 332 | |||
| 333 | public function zRevRank(): bool |
||
| 337 | |||
| 338 | public function zRem(): bool |
||
| 342 | |||
| 343 | public function zDelete(): bool |
||
| 347 | |||
| 348 | public function zRemove(): bool |
||
| 352 | |||
| 353 | public function zRemRangeByRank(): bool |
||
| 357 | |||
| 358 | public function zDeleteRangeByRank(): bool |
||
| 362 | |||
| 363 | public function zRemRangeByScore(): bool |
||
| 367 | |||
| 368 | public function zDeleteRangeByScore(): bool |
||
| 372 | |||
| 373 | public function zRemoveRangeByScore(): bool |
||
| 377 | |||
| 378 | public function zRevRange(): bool |
||
| 382 | |||
| 383 | public function zScore(): bool |
||
| 387 | |||
| 388 | public function zunionstore(): bool |
||
| 392 | |||
| 393 | public function zUnion(): bool |
||
| 397 | |||
| 398 | public function zScan(): bool |
||
| 402 | } |
||
| 403 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.