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 | View Code Duplication | class PragmaticRequestQueryStringValueValidator extends RequestQueryStringValueValidator |
|
|
|
|||
| 17 | { |
||
| 18 | /** @var RawValueValidator */ |
||
| 19 | protected $rawValueValidator; |
||
| 20 | |||
| 21 | public function __construct(PragmaticRawValueValidator $rawValueValidator) |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Domain should be responsible for id format |
||
| 28 | * Exceptions are caught in order to be processed later |
||
| 29 | * |
||
| 30 | * @throws CommandMappingException If any mapping validation failed |
||
| 31 | */ |
||
| 32 | public function mustBeUuid(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): string |
||
| 33 | { |
||
| 34 | $value = $this->extractValueFromRequestQueryString($request, $queryStringKey); |
||
| 35 | |||
| 36 | return $this->rawValueValidator->mustBeUuid( |
||
| 37 | $value, |
||
| 38 | $queryStringKey, |
||
| 39 | $this, |
||
| 40 | $exceptionMessage |
||
| 41 | ); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Domain should be responsible for legit values |
||
| 46 | * Exceptions are caught in order to be processed later |
||
| 47 | * |
||
| 48 | * @throws CommandMappingException If any mapping validation failed |
||
| 49 | * @return mixed Untouched value |
||
| 50 | */ |
||
| 51 | public function mustBeInArray(ServerRequestInterface $request, array $availableValues, string $queryStringKey, string $exceptionMessage = null) |
||
| 52 | { |
||
| 53 | $value = $this->extractValueFromRequestQueryString($request, $queryStringKey); |
||
| 54 | |||
| 55 | return $this->rawValueValidator->mustBeInArray( |
||
| 56 | $value, |
||
| 57 | $availableValues, |
||
| 58 | $queryStringKey, |
||
| 59 | $this, |
||
| 60 | $exceptionMessage |
||
| 61 | ); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Domain should be responsible for string emptiness |
||
| 66 | * Exceptions are caught in order to be processed later |
||
| 67 | * |
||
| 68 | * @throws CommandMappingException If any mapping validation failed |
||
| 69 | * @return mixed Untouched value |
||
| 70 | */ |
||
| 71 | public function mustBeStringNotEmpty(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Domain should be responsible for id format |
||
| 85 | * Exceptions are caught in order to be processed later |
||
| 86 | * |
||
| 87 | * @throws CommandMappingException If any mapping validation failed |
||
| 88 | * @return mixed Untouched value |
||
| 89 | */ |
||
| 90 | public function mustHaveLengthBetween(ServerRequestInterface $request, string $queryStringKey, int $min, int $max, string $exceptionMessage = null) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Domain should be responsible for email format |
||
| 106 | * Exceptions are caught in order to be processed later |
||
| 107 | * |
||
| 108 | * @throws CommandMappingException If any mapping validation failed |
||
| 109 | * @return mixed Untouched value |
||
| 110 | */ |
||
| 111 | public function mustBeEmailAddress(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Domain should be responsible for regex validation |
||
| 125 | * Exceptions are caught in order to be processed later |
||
| 126 | * |
||
| 127 | * @throws CommandMappingException If any mapping validation failed |
||
| 128 | * @return mixed Untouched value |
||
| 129 | */ |
||
| 130 | public function mustBeValidAgainstRegex(ServerRequestInterface $request, string $pattern, string $queryStringKey, string $exceptionMessage = null) |
||
| 142 | } |
||
| 143 |
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.