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 |
||
11 | class RequestAttributeValueValidator implements UIValidatorInterface |
||
12 | { |
||
13 | /** @var RawValueValidator */ |
||
14 | protected $rawValueValidator; |
||
15 | |||
16 | public function __construct(RawValueValidator $rawValueValidator) |
||
20 | |||
21 | /** |
||
22 | * Validate if request attribute $_POST field can be mapped to a Command |
||
23 | * Throw CommandMappingException directly if any mapping issue |
||
24 | * @param ServerRequestInterface $request |
||
25 | * @param string $attributeKey |
||
26 | * |
||
27 | * @return mixed |
||
|
|||
28 | * @throws CommandMappingException If any mapping validation failed |
||
29 | */ |
||
30 | public function extractValueFromRequestAttribute(ServerRequestInterface $request, string $attributeKey) |
||
52 | |||
53 | /** |
||
54 | * Exceptions are caught in order to be processed later |
||
55 | * |
||
56 | * @throws CommandMappingException If any mapping validation failed |
||
57 | */ |
||
58 | public function mustBeString(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): string |
||
69 | |||
70 | /** |
||
71 | * Exceptions are caught in order to be processed later |
||
72 | * |
||
73 | * @throws CommandMappingException If any mapping validation failed |
||
74 | * @return string|null |
||
75 | */ |
||
76 | public function mustBeStringOrEmpty(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null) |
||
87 | |||
88 | /** |
||
89 | * Exceptions are caught in order to be processed later |
||
90 | * |
||
91 | * @throws CommandMappingException If any mapping validation failed |
||
92 | */ |
||
93 | public function mustBeBoolean(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): bool |
||
104 | |||
105 | /** |
||
106 | * Exceptions are caught in order to be processed later |
||
107 | * |
||
108 | * @throws CommandMappingException If any mapping validation failed |
||
109 | * @return bool|null |
||
110 | */ |
||
111 | public function mustBeBooleanOrEmpty(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null) |
||
122 | |||
123 | /** |
||
124 | * Exceptions are caught in order to be processed later |
||
125 | * |
||
126 | * @throws CommandMappingException If any mapping validation failed |
||
127 | */ |
||
128 | public function mustBeArray(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): array |
||
139 | |||
140 | /** |
||
141 | * Exceptions are caught in order to be processed later |
||
142 | * |
||
143 | * @throws CommandMappingException If any mapping validation failed |
||
144 | */ |
||
145 | public function mustBeFloat(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): float |
||
156 | |||
157 | /** |
||
158 | * Exceptions are caught in order to be processed later |
||
159 | * |
||
160 | * @throws CommandMappingException If any mapping validation failed |
||
161 | */ |
||
162 | public function mustBeInteger(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): int |
||
173 | |||
174 | /** |
||
175 | * Exceptions are caught in order to be processed later |
||
176 | * |
||
177 | * @throws CommandMappingException If any mapping validation failed |
||
178 | * @return int|null |
||
179 | */ |
||
180 | View Code Duplication | public function mustBeIntegerOrEmpty(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null) |
|
191 | |||
192 | /** |
||
193 | * Exceptions are caught in order to be processed later |
||
194 | * |
||
195 | * @throws CommandMappingException If any mapping validation failed |
||
196 | */ |
||
197 | public function mustBeDate(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): \DateTime |
||
208 | |||
209 | /** |
||
210 | * Exceptions are caught in order to be processed later |
||
211 | * |
||
212 | * @throws CommandMappingException If any mapping validation failed |
||
213 | */ |
||
214 | View Code Duplication | public function mustBeDateTime(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null): \DateTime |
|
225 | |||
226 | /** |
||
227 | * Exceptions are caught in order to be processed later |
||
228 | * |
||
229 | * @return \DateTime|null |
||
230 | * @throws CommandMappingException If any mapping validation failed |
||
231 | */ |
||
232 | public function mustBeDateTimeOrEmpty(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null) |
||
243 | |||
244 | /** |
||
245 | * @inheritdoc |
||
246 | */ |
||
247 | public function createUIValidationException(string $message, string $propertyPath = null): UIValidationException |
||
251 | } |
||
252 |
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.