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 QueueTest 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 QueueTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | final class QueueTest extends TestCase |
||
14 | { |
||
15 | private $collection; |
||
16 | private $mongoUrl; |
||
17 | private $queue; |
||
18 | |||
19 | public function setUp() |
||
32 | |||
33 | /** |
||
34 | * @test |
||
35 | * @covers ::__construct |
||
36 | * @expectedException \InvalidArgumentException |
||
37 | */ |
||
38 | public function constructWithNonStringUrl() |
||
42 | |||
43 | /** |
||
44 | * @test |
||
45 | * @covers ::ensureGetIndex |
||
46 | */ |
||
47 | public function ensureGetIndex() |
||
72 | |||
73 | /** |
||
74 | * @test |
||
75 | * @covers ::ensureGetIndex |
||
76 | * @expectedException \Exception |
||
77 | */ |
||
78 | public function ensureGetIndexWithTooLongCollectionName() |
||
86 | |||
87 | /** |
||
88 | * @test |
||
89 | * @covers ::ensureGetIndex |
||
90 | * @expectedException \InvalidArgumentException |
||
91 | */ |
||
92 | public function ensureGetIndexWithNonStringBeforeSortKey() |
||
96 | |||
97 | /** |
||
98 | * @test |
||
99 | * @covers ::ensureGetIndex |
||
100 | * @expectedException \InvalidArgumentException |
||
101 | */ |
||
102 | public function ensureGetIndexWithNonStringAfterSortKey() |
||
106 | |||
107 | /** |
||
108 | * @test |
||
109 | * @covers ::ensureGetIndex |
||
110 | * @expectedException \InvalidArgumentException |
||
111 | */ |
||
112 | public function ensureGetIndexWithBadBeforeSortValue() |
||
116 | |||
117 | /** |
||
118 | * @test |
||
119 | * @covers ::ensureGetIndex |
||
120 | * @expectedException \InvalidArgumentException |
||
121 | */ |
||
122 | public function ensureGetIndexWithBadAfterSortValue() |
||
126 | |||
127 | /** |
||
128 | * Verifies the behaviour of the Queue when it cannot create an index after 5 attempts. |
||
129 | * |
||
130 | * @test |
||
131 | * @covers ::ensureGetIndex |
||
132 | * @expectedException \Exception |
||
133 | * @expectedExceptionMessage couldnt create index after 5 attempts |
||
134 | */ |
||
135 | public function ensureIndexCannotBeCreatedAfterFiveAttempts() |
||
144 | |||
145 | /** |
||
146 | * @test |
||
147 | * @covers ::ensureCountIndex |
||
148 | */ |
||
149 | public function ensureCountIndex() |
||
163 | |||
164 | /** |
||
165 | * @test |
||
166 | * @covers ::ensureCountIndex |
||
167 | */ |
||
168 | public function ensureCountIndexWithPrefixOfPrevious() |
||
179 | |||
180 | /** |
||
181 | * @test |
||
182 | * @covers ::ensureCountIndex |
||
183 | * @expectedException \InvalidArgumentException |
||
184 | */ |
||
185 | public function ensureCountIndexWithNonStringKey() |
||
189 | |||
190 | /** |
||
191 | * @test |
||
192 | * @covers ::ensureCountIndex |
||
193 | * @expectedException \InvalidArgumentException |
||
194 | */ |
||
195 | public function ensureCountIndexWithBadValue() |
||
199 | |||
200 | /** |
||
201 | * @test |
||
202 | * @covers ::get |
||
203 | */ |
||
204 | public function getByBadQuery() |
||
213 | |||
214 | /** |
||
215 | * @test |
||
216 | * @covers ::get |
||
217 | */ |
||
218 | public function getWithNegativePollDuration() |
||
223 | |||
224 | /** |
||
225 | * @test |
||
226 | * @covers ::get |
||
227 | * @expectedException \InvalidArgumentException |
||
228 | */ |
||
229 | public function getWithNonStringKey() |
||
233 | |||
234 | /** |
||
235 | * @test |
||
236 | * @covers ::get |
||
237 | */ |
||
238 | public function getByFullQuery() |
||
252 | |||
253 | /** |
||
254 | * @test |
||
255 | * @covers ::get |
||
256 | */ |
||
257 | public function getBySubDocQuery() |
||
276 | |||
277 | /** |
||
278 | * @test |
||
279 | * @covers ::get |
||
280 | */ |
||
281 | public function getBeforeAck() |
||
294 | |||
295 | /** |
||
296 | * @test |
||
297 | * @covers ::get |
||
298 | */ |
||
299 | public function getWithCustomPriority() |
||
317 | |||
318 | /** |
||
319 | * @test |
||
320 | * @covers ::get |
||
321 | */ |
||
322 | public function getWithTimeBasedPriority() |
||
340 | |||
341 | /** |
||
342 | * @test |
||
343 | * @covers ::get |
||
344 | */ |
||
345 | public function getWithTimeBasedPriorityWithOldTimestamp() |
||
367 | |||
368 | /** |
||
369 | * @test |
||
370 | * @covers ::get |
||
371 | */ |
||
372 | public function getWait() |
||
383 | |||
384 | /** |
||
385 | * @test |
||
386 | * @covers ::get |
||
387 | */ |
||
388 | public function earliestGet() |
||
400 | |||
401 | /** |
||
402 | * @test |
||
403 | * @covers ::get |
||
404 | */ |
||
405 | public function resetStuck() |
||
440 | |||
441 | /** |
||
442 | * @test |
||
443 | * @covers ::count |
||
444 | * @expectedException \InvalidArgumentException |
||
445 | */ |
||
446 | public function countWithNonStringKey() |
||
450 | |||
451 | /** |
||
452 | * @test |
||
453 | * @covers ::count |
||
454 | */ |
||
455 | public function testCount() |
||
473 | |||
474 | /** |
||
475 | * @test |
||
476 | * @covers ::ack |
||
477 | */ |
||
478 | public function ack() |
||
491 | |||
492 | /** |
||
493 | * @test |
||
494 | * @covers ::ack |
||
495 | * @expectedException \InvalidArgumentException |
||
496 | */ |
||
497 | public function ackBadArg() |
||
501 | |||
502 | /** |
||
503 | * @test |
||
504 | * @covers ::ackSend |
||
505 | */ |
||
506 | public function ackSend() |
||
527 | |||
528 | /** |
||
529 | * Verify earliestGet with ackSend. |
||
530 | * |
||
531 | * @test |
||
532 | * @covers ::ackSend |
||
533 | * |
||
534 | * @return void |
||
535 | */ |
||
536 | public function ackSendWithEarliestGet() |
||
547 | |||
548 | /** |
||
549 | * @test |
||
550 | * @covers ::ackSend |
||
551 | * @expectedException \InvalidArgumentException |
||
552 | */ |
||
553 | public function ackSendWithWrongIdType() |
||
557 | |||
558 | /** |
||
559 | * @test |
||
560 | * @covers ::ackSend |
||
561 | * @expectedException \InvalidArgumentException |
||
562 | */ |
||
563 | public function ackSendWithNanPriority() |
||
567 | |||
568 | /** |
||
569 | * @test |
||
570 | * @covers ::ackSend |
||
571 | */ |
||
572 | public function ackSendWithHighEarliestGet() |
||
587 | |||
588 | /** |
||
589 | * @covers ::ackSend |
||
590 | */ |
||
591 | public function ackSendWithLowEarliestGet() |
||
606 | |||
607 | /** |
||
608 | * @test |
||
609 | * @covers ::requeue |
||
610 | */ |
||
611 | public function requeue() |
||
626 | |||
627 | /** |
||
628 | * @test |
||
629 | * @covers ::requeue |
||
630 | * @expectedException \InvalidArgumentException |
||
631 | */ |
||
632 | public function requeueBadArg() |
||
636 | |||
637 | /** |
||
638 | * @test |
||
639 | * @covers ::send |
||
640 | */ |
||
641 | View Code Duplication | public function send() |
|
654 | |||
655 | /** |
||
656 | * @test |
||
657 | * @covers ::send |
||
658 | * @expectedException \InvalidArgumentException |
||
659 | */ |
||
660 | public function sendWithNanPriority() |
||
664 | |||
665 | /** |
||
666 | * @test |
||
667 | * @covers ::send |
||
668 | */ |
||
669 | public function sendWithHighEarliestGet() |
||
681 | |||
682 | /** |
||
683 | * @test |
||
684 | * @covers ::send |
||
685 | */ |
||
686 | public function sendWithLowEarliestGet() |
||
698 | |||
699 | /** |
||
700 | * Verify Queue can be constructed with \MongoDB\Collection |
||
701 | * |
||
702 | * @test |
||
703 | * @covers ::__construct |
||
704 | * |
||
705 | * @return void |
||
706 | */ |
||
707 | View Code Duplication | public function constructWithCollection() |
|
723 | |||
724 | private function assertSingleMessage(array $expected) |
||
738 | } |
||
739 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.