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 |
||
| 12 | abstract class TimeBase implements IComparable |
||
| 13 | { |
||
| 14 | use ComparableWithPhpOperators; |
||
| 15 | |||
| 16 | /** Number of decimal digits of precision in the fractional seconds part. */ |
||
| 17 | const PRECISION = 6; |
||
| 18 | |||
| 19 | /** @var int|float */ |
||
| 20 | protected $sec; |
||
| 21 | |||
| 22 | |||
| 23 | /** |
||
| 24 | * @internal |
||
| 25 | * @param int $hour |
||
| 26 | * @param int $minute |
||
| 27 | * @param int|float $second |
||
| 28 | * @return int|float |
||
| 29 | * @throws \OutOfRangeException |
||
| 30 | */ |
||
| 31 | protected static function partsToSec($hour, $minute, $second) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @internal |
||
| 46 | * @param int $hour |
||
| 47 | * @param int $minute |
||
| 48 | * @param int|float $second |
||
| 49 | * @return int|float |
||
| 50 | * @throws \OutOfRangeException |
||
| 51 | */ |
||
| 52 | protected static function partsToSecStrict($hour, $minute, $second) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @internal |
||
| 75 | * @param int|float $timestamp |
||
| 76 | * @return int |
||
| 77 | */ |
||
| 78 | protected static function cutUnixTimestampToSec($timestamp) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @internal Only for the purpose of Ivory itself. |
||
| 95 | * @param int|float $sec |
||
| 96 | */ |
||
| 97 | protected function __construct($sec) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return int the hours part of the time (0-24) |
||
| 104 | */ |
||
| 105 | public function getHours() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return int the minutes part of the time (0-59) |
||
| 112 | */ |
||
| 113 | public function getMinutes() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @return int|float the seconds part of the time (0-59), potentially with the fractional part, if any |
||
| 120 | */ |
||
| 121 | public function getSeconds() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param Date|string|null $date the date for the resulting timestamp; |
||
| 128 | * besides a {@link Date} object, an ISO date string is accepted - see |
||
| 129 | * {@link Date::fromISOString()}; |
||
| 130 | * the given date (if any) must be finite; |
||
| 131 | * if not given the time on 1970-01-01 is returned, which is effectively the amount of |
||
| 132 | * time, in seconds, between the time this object represents and <tt>00:00:00</tt> |
||
| 133 | * @return float|int the UNIX timestamp of this time on the given day |
||
| 134 | * @throws \InvalidArgumentException if the date is infinite or if the <tt>$date</tt> string is not a valid ISO date |
||
| 135 | * string |
||
| 136 | */ |
||
| 137 | public function toUnixTimestamp($date = null) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param string $timeFmt the format string as accepted by {@link date()} |
||
| 156 | * @return string the time formatted according to <tt>$timeFmt</tt> |
||
| 157 | */ |
||
| 158 | public function format($timeFmt) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @return string the ISO representation of this time, in format <tt>HH:MM:SS[.p]</tt>; |
||
| 181 | * the fractional seconds part is only used if non-zero |
||
| 182 | */ |
||
| 183 | public function toString() |
||
| 192 | } |
||
| 193 |
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.