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:
1 | <?php |
||
24 | class ZipIterator extends \MultipleIterator implements \Countable |
||
25 | { |
||
26 | use CountableTrait; |
||
27 | use DebugInfoTrait; |
||
28 | |||
29 | // Fluent interface traits |
||
30 | use AllTrait; |
||
31 | use AnyTrait; |
||
32 | use ChainTrait; |
||
33 | use CycleTrait; |
||
34 | use FilterTrait; |
||
35 | use FirstTrait; |
||
36 | use GroupByTrait; |
||
37 | use LastTrait; |
||
38 | use MapByTrait; |
||
39 | use MapTrait; |
||
40 | use ReduceTrait; |
||
41 | use ReversedTrait; |
||
42 | use SliceTrait; |
||
43 | use SortedTrait; |
||
44 | use UniqueTrait; |
||
45 | use ZipTrait; |
||
46 | |||
47 | private $key; |
||
48 | |||
49 | 12 | public function __construct(/* \Iterator $iterable1, \Iterator $iterable2, ... */) |
|
|
|||
50 | { |
||
51 | 12 | parent::__construct(\MultipleIterator::MIT_NEED_ALL| \MultipleIterator::MIT_KEYS_NUMERIC); |
|
52 | 12 | View Code Duplication | foreach (func_get_args() as $iterable) { |
53 | 12 | if (!$iterable instanceof \Iterator) { |
|
54 | 3 | throw new \InvalidArgumentException(sprintf('Not all arguments are iterators')); |
|
55 | } |
||
56 | 9 | $this->attachIterator($iterable); |
|
57 | } |
||
58 | 9 | $this->key = 0; |
|
59 | 9 | } |
|
60 | |||
61 | 8 | public function rewind() |
|
66 | |||
67 | 4 | public function key() |
|
71 | |||
72 | 4 | public function next() |
|
77 | } |
||
78 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.