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 |
||
5 | class UniqueIterator extends \FilterIterator |
||
6 | { |
||
7 | protected $found; |
||
8 | protected $callback; |
||
9 | protected $method; |
||
10 | |||
11 | const ASSUME_SORTED = 1; |
||
12 | const REINDEX = 2; |
||
13 | const UNIQUE_CURRENT = [__CLASS__, 'filterCurrent']; |
||
14 | const UNIQUE_KEY = [__CLASS__, 'filterKey']; |
||
15 | |||
16 | 8 | public function __construct(\Traversable $iterator, int $flags = 0, callable $callback = self::UNIQUE_CURRENT) |
|
27 | |||
28 | 7 | public function rewind() |
|
33 | |||
34 | 3 | View Code Duplication | public function filterAssumeUnsorted($key) |
42 | |||
43 | 4 | View Code Duplication | public function filterAssumeSorted($key) |
51 | |||
52 | 4 | public static function filterCurrent($iterator) |
|
56 | |||
57 | 3 | public static function filterKey($iterator) |
|
61 | |||
62 | 7 | public function accept() |
|
68 | } |
||
69 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.