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 RaSecondFactorSearchQuery implements HttpQuery |
||
25 | { |
||
26 | const STATUS_UNVERIFIED = 'unverified'; |
||
27 | const STATUS_VERIFIED = 'verified'; |
||
28 | const STATUS_VETTED = 'vetted'; |
||
29 | const STATUS_REVOKED = 'revoked'; |
||
30 | |||
31 | /** |
||
32 | * @var string|null |
||
33 | */ |
||
34 | private $name; |
||
35 | |||
36 | /** |
||
37 | * @var string|null |
||
38 | */ |
||
39 | private $type; |
||
40 | |||
41 | /** |
||
42 | * @var string|null The second factor type's ID (eg. Yubikey public ID) |
||
43 | */ |
||
44 | private $secondFactorId; |
||
45 | |||
46 | /** |
||
47 | * @var string|null |
||
48 | */ |
||
49 | private $email; |
||
50 | |||
51 | /** |
||
52 | * @var string|null One of the STATUS_* constants. |
||
53 | */ |
||
54 | private $status; |
||
55 | |||
56 | /** |
||
57 | * @var string|null |
||
58 | */ |
||
59 | private $institution; |
||
60 | |||
61 | /** |
||
62 | * @var string|null |
||
63 | */ |
||
64 | private $orderBy; |
||
65 | |||
66 | /** |
||
67 | * @var string|null |
||
68 | */ |
||
69 | private $orderDirection; |
||
70 | |||
71 | /** |
||
72 | * @var int |
||
73 | */ |
||
74 | private $pageNumber; |
||
75 | |||
76 | /** |
||
77 | * @var string |
||
78 | */ |
||
79 | private $actorId; |
||
80 | |||
81 | /** |
||
82 | * @param int $pageNumber |
||
83 | * @param string $actorId |
||
84 | */ |
||
85 | public function __construct($pageNumber, $actorId) |
||
86 | { |
||
87 | Assert\that($pageNumber) |
||
88 | ->integer('Page number must be an integer') |
||
89 | ->min(1, 'Page number must be greater than or equal to 1'); |
||
90 | |||
91 | $this->actorId = $actorId; |
||
92 | $this->pageNumber = $pageNumber; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param string $actorId |
||
97 | * @return VerifiedSecondFactorSearchQuery |
||
|
|||
98 | */ |
||
99 | public function setActorId($actorId) |
||
107 | |||
108 | /** |
||
109 | * @return null|string |
||
110 | */ |
||
111 | public function getName() |
||
115 | |||
116 | /** |
||
117 | * @param null|string $name |
||
118 | */ |
||
119 | public function setName($name) |
||
125 | |||
126 | /** |
||
127 | * @return null|string |
||
128 | */ |
||
129 | public function getType() |
||
133 | |||
134 | /** |
||
135 | * @param null|string $type |
||
136 | */ |
||
137 | public function setType($type) |
||
143 | |||
144 | /** |
||
145 | * @return null|string |
||
146 | */ |
||
147 | public function getSecondFactorId() |
||
151 | |||
152 | /** |
||
153 | * @param null|string $secondFactorId |
||
154 | */ |
||
155 | public function setSecondFactorId($secondFactorId) |
||
161 | |||
162 | /** |
||
163 | * @return null|string |
||
164 | */ |
||
165 | public function getEmail() |
||
169 | |||
170 | /** |
||
171 | * @param null|string $email |
||
172 | */ |
||
173 | public function setEmail($email) |
||
179 | |||
180 | /** |
||
181 | * @return null|string |
||
182 | */ |
||
183 | public function getStatus() |
||
187 | |||
188 | /** |
||
189 | * @param string $status |
||
190 | */ |
||
191 | View Code Duplication | public function setStatus($status) |
|
200 | |||
201 | /** |
||
202 | * @return null|string |
||
203 | */ |
||
204 | public function getInstitution() |
||
208 | |||
209 | /** |
||
210 | * @param null|string $institution |
||
211 | */ |
||
212 | public function setInstitution($institution) |
||
216 | |||
217 | /** |
||
218 | * @param string $orderBy |
||
219 | */ |
||
220 | public function setOrderBy($orderBy) |
||
226 | |||
227 | /** |
||
228 | * @param string|null $orderDirection |
||
229 | */ |
||
230 | View Code Duplication | public function setOrderDirection($orderDirection) |
|
239 | |||
240 | private function assertNonEmptyString($value, $name) |
||
250 | |||
251 | /** |
||
252 | * Return the Http Query string as should be used, MUST include the '?' prefix. |
||
253 | * |
||
254 | * @return string |
||
255 | */ |
||
256 | View Code Duplication | public function toHttpQuery() |
|
278 | } |
||
279 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.