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