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 |
||
24 | final class SecondFactorAuditLogSearchQuery implements HttpQuery |
||
25 | { |
||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $institution; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $identityId; |
||
35 | |||
36 | /** |
||
37 | * @var string|null |
||
38 | */ |
||
39 | private $orderBy; |
||
40 | |||
41 | /** |
||
42 | * @var string|null |
||
43 | */ |
||
44 | private $orderDirection; |
||
45 | |||
46 | /** |
||
47 | * @var int |
||
48 | */ |
||
49 | private $pageNumber; |
||
50 | |||
51 | /** |
||
52 | * @param string $institution |
||
53 | * @param string $identityId |
||
54 | * @param int $pageNumber |
||
55 | */ |
||
56 | public function __construct($institution, $identityId, $pageNumber) |
||
70 | |||
71 | /** |
||
72 | * @param string $orderBy |
||
73 | */ |
||
74 | public function setOrderBy($orderBy) |
||
80 | |||
81 | /** |
||
82 | * @param string|null $orderDirection |
||
83 | */ |
||
84 | View Code Duplication | public function setOrderDirection($orderDirection) |
|
93 | |||
94 | private function assertNonEmptyString($value, $name) |
||
95 | { |
||
96 | $message = sprintf( |
||
97 | '"%s" must be a non-empty string, "%s" given', |
||
98 | $name, |
||
99 | (is_object($value) ? get_class($value) : gettype($value)) |
||
100 | ); |
||
101 | |||
102 | Assert\that($value)->string($message)->notEmpty($message); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Return the Http Query string as should be used, MUST include the '?' prefix. |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | public function toHttpQuery() |
||
122 | } |
||
123 |
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.