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 |
||
| 3 | class CalendarTime |
||
|
|
|||
| 4 | { |
||
| 5 | |||
| 6 | // Constants |
||
| 7 | |||
| 8 | private static $hours_max = 23; |
||
| 9 | private static $minutes_max = 59; |
||
| 10 | private static $seconds_max = 59; |
||
| 11 | |||
| 12 | // Attributes |
||
| 13 | |||
| 14 | private $hours; |
||
| 15 | private $minutes; |
||
| 16 | private $seconds; |
||
| 17 | |||
| 18 | // Constructor |
||
| 19 | |||
| 20 | public function __construct($hours = 0, $minutes = 0, $seconds = 0) |
||
| 26 | |||
| 27 | // Functions |
||
| 28 | |||
| 29 | public function getHours() |
||
| 33 | |||
| 34 | public function getMinutes() |
||
| 38 | |||
| 39 | public function getSeconds() |
||
| 43 | |||
| 44 | public function setHours($hours) |
||
| 48 | |||
| 49 | public function setMinutes($minutes) |
||
| 53 | |||
| 54 | public function setSeconds($seconds) |
||
| 58 | |||
| 59 | public function __less(CalendarTime $time) |
||
| 63 | |||
| 64 | public function add(CalendarTime $time) |
||
| 85 | |||
| 86 | // Private Functions |
||
| 87 | |||
| 88 | private function setAttribute($name, $value, $max) |
||
| 106 | } |
||
| 107 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.