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 |
||
| 18 | class RawValueValidator implements UIValidatorInterface |
||
| 19 | { |
||
| 20 | use BooleanValidatorTrait; |
||
| 21 | use IntegerValidatorTrait; |
||
| 22 | use FloatValidatorTrait; |
||
| 23 | |||
| 24 | /** @var UIValidationEngine */ |
||
| 25 | protected $validationEngine; |
||
| 26 | |||
| 27 | public function __construct(UIValidationEngine $validationEngine) |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Exceptions are caught in order to be processed later |
||
| 34 | * @param mixed $value String ? |
||
| 35 | * |
||
| 36 | * @return mixed Untouched value |
||
| 37 | */ |
||
| 38 | View Code Duplication | public function mustBeString($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) |
|
| 53 | |||
| 54 | /** |
||
| 55 | * Exceptions are caught in order to be processed later |
||
| 56 | * @param mixed $value Array ? |
||
| 57 | * |
||
| 58 | * @return mixed Untouched value |
||
| 59 | */ |
||
| 60 | View Code Duplication | public function mustBeArray($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Exceptions are caught in order to be processed later |
||
| 78 | * @param mixed $value Date Y-m-d ? |
||
| 79 | * |
||
| 80 | * @return mixed Untouched value |
||
| 81 | */ |
||
| 82 | View Code Duplication | public function mustBeDate($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Exceptions are caught in order to be processed later |
||
| 104 | * @param mixed $value Date Y-m-d\TH:i:sP (RFC3339). Ex: 2016-06-01T00:00:00+00:00 ? |
||
| 105 | * |
||
| 106 | * @return mixed Untouched value |
||
| 107 | */ |
||
| 108 | View Code Duplication | public function mustBeDateTime($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @inheritdoc |
||
| 130 | */ |
||
| 131 | public function createUIValidationException(string $message, string $propertyPath = null): UIValidationException |
||
| 135 | } |
||
| 136 |
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.