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 | View Code Duplication | class AboveRule extends AbstractAtomicRule |
|
|
|
|||
| 8 | { |
||
| 9 | /** @var string operator */ |
||
| 10 | const operator = '>'; |
||
| 11 | |||
| 12 | /** @var scalar $minimum */ |
||
| 13 | protected $minimum; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @param string $field The field to apply the rule on. |
||
| 17 | * @param array $value The value the field can above to. |
||
| 18 | */ |
||
| 19 | 140 | public function __construct( $field, $minimum ) |
|
| 35 | |||
| 36 | /** |
||
| 37 | * @deprecated getLowerLimit |
||
| 38 | */ |
||
| 39 | 130 | public function getMinimum() |
|
| 43 | |||
| 44 | /** |
||
| 45 | */ |
||
| 46 | 42 | public function getLowerLimit() |
|
| 50 | |||
| 51 | /** |
||
| 52 | * @return array |
||
| 53 | */ |
||
| 54 | 130 | public function getValues() |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Checks if the rule do not expect the value to be above infinity. |
||
| 61 | * |
||
| 62 | * @return bool |
||
| 63 | */ |
||
| 64 | 42 | public function hasSolution(array $simplification_options=[]) |
|
| 79 | |||
| 80 | /**/ |
||
| 81 | } |
||
| 82 |
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.