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