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 | class RaCandidateSearchQuery implements HttpQuery |
||
25 | { |
||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $institution; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $commonName; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $email; |
||
40 | |||
41 | /** |
||
42 | * @var int |
||
43 | */ |
||
44 | private $pageNumber = 1; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | private $orderBy; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $orderDirection; |
||
55 | |||
56 | /** |
||
57 | * @var string[] |
||
58 | */ |
||
59 | private $secondFactorTypes = []; |
||
60 | |||
61 | /** |
||
62 | * @param string $institution |
||
63 | * @param int $pageNumber |
||
64 | */ |
||
65 | public function __construct($institution, $pageNumber) |
||
66 | { |
||
67 | $this->assertNonEmptyString($institution, 'institution'); |
||
68 | Assert\that($pageNumber) |
||
69 | ->integer('Page number must be an integer') |
||
70 | ->min(0, 'Page number must be greater than or equal to 1'); |
||
71 | |||
72 | $this->institution = $institution; |
||
73 | $this->pageNumber = $pageNumber; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param string $commonName |
||
78 | * @return $this |
||
79 | */ |
||
80 | public function setCommonName($commonName) |
||
88 | |||
89 | /** |
||
90 | * @param string $email |
||
91 | * @return $this |
||
92 | */ |
||
93 | public function setEmail($email) |
||
101 | |||
102 | /** |
||
103 | * @param array $secondFactorTypes |
||
104 | * |
||
105 | * @return void |
||
106 | */ |
||
107 | public function setSecondFactorTypes(array $secondFactorTypes) |
||
113 | |||
114 | /** |
||
115 | * @param string $orderBy |
||
116 | * @return $this |
||
117 | */ |
||
118 | public function setOrderBy($orderBy) |
||
126 | |||
127 | /** |
||
128 | * @param string $orderDirection |
||
129 | * @return $this |
||
130 | */ |
||
131 | public function setOrderDirection($orderDirection) |
||
132 | { |
||
133 | $this->assertNonEmptyString($orderDirection, 'orderDirection'); |
||
134 | Assert\that($orderDirection)->choice( |
||
135 | ['asc', 'desc', '', null], |
||
136 | "Invalid order direction, must be one of 'asc', 'desc'" |
||
137 | ); |
||
138 | |||
139 | $this->orderDirection = $orderDirection; |
||
140 | |||
141 | return $this; |
||
142 | } |
||
143 | |||
144 | public function toHttpQuery() |
||
163 | |||
164 | /** |
||
165 | * @param mixed $value |
||
166 | * @param string $parameterName |
||
167 | * @param string|null $message |
||
168 | */ |
||
169 | View Code Duplication | private function assertNonEmptyString($value, $parameterName, $message = null) |
|
179 | |||
180 | /** |
||
181 | * @param array $values |
||
182 | * @param string $parameterName |
||
183 | * |
||
184 | * @return void |
||
185 | */ |
||
186 | private function assertAllNonEmptyString(array $values, $parameterName) |
||
196 | } |
||
197 |
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.