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 |
||
| 8 | class BoolType implements Type |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var mixed |
||
| 12 | */ |
||
| 13 | private $true_value; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var mixed |
||
| 17 | */ |
||
| 18 | private $false_value; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @param mixed $true_value |
||
| 22 | * @param mixed $false_value |
||
| 23 | */ |
||
| 24 | 1 | protected function __construct($true_value = true, $false_value = false) |
|
| 29 | |||
| 30 | /** |
||
| 31 | * Flyweight factory method. |
||
| 32 | * |
||
| 33 | * @param int|string|bool $true_value value to which boolean TRUE should be mapped |
||
| 34 | * @param int|string|bool $false_value value to which boolean FALSE should be mapped |
||
| 35 | * |
||
| 36 | * @return self |
||
| 37 | */ |
||
| 38 | 1 | public static function get($true_value = true, $false_value = false) |
|
| 50 | |||
| 51 | 1 | View Code Duplication | public function convertToSQL($value) |
| 67 | |||
| 68 | 1 | View Code Duplication | public function convertToPHP($value) |
| 84 | } |
||
| 85 |
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.