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 |
||
16 | class PragmaticRawValueValidator extends RawValueValidator |
||
17 | { |
||
18 | use UuidValidatorTrait; |
||
19 | use InArrayValidatorTrait; |
||
20 | use StringBetweenLengthValidatorTrait; |
||
21 | |||
22 | /** |
||
23 | * Domain should be responsible for string emptiness |
||
24 | * Exceptions are caught in order to be processed later |
||
25 | * @param mixed $value String not empty ? |
||
26 | * |
||
27 | * @return mixed Untouched value |
||
28 | */ |
||
29 | View Code Duplication | public function mustBeStringNotEmpty($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) |
|
56 | |||
57 | /** |
||
58 | * Domain should be responsible for email format |
||
59 | * Exceptions are caught in order to be processed later |
||
60 | * @param mixed $value Email ? |
||
61 | * |
||
62 | * @return mixed Untouched value |
||
63 | */ |
||
64 | public function mustBeEmailAddress($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) |
||
92 | |||
93 | /** |
||
94 | * Domain should be responsible for regex validation |
||
95 | * Exceptions are caught in order to be processed later |
||
96 | * @param mixed $value Valid against Regex ? |
||
97 | * @param string $pattern Regex |
||
98 | * |
||
99 | * @return mixed Untouched value |
||
100 | */ |
||
101 | View Code Duplication | public function mustBeValidAgainstRegex($value, string $pattern, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null) |
|
121 | } |
||
122 |
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.