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 | class RequestAttributeValueValidator implements UIValidatorInterface |
||
| 15 | { |
||
| 16 | /** @var RawValueValidator */ |
||
| 17 | protected $rawValueValidator; |
||
| 18 | |||
| 19 | public function __construct(RawValueValidator $rawValueValidator) |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Validate if request attribute $_POST field can be mapped to a Command |
||
| 26 | * Throw CommandMappingException directly if any mapping issue |
||
| 27 | * @param ServerRequestInterface $request |
||
| 28 | * @param string $attributeKey |
||
| 29 | * |
||
| 30 | * @return mixed |
||
|
|
|||
| 31 | * @throws CommandMappingException If any mapping validation failed |
||
| 32 | */ |
||
| 33 | public function extractValueFromRequestAttribute(ServerRequestInterface $request, string $attributeKey) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Exceptions are caught in order to be processed later |
||
| 58 | * |
||
| 59 | * @throws CommandMappingException If any mapping validation failed |
||
| 60 | */ |
||
| 61 | View Code Duplication | public function mustBeString(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): string |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Exceptions are caught in order to be processed later |
||
| 75 | * |
||
| 76 | * @throws CommandMappingException If any mapping validation failed |
||
| 77 | * @return string|null |
||
| 78 | */ |
||
| 79 | View Code Duplication | public function mustBeStringOrEmpty(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Exceptions are caught in order to be processed later |
||
| 93 | * |
||
| 94 | * @throws CommandMappingException If any mapping validation failed |
||
| 95 | */ |
||
| 96 | View Code Duplication | public function mustBeBoolean(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): bool |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Exceptions are caught in order to be processed later |
||
| 110 | * |
||
| 111 | * @throws CommandMappingException If any mapping validation failed |
||
| 112 | * @return bool|null |
||
| 113 | */ |
||
| 114 | View Code Duplication | public function mustBeBooleanOrEmpty(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Exceptions are caught in order to be processed later |
||
| 128 | * |
||
| 129 | * @throws CommandMappingException If any mapping validation failed |
||
| 130 | */ |
||
| 131 | View Code Duplication | public function mustBeArray(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): array |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Exceptions are caught in order to be processed later |
||
| 145 | * |
||
| 146 | * @throws CommandMappingException If any mapping validation failed |
||
| 147 | */ |
||
| 148 | View Code Duplication | public function mustBeFloat(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): float |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Exceptions are caught in order to be processed later |
||
| 162 | * |
||
| 163 | * @throws CommandMappingException If any mapping validation failed |
||
| 164 | */ |
||
| 165 | View Code Duplication | public function mustBeInteger(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): int |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Exceptions are caught in order to be processed later |
||
| 179 | * |
||
| 180 | * @throws CommandMappingException If any mapping validation failed |
||
| 181 | * @return int|null |
||
| 182 | */ |
||
| 183 | View Code Duplication | public function mustBeIntegerOrEmpty(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Exceptions are caught in order to be processed later |
||
| 197 | * |
||
| 198 | * @throws CommandMappingException If any mapping validation failed |
||
| 199 | */ |
||
| 200 | public function mustBeDate(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): \DateTime |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Exceptions are caught in order to be processed later |
||
| 214 | * |
||
| 215 | * @throws CommandMappingException If any mapping validation failed |
||
| 216 | */ |
||
| 217 | public function mustBeDateTime(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): \DateTime |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Exceptions are caught in order to be processed later |
||
| 231 | * |
||
| 232 | * @return \DateTime|null |
||
| 233 | * @throws CommandMappingException If any mapping validation failed |
||
| 234 | */ |
||
| 235 | View Code Duplication | public function mustBeDateTimeOrEmpty(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @inheritdoc |
||
| 249 | */ |
||
| 250 | public function createUIValidationException(string $message, string $propertyPath = null): UIValidationException |
||
| 254 | } |
||
| 255 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.