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 BelowRule extends AbstractAtomicRule |
|
|
|
|||
| 8 | { |
||
| 9 | /** @var string operator */ |
||
| 10 | const operator = '<'; |
||
| 11 | |||
| 12 | /** @var scalar $minimum */ |
||
| 13 | protected $maximum; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @param string $field The field to apply the rule on. |
||
| 17 | * @param array $value The value the field can below to. |
||
| 18 | */ |
||
| 19 | 123 | public function __construct( $field, $maximum ) |
|
| 35 | |||
| 36 | /** |
||
| 37 | * Checks if the rule do not expect the value to be above infinity. |
||
| 38 | * |
||
| 39 | * @return bool |
||
| 40 | */ |
||
| 41 | 32 | public function hasSolution(array $contextual_options=[]) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * @deprecated getUpperLimit |
||
| 59 | */ |
||
| 60 | 111 | public function getMaximum() |
|
| 64 | |||
| 65 | /** |
||
| 66 | */ |
||
| 67 | 36 | public function getUpperLimit() |
|
| 71 | |||
| 72 | /** |
||
| 73 | */ |
||
| 74 | 111 | public function getValues() |
|
| 78 | |||
| 79 | /**/ |
||
| 80 | } |
||
| 81 |
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.