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 $actorInstitution; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $institution; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $commonName; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | private $email; |
||
45 | |||
46 | /** |
||
47 | * @var int |
||
48 | */ |
||
49 | private $pageNumber = 1; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $orderBy; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | private $orderDirection; |
||
60 | |||
61 | /** |
||
62 | * @var string[] |
||
63 | */ |
||
64 | private $secondFactorTypes = []; |
||
65 | |||
66 | /** |
||
67 | * @param string $institution |
||
|
|||
68 | * @param int $pageNumber |
||
69 | */ |
||
70 | View Code Duplication | public function __construct($actorInstitution, $pageNumber) |
|
71 | { |
||
72 | $this->assertNonEmptyString($actorInstitution, 'actorInstitution'); |
||
73 | Assert\that($pageNumber) |
||
74 | ->integer('Page number must be an integer') |
||
75 | ->min(0, 'Page number must be greater than or equal to 1'); |
||
76 | |||
77 | $this->actorInstitution = $actorInstitution; |
||
78 | $this->pageNumber = $pageNumber; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param string $commonName |
||
83 | * @return $this |
||
84 | */ |
||
85 | public function setCommonName($commonName) |
||
93 | |||
94 | /** |
||
95 | * @param string $email |
||
96 | * @return $this |
||
97 | */ |
||
98 | public function setEmail($email) |
||
106 | |||
107 | /** |
||
108 | * @param string $institution |
||
109 | * @return $this |
||
110 | */ |
||
111 | public function setInstitution($institution) |
||
119 | |||
120 | /** |
||
121 | * @param string $role |
||
122 | * @return $this |
||
123 | */ |
||
124 | public function setRole($role) |
||
132 | |||
133 | /** |
||
134 | * @param array $secondFactorTypes |
||
135 | * |
||
136 | * @return void |
||
137 | */ |
||
138 | public function setSecondFactorTypes(array $secondFactorTypes) |
||
144 | |||
145 | /** |
||
146 | * @param string $orderBy |
||
147 | * @return $this |
||
148 | */ |
||
149 | public function setOrderBy($orderBy) |
||
157 | |||
158 | /** |
||
159 | * @param string $orderDirection |
||
160 | * @return $this |
||
161 | */ |
||
162 | public function setOrderDirection($orderDirection) |
||
174 | |||
175 | public function toHttpQuery() |
||
176 | { |
||
177 | return '?' . http_build_query( |
||
178 | array_filter( |
||
179 | [ |
||
180 | 'actorInstitution' => $this->actorInstitution, |
||
181 | 'institution' => $this->institution, |
||
182 | 'commonName' => $this->commonName, |
||
183 | 'email' => $this->email, |
||
184 | 'secondFactorTypes' => $this->secondFactorTypes, |
||
185 | 'orderBy' => $this->orderBy, |
||
186 | 'orderDirection' => $this->orderDirection, |
||
187 | 'p' => $this->pageNumber, |
||
188 | ], |
||
189 | function ($value) { |
||
190 | return !is_null($value); |
||
191 | } |
||
192 | ) |
||
193 | ); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @param mixed $value |
||
198 | * @param string $parameterName |
||
199 | * @param string|null $message |
||
200 | */ |
||
201 | View Code Duplication | private function assertNonEmptyString($value, $parameterName, $message = null) |
|
202 | { |
||
203 | $message = sprintf( |
||
204 | $message ?: '"%s" must be a non-empty string, "%s" given', |
||
205 | $parameterName, |
||
206 | (is_object($value) ? get_class($value) : gettype($value)) |
||
207 | ); |
||
208 | |||
209 | Assert\that($value)->string($message)->notEmpty($message); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * @param array $values |
||
214 | * @param string $parameterName |
||
215 | * |
||
216 | * @return void |
||
217 | */ |
||
218 | private function assertAllNonEmptyString(array $values, $parameterName) |
||
228 | } |
||
229 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.