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 |
||
| 14 | View Code Duplication | class MonthIntervalComparator |
|
|
|
|||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var MonthInterval |
||
| 18 | */ |
||
| 19 | private $interval; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @param MonthInterval $interval |
||
| 23 | */ |
||
| 24 | public function __construct(MonthInterval $interval) |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param MonthIntervalPoint $point |
||
| 31 | * |
||
| 32 | * @return bool |
||
| 33 | */ |
||
| 34 | public function contains(MonthIntervalPoint $point) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param MonthInterval $interval |
||
| 49 | * @param bool $check_interval_type |
||
| 50 | * |
||
| 51 | * @return bool |
||
| 52 | */ |
||
| 53 | public function intersect(MonthInterval $interval, $check_interval_type = true) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param MonthInterval $interval |
||
| 77 | * |
||
| 78 | * @return MonthInterval|null |
||
| 79 | */ |
||
| 80 | public function intersectInterval(MonthInterval $interval) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * The point is before the interval |
||
| 123 | * |
||
| 124 | * @param MonthIntervalPoint $point |
||
| 125 | * |
||
| 126 | * @return bool |
||
| 127 | */ |
||
| 128 | public function before(MonthIntervalPoint $point) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * The point is after the interval |
||
| 135 | * |
||
| 136 | * @param MonthIntervalPoint $point |
||
| 137 | * |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | public function after(MonthIntervalPoint $point) |
||
| 144 | } |
||
| 145 |
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.