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 |
||
| 7 | class VibratingString |
||
| 8 | { |
||
| 9 | private $frequency; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * @param float $frequency |
||
| 13 | */ |
||
| 14 | public function __construct(float $frequency) |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @param float $stringLength |
||
| 21 | * |
||
| 22 | * @return float |
||
| 23 | */ |
||
| 24 | View Code Duplication | public function getStoppedFrequency(float $stringLength = 1.0): float |
|
| 33 | |||
| 34 | /** |
||
| 35 | * @param float $stringLength |
||
| 36 | * |
||
| 37 | * @return float |
||
| 38 | */ |
||
| 39 | public function getHarmonicSoundingFrequency(float $stringLength = 1.0): float |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param float $frequency |
||
| 48 | * |
||
| 49 | * @return float |
||
| 50 | */ |
||
| 51 | View Code Duplication | public function getStringLength(float $frequency): float |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @param float $cents |
||
| 63 | * The number of cents between the open string and the stop. |
||
| 64 | * |
||
| 65 | * @return float |
||
| 66 | * The length of the remaining vibrating string. |
||
| 67 | */ |
||
| 68 | private function centsToStringLength(float $cents): float |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param float $stringLength |
||
| 75 | * |
||
| 76 | * @throws \InvalidArgumentException |
||
| 77 | * |
||
| 78 | * @return int |
||
| 79 | */ |
||
| 80 | public static function getHarmonicNumber(float $stringLength): int |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param int $limit |
||
| 92 | * |
||
| 93 | * @return float[] |
||
| 94 | */ |
||
| 95 | public static function getHarmonicSeries(int $limit): array |
||
| 105 | } |
||
| 106 |