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 |
||
13 | class Event extends AbstractEvent { |
||
14 | |||
15 | // Redeclaring constants used in AbstractEvent because of no clean way to |
||
16 | // have constants inherited; |
||
17 | const BAT_DAY = 'bat_day'; |
||
18 | const BAT_HOUR = 'bat_hour'; |
||
19 | const BAT_MINUTE = 'bat_minute'; |
||
20 | const BAT_HOURLY = 'bat_hourly'; |
||
21 | const BAT_DAILY = 'bat_daily'; |
||
22 | |||
23 | /** |
||
24 | * Event constructor. |
||
25 | * |
||
26 | * @param \DateTime $start_date |
||
27 | * @param \DateTime $end_date |
||
28 | * @param $unit |
||
29 | * @param $value |
||
30 | */ |
||
31 | View Code Duplication | public function __construct(\DateTime $start_date, \DateTime $end_date, UnitInterface $unit, $value = 0) { |
|
38 | |||
39 | } |
||
40 |
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.