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 |
||
9 | trait SortedSets |
||
10 | { |
||
11 | /* |
||
12 | * Available Set Operations |
||
13 | */ |
||
14 | protected $SET_OPERATIONS = ['SUM', 'MIN', 'MAX']; |
||
15 | |||
16 | public function bzPop(): bool |
||
20 | |||
21 | |||
22 | /** |
||
23 | * Add one or more members to a sorted set or update its score if it |
||
24 | * already exists. |
||
25 | * |
||
26 | * See: https://redis.io/commands/zadd. |
||
27 | * |
||
28 | * @param string $key |
||
29 | * @param float $score |
||
30 | * @param mixed] $member |
||
|
|||
31 | * |
||
32 | * @return int 1 if the element is added. 0 otherwise. |
||
33 | */ |
||
34 | public function zAdd(string $key, float $score, $member): int |
||
38 | |||
39 | |||
40 | /** |
||
41 | * Get the number of members in a sorted set. |
||
42 | * See: https://redis.io/commands/zcard. |
||
43 | * |
||
44 | * @param string $key |
||
45 | * |
||
46 | * @return int The Set's cardinality |
||
47 | */ |
||
48 | public function zCard(string $key): int |
||
52 | |||
53 | |||
54 | /** |
||
55 | * Get the number of members in a sorted set. |
||
56 | * |
||
57 | * Note: zSize is an alias for zCard and will be removed in future |
||
58 | * versions of phpredis. |
||
59 | * |
||
60 | * See: https://redis.io/commands/zcard. |
||
61 | * |
||
62 | * @param string $key |
||
63 | * |
||
64 | * @return int The Set's cardinality |
||
65 | */ |
||
66 | public function zSize(string $key): int |
||
70 | |||
71 | |||
72 | /** |
||
73 | * Returns the number of elements of the sorted set stored at the specified |
||
74 | * key which have scores in the range [start, end]. Adding a parenthesis |
||
75 | * before start or end excludes it from the range. +inf and -inf are also |
||
76 | * valid limits. |
||
77 | * |
||
78 | * See: https://redis.io/commands/zcount. |
||
79 | * |
||
80 | * @param string $key |
||
81 | * @param mixed|int|string $start |
||
82 | * @param mixed|int|string $end |
||
83 | * |
||
84 | * @return int the size of a corresponding zRangeByScore. |
||
85 | */ |
||
86 | public function zCount(string $key, $start, $end): int |
||
90 | |||
91 | |||
92 | /** |
||
93 | * Increments the score of a member from a sorted set by a given amount. |
||
94 | * |
||
95 | * See: https://redis.io/commands/zincrby. |
||
96 | * |
||
97 | * @param string $key |
||
98 | * @param float $value (double) value that will be added to the |
||
99 | * member's score). |
||
100 | * @param string $member |
||
101 | * |
||
102 | * @return float the new value |
||
103 | */ |
||
104 | public function zIncrBy(string $key, float $value, $member): float |
||
108 | |||
109 | |||
110 | /** |
||
111 | * Creates an intersection of sorted sets given in second argument. |
||
112 | * The result of the union will be stored in the sorted set defined by the |
||
113 | * first argument. |
||
114 | * |
||
115 | * The third optional argument defines weights to apply to the sorted sets |
||
116 | * in input. In this case, the weights will be multiplied by the score of |
||
117 | * each element in the sorted set before applying the aggregation. The |
||
118 | * forth argument defines the AGGREGATE option which specify how the |
||
119 | * results of the union are aggregated. |
||
120 | * |
||
121 | * See: https://redis.io/commands/zinterstore. |
||
122 | * |
||
123 | * @param string $keyOutput |
||
124 | * @param array $arrayZSetKeys |
||
125 | * @param array $arrayWeights |
||
126 | * @param string $aggregateFunction Either "SUM", "MIN", or "MAX": |
||
127 | * defines the behaviour to use on |
||
128 | * duplicate entries during the |
||
129 | * zInterStore. |
||
130 | * |
||
131 | * @return int The number of values in the new |
||
132 | * sorted set. |
||
133 | */ |
||
134 | public function zInterStore( |
||
159 | |||
160 | |||
161 | /** |
||
162 | * Creates an intersection of sorted sets given in second argument. |
||
163 | * The result of the union will be stored in the sorted set defined by the |
||
164 | * first argument. |
||
165 | * |
||
166 | * The third optional argument defines weights to apply to the sorted sets |
||
167 | * in input. In this case, the weights will be multiplied by the score of |
||
168 | * each element in the sorted set before applying the aggregation. The |
||
169 | * forth argument defines the AGGREGATE option which specify how the |
||
170 | * results of the union are aggregated. |
||
171 | * |
||
172 | * Note: zInter is an alias for zinterstore and will be removed in future |
||
173 | * versions of phpredis. |
||
174 | * |
||
175 | * See: https://redis.io/commands/zinterstore. |
||
176 | * |
||
177 | * @param string $keyOutput |
||
178 | * @param array $arrayZSetKeys |
||
179 | * @param array $arrayWeights |
||
180 | * @param string $aggregateFunction Either "SUM", "MIN", or "MAX": |
||
181 | * defines the behaviour to use on |
||
182 | * duplicate entries during the |
||
183 | * zInterStore. |
||
184 | * |
||
185 | * @return int The number of values in the new |
||
186 | * sorted set. |
||
187 | */ |
||
188 | public function zInter( |
||
213 | |||
214 | |||
215 | /** |
||
216 | * Can pop the highest or lowest scoring members from one ZSETs. |
||
217 | * There are two commands (ZPOPMIN and ZPOPMAX for popping the lowest and |
||
218 | * highest scoring elements respectively.). |
||
219 | * |
||
220 | * See: https://redis.io/commands/zpopmin. |
||
221 | * See: https://redis.io/commands/zpopmax. |
||
222 | * |
||
223 | * @param string $key |
||
224 | * @param int|integer $count |
||
225 | * @param bool|boolean $max |
||
226 | * |
||
227 | * @return array Either an array with the key member and |
||
228 | * score of the highest or lowest element |
||
229 | * or an empty array if there is no element |
||
230 | * available. |
||
231 | */ |
||
232 | public function zPop(string $key, int $count = 1, bool $max = true): array |
||
236 | |||
237 | |||
238 | /** |
||
239 | * Can pop the lowest scoring members from one ZSETs. |
||
240 | * |
||
241 | * See: https://redis.io/commands/zpopmin. |
||
242 | * |
||
243 | * @param string $key |
||
244 | * @param int|integer $count |
||
245 | * |
||
246 | * @return array Either an array with the key member and |
||
247 | * score of the highest or lowest element |
||
248 | * or an empty array if there is no element |
||
249 | * available. |
||
250 | */ |
||
251 | public function zPopMin(string $key, int $count = 1): array |
||
255 | |||
256 | |||
257 | /** |
||
258 | * Can pop the highest scoring members from one ZSETs. |
||
259 | * |
||
260 | * See: https://redis.io/commands/zpopmax. |
||
261 | * |
||
262 | * @param string $key |
||
263 | * @param int|integer $count |
||
264 | * |
||
265 | * @return array Either an array with the key member and |
||
266 | * score of the highest or lowest element |
||
267 | * or an empty array if there is no element |
||
268 | * available. |
||
269 | */ |
||
270 | public function zPopMax(string $key, int $count = 1): array |
||
274 | |||
275 | |||
276 | /** |
||
277 | * Returns a range of elements from the ordered set stored at the specified |
||
278 | * key, with values in the range [start, end]. |
||
279 | * |
||
280 | * Start and stop are interpreted as zero-based indices: |
||
281 | * 0 the first element, 1 the second ... |
||
282 | * -1 the last element, -2 the penultimate ... |
||
283 | * |
||
284 | * See: https://redis.io/commands/zrange. |
||
285 | * |
||
286 | * @param string $key |
||
287 | * @param int|integer $start |
||
288 | * @param int|integer $end |
||
289 | * @param bool|boolean $withScores |
||
290 | * |
||
291 | * @return array Array containing the values in specified range. |
||
292 | */ |
||
293 | public function zRange(string $key, int $start = 0, int $end = -1, bool $withScores = false): array |
||
297 | |||
298 | |||
299 | /** |
||
300 | * Returns the elements of the sorted set stored at the specified key which |
||
301 | * have scores in the range [start,end]. Adding a parenthesis before start |
||
302 | * or end excludes it from the range. +inf and -inf are also valid limits. |
||
303 | * |
||
304 | * See: https://redis.io/commands/zrangebyscore. |
||
305 | * |
||
306 | * @param string $key |
||
307 | * @param mixed|int|string $start |
||
308 | * @param mixed|int|string $end |
||
309 | * @param array|null $options Two options are available: |
||
310 | * - withscores => TRUE, |
||
311 | * and limit => [$offset, $count] |
||
312 | * |
||
313 | * @return array Array containing the values in |
||
314 | * specified range. |
||
315 | */ |
||
316 | public function zRangeByScore(string $key, $start, $end, ?array $options = null): array |
||
330 | |||
331 | |||
332 | /** |
||
333 | * Returns the elements of the sorted set stored at the specified key which |
||
334 | * have scores in the range [start,end]. Adding a parenthesis before start |
||
335 | * or end excludes it from the range. +inf and -inf are also valid limits. |
||
336 | * |
||
337 | * zRevRangeByScore returns the same items in reverse order, when the start |
||
338 | * and end parameters are swapped. |
||
339 | * |
||
340 | * See: https://redis.io/commands/zrevrangebyscore. |
||
341 | * |
||
342 | * @param string $key |
||
343 | * @param mixed|int|string $start |
||
344 | * @param mixed|int|string $end |
||
345 | * @param array|null $options Two options are available: |
||
346 | * - withscores => TRUE, |
||
347 | * and limit => [$offset, $count] |
||
348 | * |
||
349 | * @return array Array containing the values in |
||
350 | * specified range. |
||
351 | */ |
||
352 | public function zRevRangeByScore(string $key, $start, $end, ?array $options = null): array |
||
366 | |||
367 | |||
368 | /** |
||
369 | * Returns a lexicographical range of members in a sorted set, assuming the |
||
370 | * members have the same score. The min and max values are required to start |
||
371 | * with '(' (exclusive), '[' (inclusive), or be exactly the values |
||
372 | * '-' (negative inf) or '+' (positive inf). |
||
373 | * |
||
374 | * The command must be called with either three or five arguments or will |
||
375 | * return FALSE. |
||
376 | * |
||
377 | * See: https://redis.io/commands/zrangebylex |
||
378 | * |
||
379 | * @param string $key The ZSET you wish to run against |
||
380 | * @param mixed|string $min The minimum alphanumeric value you wish to get |
||
381 | * @param mixed|string $max The maximum alphanumeric value you wish to get |
||
382 | * @param int|null $offset Optional argument if you wish to start |
||
383 | * somewhere other than the first element. |
||
384 | * @param int|null $limit Optional argument if you wish to limit the |
||
385 | * number of elements returned. |
||
386 | * |
||
387 | * @return array Array containing the values in the specified |
||
388 | * range. |
||
389 | */ |
||
390 | public function zRangeByLex(string $key, $min, $max, ?int $offset = null, ?int $limit = null): array |
||
406 | |||
407 | |||
408 | /** |
||
409 | * Returns the rank of a given member in the specified sorted set, starting |
||
410 | * at 0 for the item with the smallest score. |
||
411 | * |
||
412 | * See: https://redis.io/commands/zrank. |
||
413 | * |
||
414 | * @param string $key The ZSET you wish to run against |
||
415 | * @param mixed|string|int|float $member The member to look for |
||
416 | * |
||
417 | * @return int The member's rank position |
||
418 | */ |
||
419 | public function zRank(string $key, $member): int |
||
423 | |||
424 | |||
425 | /** |
||
426 | * Returns the rank of a given member in the specified sorted set, starting |
||
427 | * at 0 for the item with the smallest score. |
||
428 | * |
||
429 | * zRevRank starts at 0 for the item with the largest score. |
||
430 | * |
||
431 | * See: https://redis.io/commands/zrevrank. |
||
432 | * |
||
433 | * @param string $key The ZSET you wish to run against |
||
434 | * @param mixed|string|int|float $member The member to look for |
||
435 | * |
||
436 | * @return int The member's rank position |
||
437 | */ |
||
438 | public function zRevRank(string $key, $member): int |
||
442 | |||
443 | |||
444 | /** |
||
445 | * Delete one or more members from a sorted set. |
||
446 | * |
||
447 | * See: https://redis.io/commands/zrem. |
||
448 | * |
||
449 | * @param string $key The ZSET you wish to run against |
||
450 | * @param splay $members Member(s) to be removed from the ZSET |
||
451 | * |
||
452 | * @return int The number of members deleted. |
||
453 | */ |
||
454 | public function zRem(string $key, ...$members): int |
||
458 | |||
459 | |||
460 | /** |
||
461 | * Delete one or more members from a sorted set. |
||
462 | * |
||
463 | * Note: zDelete and zRemove are an alias for zRem and will be removed |
||
464 | * in future versions of phpredis. |
||
465 | * |
||
466 | * See: https://redis.io/commands/zrem. |
||
467 | * |
||
468 | * @param string $key The ZSET you wish to run against |
||
469 | * @param splay $members Member(s) to be removed from the ZSET |
||
470 | * |
||
471 | * @return int The number of members deleted. |
||
472 | */ |
||
473 | public function zDelete(string $key, ...$members): int |
||
477 | |||
478 | |||
479 | /** |
||
480 | * Delete one or more members from a sorted set. |
||
481 | * |
||
482 | * Note: zDelete and zRemove are an alias for zRem and will be removed |
||
483 | * in future versions of phpredis. |
||
484 | * |
||
485 | * See: https://redis.io/commands/zrem. |
||
486 | * |
||
487 | * @param string $key The ZSET you wish to run against |
||
488 | * @param splay $members Member(s) to be removed from the ZSET |
||
489 | * |
||
490 | * @return int The number of members deleted. |
||
491 | */ |
||
492 | public function zRemove(string $key, ...$members): int |
||
496 | |||
497 | |||
498 | /** |
||
499 | * Deletes the elements of the sorted set stored at the specified key which |
||
500 | * have rank in the range [start,end]. |
||
501 | * |
||
502 | * See: https://redis.io/commands/zremrangebyrank. |
||
503 | * |
||
504 | * @param string $key The ZSET you wish to run against |
||
505 | * @param int $start |
||
506 | * @param int $end |
||
507 | * |
||
508 | * @return int The number of values deleted from the sorted set |
||
509 | */ |
||
510 | public function zRemRangeByRank(string $key, int $start, int $end): int |
||
518 | |||
519 | |||
520 | /** |
||
521 | * Deletes the elements of the sorted set stored at the specified key which |
||
522 | * have rank in the range [start,end]. |
||
523 | * |
||
524 | * Note: zDeleteRangeByRank is an alias for zRemRangeByRank and will be |
||
525 | * removed in future versions of phpredis. |
||
526 | * |
||
527 | * See: https://redis.io/commands/zremrangebyrank. |
||
528 | * |
||
529 | * @param string $key The ZSET you wish to run against |
||
530 | * @param int $start |
||
531 | * @param int $end |
||
532 | * |
||
533 | * @return int The number of values deleted from the sorted set |
||
534 | */ |
||
535 | public function zDeleteRangeByRank(string $key, int $start, int $end): int |
||
543 | |||
544 | |||
545 | /** |
||
546 | * Deletes the elements of the sorted set stored at the specified key which |
||
547 | * have scores in the range [start,end]. |
||
548 | * |
||
549 | * See: https://redis.io/commands/zremrangebyscore. |
||
550 | * |
||
551 | * @param string $key The ZSET you wish to run against |
||
552 | * @param mixed|float|string $start double or "+inf" or "-inf" string |
||
553 | * @param mixed|float|string $end double or "+inf" or "-inf" string |
||
554 | * |
||
555 | * @return The number of values deleted from the sorted set |
||
556 | */ |
||
557 | public function zRemRangeByScore(string $key, $start, $end): int |
||
569 | |||
570 | |||
571 | /** |
||
572 | * Deletes the elements of the sorted set stored at the specified key which |
||
573 | * have scores in the range [start,end]. |
||
574 | * |
||
575 | * Note: zDeleteRangeByScore and zRemoveRangeByScore are an alias for |
||
576 | * zRemRangeByScore and will be removed in future versions of phpredis. |
||
577 | * |
||
578 | * See: https://redis.io/commands/zremrangebyscore. |
||
579 | * |
||
580 | * @param string $key The ZSET you wish to run against |
||
581 | * @param mixed|float|string $start double or "+inf" or "-inf" string |
||
582 | * @param mixed|float|string $end double or "+inf" or "-inf" string |
||
583 | * |
||
584 | * @return The number of values deleted from the sorted set |
||
585 | */ |
||
586 | public function zDeleteRangeByScore(string $key, $start, $end): int |
||
598 | |||
599 | |||
600 | /** |
||
601 | * Deletes the elements of the sorted set stored at the specified key which |
||
602 | * have scores in the range [start,end]. |
||
603 | * |
||
604 | * Note: zDeleteRangeByScore and zRemoveRangeByScore are an alias for |
||
605 | * zRemRangeByScore and will be removed in future versions of phpredis. |
||
606 | * |
||
607 | * See: https://redis.io/commands/zremrangebyscore. |
||
608 | * |
||
609 | * @param string $key The ZSET you wish to run against |
||
610 | * @param mixed|float|string $start double or "+inf" or "-inf" string |
||
611 | * @param mixed|float|string $end double or "+inf" or "-inf" string |
||
612 | * |
||
613 | * @return The number of values deleted from the sorted set |
||
614 | */ |
||
615 | public function zRemoveRangeByScore(string $key, $start, $end): int |
||
627 | |||
628 | |||
629 | /** |
||
630 | * Returns the elements of the sorted set stored at the specified key in |
||
631 | * the range [start, end] in reverse order. start and stop are interpreted |
||
632 | * as zero-based indices: |
||
633 | * 0 the first element, 1 the second ... |
||
634 | * -1 the last element, -2 the penultimate ... |
||
635 | * |
||
636 | * See: https://redis.io/commands/zrevrange. |
||
637 | * |
||
638 | * @param string $key The ZSET you wish to run against |
||
639 | * @param int $start 0 the first element, 1 the second ... |
||
640 | * @param int $end -1 the last element, -2 the penultimate ... |
||
641 | * @param bool|boolean $withScores false No Scores | true Scores in |
||
642 | * associative array |
||
643 | * |
||
644 | * @return array Array containing the values in |
||
645 | * specified range. |
||
646 | */ |
||
647 | public function zRevRange(string $key, int $start = 0, int $end = -1, bool $withScores = false): array |
||
651 | |||
652 | |||
653 | /** |
||
654 | * Returns the score of a given member in the specified sorted set. |
||
655 | * |
||
656 | * @param string $key The ZSET you wish to run against. |
||
657 | * @param mixed|string|float|int $member |
||
658 | * |
||
659 | * @return float The Score associated with the Member. |
||
660 | */ |
||
661 | public function zScore(string $key, $member): float |
||
665 | |||
666 | |||
667 | /** |
||
668 | * Creates an union of sorted sets given in second argument. |
||
669 | * The result of the union will be stored in the sorted set defined by the |
||
670 | * first argument. |
||
671 | * |
||
672 | * The third optional argument defines weights to apply to the sorted sets |
||
673 | * in input. In this case, the weights will be multiplied by the score of |
||
674 | * each element in the sorted set before applying the aggregation. The |
||
675 | * forth argument defines the AGGREGATE option which specify how the |
||
676 | * results of the union are aggregated. |
||
677 | * |
||
678 | * See: https://redis.io/commands/zunionstore. |
||
679 | * |
||
680 | * @param string $keyOutput The ZSET you wish to store the result. |
||
681 | * @param array $arrayZSetKeys |
||
682 | * @param array $arrayWeights |
||
683 | * @param string $aggregateFunction Either "SUM", "MIN", or "MAX": |
||
684 | * defines the behaviour to use on |
||
685 | * duplicate entries during the |
||
686 | * zunionstore. |
||
687 | * |
||
688 | * @return [type] |
||
689 | */ |
||
690 | public function zUnionStore( |
||
714 | |||
715 | |||
716 | /** |
||
717 | * Creates an union of sorted sets given in second argument. |
||
718 | * The result of the union will be stored in the sorted set defined by the |
||
719 | * first argument. |
||
720 | * |
||
721 | * The third optional argument defines weights to apply to the sorted sets |
||
722 | * in input. In this case, the weights will be multiplied by the score of |
||
723 | * each element in the sorted set before applying the aggregation. The |
||
724 | * forth argument defines the AGGREGATE option which specify how the |
||
725 | * results of the union are aggregated. |
||
726 | * |
||
727 | * Note: zUnion is an alias for zunionstore and will be removed in future |
||
728 | * versions of phpredis. |
||
729 | * |
||
730 | * See: https://redis.io/commands/zunionstore. |
||
731 | * |
||
732 | * @param string $keyOutput The ZSET you wish to store the result. |
||
733 | * @param array $arrayZSetKeys |
||
734 | * @param array $arrayWeights |
||
735 | * @param string $aggregateFunction Either "SUM", "MIN", or "MAX": |
||
736 | * defines the behaviour to use on |
||
737 | * duplicate entries during the |
||
738 | * zunionstore. |
||
739 | * |
||
740 | * @return [type] |
||
741 | */ |
||
742 | public function zUnion( |
||
766 | |||
767 | |||
768 | /** |
||
769 | * Scan a sorted set for members, with optional pattern and count. |
||
770 | * |
||
771 | * @param string $key The ZSET you wish to run against. |
||
772 | * @param int &$iterator Long (reference), initialized to NULL |
||
773 | * @param string $pattern String (optional), the pattern to match |
||
774 | * @param int|integer $count How many keys to return per iteration |
||
775 | * (Redis might return a different number). |
||
776 | * |
||
777 | * @return Array, boolean PHPRedis will return matching keys from Redis, |
||
778 | * or FALSE when iteration is complete |
||
779 | */ |
||
780 | public function zScan(string $key, ?int &$iterator, string $pattern = '*', int $count = 10) |
||
784 | |||
785 | |||
786 | /** |
||
787 | * ======================================================================== |
||
788 | * H E L P E R M E T H O D S |
||
789 | * ======================================================================== |
||
790 | */ |
||
791 | |||
792 | |||
793 | /** |
||
794 | * Validate Lex Params |
||
795 | * |
||
796 | * @param splat $params |
||
797 | * |
||
798 | * @return bool |
||
799 | */ |
||
800 | protected function _validateLexParams(...$params) |
||
804 | } |
||
805 |
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.