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 |
||
| 14 | View Code Duplication | class RequestQueryStringValueValidator implements UIValidatorInterface |
|
|
|
|||
| 15 | { |
||
| 16 | /** @var RawValueValidator */ |
||
| 17 | protected $rawValueValidator; |
||
| 18 | |||
| 19 | public function __construct(RawValueValidator $rawValueValidator) |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Validate if request query string $_GET field can be mapped to a Command |
||
| 26 | * Throw CommandMappingException directly if any mapping issue |
||
| 27 | * @param ServerRequestInterface $request |
||
| 28 | * @param string $queryStringKey |
||
| 29 | * |
||
| 30 | * @return mixed |
||
| 31 | * @throws CommandMappingException If any mapping validation failed |
||
| 32 | */ |
||
| 33 | public function extractValueFromRequestQueryString(ServerRequestInterface $request, string $queryStringKey) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Exceptions are caught in order to be processed later |
||
| 52 | * |
||
| 53 | * @throws CommandMappingException If any mapping validation failed |
||
| 54 | */ |
||
| 55 | public function mustBeString(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): string |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Exceptions are caught in order to be processed later |
||
| 69 | * |
||
| 70 | * @throws CommandMappingException If any mapping validation failed |
||
| 71 | */ |
||
| 72 | public function mustBeBoolean(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): bool |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Exceptions are caught in order to be processed later |
||
| 86 | * |
||
| 87 | * @throws CommandMappingException If any mapping validation failed |
||
| 88 | */ |
||
| 89 | public function mustBeArray(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): array |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Exceptions are caught in order to be processed later |
||
| 103 | * |
||
| 104 | * @throws CommandMappingException If any mapping validation failed |
||
| 105 | */ |
||
| 106 | public function mustBeFloat(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): float |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Exceptions are caught in order to be processed later |
||
| 120 | * |
||
| 121 | * @throws CommandMappingException If any mapping validation failed |
||
| 122 | */ |
||
| 123 | public function mustBeInteger(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): int |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Exceptions are caught in order to be processed later |
||
| 137 | * |
||
| 138 | * @throws CommandMappingException If any mapping validation failed |
||
| 139 | */ |
||
| 140 | public function mustBeDate(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): \DateTime |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Exceptions are caught in order to be processed later |
||
| 154 | * |
||
| 155 | * @throws CommandMappingException If any mapping validation failed |
||
| 156 | */ |
||
| 157 | public function mustBeDateTime(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): \DateTime |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @inheritdoc |
||
| 171 | */ |
||
| 172 | public function createUIValidationException(string $message, string $propertyPath = null): UIValidationException |
||
| 176 | } |
||
| 177 |
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.