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 | abstract class AbstractPureCommandResolver |
|
|
|||
17 | { |
||
18 | /** @var UIValidationEngine */ |
||
19 | protected $validationEngine; |
||
20 | |||
21 | /** @var RawValueValidator */ |
||
22 | protected $rawValueValidator; |
||
23 | |||
24 | /** @var RequestAttributeValueValidator */ |
||
25 | protected $attributeValueValidator; |
||
26 | |||
27 | /** @var RequestQueryStringValueValidator */ |
||
28 | protected $queryStringValueValidator; |
||
29 | |||
30 | public function __construct(UIValidationEngine $validationEngine = null, RawValueValidator $rawValueValidator = null, RequestAttributeValueValidator $attributeValueValidator = null, RequestQueryStringValueValidator $queryStringValueValidator = null) |
||
57 | |||
58 | /** |
||
59 | * @api |
||
60 | * Create a Command from a Request |
||
61 | * Perform the UI Validation (simple validation) |
||
62 | * Complex Validation will be done in the Domain |
||
63 | * @param ServerRequestInterface $request PSR7 Request |
||
64 | * |
||
65 | * @return object Immutable Command (DTO) |
||
66 | * @throws UIValidationCollectionException If any Validation fail |
||
67 | */ |
||
68 | abstract public function resolve(ServerRequestInterface $request); |
||
69 | |||
70 | /** |
||
71 | * @api |
||
72 | * Map a Command attributes from a Request into an array |
||
73 | * Perform the UI Validation (simple validation) |
||
74 | * |
||
75 | * @return mixed[] Attributes used to create the Command |
||
76 | * @throws CommandMappingException If any mapping validation failed |
||
77 | * @throws UIValidationCollectionException If any UI validation failed |
||
78 | */ |
||
79 | protected function resolveRequestAsArray(ServerRequestInterface $request): array |
||
89 | |||
90 | /** |
||
91 | * @api |
||
92 | * Resolve implementation |
||
93 | * @param ServerRequestInterface $request PSR-7 Request |
||
94 | * |
||
95 | * @return mixed[] |
||
96 | */ |
||
97 | abstract protected function validateThenMapAttributes(ServerRequestInterface $request): array; |
||
98 | } |
||
99 |
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.