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 PragmaticRequestQueryStringValueValidator extends RequestQueryStringValueValidator |
||
| 15 | { |
||
| 16 | /** @var RawValueValidator */ |
||
| 17 | protected $rawValueValidator; |
||
| 18 | |||
| 19 | public function __construct(PragmaticRawValueValidator $rawValueValidator) |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Domain should be responsible for id format |
||
| 26 | * Exceptions are caught in order to be processed later |
||
| 27 | * |
||
| 28 | * @throws CommandMappingException If any mapping validation failed |
||
| 29 | */ |
||
| 30 | public function mustBeUuid(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null): string |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Domain should be responsible for legit values |
||
| 44 | * Exceptions are caught in order to be processed later |
||
| 45 | * |
||
| 46 | * @throws CommandMappingException If any mapping validation failed |
||
| 47 | * @return mixed Untouched value |
||
| 48 | */ |
||
| 49 | public function mustBeInArray(ServerRequestInterface $request, array $availableValues, string $queryStringKey, string $exceptionMessage = null) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Domain should be responsible for string emptiness |
||
| 64 | * Exceptions are caught in order to be processed later |
||
| 65 | * |
||
| 66 | * @throws CommandMappingException If any mapping validation failed |
||
| 67 | * @return mixed Untouched value |
||
| 68 | */ |
||
| 69 | public function mustBeStringNotEmpty(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Domain should be responsible for id format |
||
| 83 | * Exceptions are caught in order to be processed later |
||
| 84 | * |
||
| 85 | * @throws CommandMappingException If any mapping validation failed |
||
| 86 | * @return mixed Untouched value |
||
| 87 | */ |
||
| 88 | public function mustHaveLengthBetween(ServerRequestInterface $request, string $queryStringKey, int $min, int $max, string $exceptionMessage = null) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Domain should be responsible for email format |
||
| 104 | * Exceptions are caught in order to be processed later |
||
| 105 | * |
||
| 106 | * @throws CommandMappingException If any mapping validation failed |
||
| 107 | * @return mixed Untouched value |
||
| 108 | */ |
||
| 109 | public function mustBeEmailAddress(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Domain should be responsible for regex validation |
||
| 123 | * Exceptions are caught in order to be processed later |
||
| 124 | * |
||
| 125 | * @throws CommandMappingException If any mapping validation failed |
||
| 126 | * @return mixed Untouched value |
||
| 127 | */ |
||
| 128 | public function mustBeValidAgainstRegex(ServerRequestInterface $request, string $pattern, string $queryStringKey, string $exceptionMessage = null) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Exceptions are caught in order to be processed later |
||
| 143 | * |
||
| 144 | * @throws CommandMappingException If any mapping validation failed |
||
| 145 | * @return int|null |
||
| 146 | */ |
||
| 147 | View Code Duplication | public function mustBeIntegerOrEmpty(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null) |
|
| 158 | } |
||
| 159 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: