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 RaListingSearchQuery implements HttpQuery |
||
25 | { |
||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $actorId; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $name; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $email; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | private $role; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | private $raInstitution; |
||
50 | |||
51 | /** |
||
52 | * @var string|null |
||
53 | */ |
||
54 | private $institution; |
||
55 | |||
56 | /** |
||
57 | * @var string|null |
||
58 | */ |
||
59 | private $identityId; |
||
60 | |||
61 | /** |
||
62 | * @var int |
||
63 | */ |
||
64 | private $pageNumber; |
||
65 | |||
66 | /** |
||
67 | * @var string|null |
||
68 | */ |
||
69 | private $orderBy = 'commonName'; |
||
70 | |||
71 | /** |
||
72 | * @var string|null |
||
73 | */ |
||
74 | private $orderDirection = 'asc'; |
||
75 | |||
76 | /** |
||
77 | * @param string $actorId |
||
78 | * @param int $pageNumber |
||
79 | */ |
||
80 | View Code Duplication | public function __construct($actorId, $pageNumber) |
|
|
|||
81 | { |
||
82 | $this->assertNonEmptyString($actorId, 'actorId'); |
||
83 | Assert\that($pageNumber) |
||
84 | ->integer('Page number must be an integer') |
||
85 | ->min(0, 'Page number must be greater than or equal to 1'); |
||
86 | |||
87 | $this->actorId = $actorId; |
||
88 | $this->pageNumber = $pageNumber; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param string $institution |
||
93 | * @return $this |
||
94 | */ |
||
95 | public function setInstitution($institution) |
||
103 | |||
104 | /** |
||
105 | * @param string $identityId |
||
106 | * @return RaListingSearchQuery |
||
107 | */ |
||
108 | public function setIdentityId($identityId) |
||
116 | |||
117 | /** |
||
118 | * @param string $orderBy |
||
119 | * @return RaListingSearchQuery |
||
120 | */ |
||
121 | public function setOrderBy($orderBy) |
||
129 | |||
130 | /** |
||
131 | * @param string|null $orderDirection |
||
132 | * @return RaListingSearchQuery |
||
133 | */ |
||
134 | View Code Duplication | public function setOrderDirection($orderDirection) |
|
145 | |||
146 | /** |
||
147 | * @param string $name |
||
148 | * @return RaListingSearchQuery |
||
149 | */ |
||
150 | public function setName($name) |
||
156 | |||
157 | /** |
||
158 | * @param string $email |
||
159 | * @return RaListingSearchQuery |
||
160 | */ |
||
161 | public function setEmail($email) |
||
167 | |||
168 | /** |
||
169 | * @param string $role |
||
170 | * @return RaListingSearchQuery |
||
171 | */ |
||
172 | public function setRole($role) |
||
178 | |||
179 | /** |
||
180 | * @param string $raInstitution |
||
181 | * @return RaListingSearchQuery |
||
182 | */ |
||
183 | public function setRaInstitution($raInstitution) |
||
189 | |||
190 | private function assertNonEmptyString($value, $parameterName) |
||
200 | |||
201 | View Code Duplication | public function toHttpQuery() |
|
223 | } |
||
224 |
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.