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 |
||
25 | class UserData extends AbstractModel implements |
||
26 | UserDataInterface |
||
27 | { |
||
28 | use TranslatorAwareTrait; |
||
29 | |||
30 | /** |
||
31 | * Client IP address of the end-user. |
||
32 | * |
||
33 | * @var integer|null |
||
34 | */ |
||
35 | private $ip; |
||
36 | |||
37 | /** |
||
38 | * Language of the end-user or source URI. |
||
39 | * |
||
40 | * @var string|null |
||
41 | */ |
||
42 | private $lang; |
||
43 | |||
44 | /** |
||
45 | * Source URL or identifier of end-user submission. |
||
46 | * |
||
47 | * @var string|null |
||
48 | */ |
||
49 | private $origin; |
||
50 | |||
51 | /** |
||
52 | * Creation timestamp of submission. |
||
53 | * |
||
54 | * @var DateTimeInterface|null |
||
55 | */ |
||
56 | private $ts; |
||
57 | |||
58 | /** |
||
59 | * Dependencies |
||
60 | * @param Container $container DI Container. |
||
61 | * @return void |
||
62 | */ |
||
63 | public function setDependencies(Container $container) |
||
69 | |||
70 | |||
71 | /** |
||
72 | * Set the client IP address. |
||
73 | * |
||
74 | * @param integer|null $ip The remote IP at object creation. |
||
75 | * @return UserDataInterface Chainable |
||
76 | */ |
||
77 | public function setIp($ip) |
||
96 | |||
97 | /** |
||
98 | * Retrieve the client IP address. |
||
99 | * |
||
100 | * @return integer|null |
||
101 | */ |
||
102 | public function ip() |
||
106 | |||
107 | /** |
||
108 | * Set the origin language. |
||
109 | * |
||
110 | * @param string $lang The language code. |
||
111 | * @throws InvalidArgumentException If the argument is not a string. |
||
112 | * @return UserDataInterface Chainable |
||
113 | */ |
||
114 | View Code Duplication | public function setLang($lang) |
|
128 | |||
129 | /** |
||
130 | * Retrieve the language. |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | public function lang() |
||
138 | |||
139 | /** |
||
140 | * Set the origin of the object submission. |
||
141 | * |
||
142 | * @param string $origin The source URL or identifier of the submission. |
||
143 | * @throws InvalidArgumentException If the argument is not a string. |
||
144 | * @return UserDataInterface Chainable |
||
145 | */ |
||
146 | View Code Duplication | public function setOrigin($origin) |
|
160 | |||
161 | /** |
||
162 | * Resolve the origin of the user data. |
||
163 | * |
||
164 | * @todo Use PSR-7 Request, if available, instead of PHP's environment variables. |
||
165 | * @return string |
||
166 | */ |
||
167 | public function resolveOrigin() |
||
198 | |||
199 | /** |
||
200 | * Retrieve the origin of the object submission. |
||
201 | * |
||
202 | * @return string |
||
203 | */ |
||
204 | public function origin() |
||
208 | |||
209 | /** |
||
210 | * Set when the object was created. |
||
211 | * |
||
212 | * @param DateTime|string|null $timestamp The timestamp at object's creation. |
||
213 | * NULL is accepted and instances of DateTimeInterface are recommended; |
||
214 | * any other value will be converted (if possible) into one. |
||
215 | * @throws InvalidArgumentException If the timestamp is invalid. |
||
216 | * @return UserDataInterface Chainable |
||
217 | */ |
||
218 | View Code Duplication | public function setTs($timestamp) |
|
246 | |||
247 | /** |
||
248 | * Retrieve the creation timestamp. |
||
249 | * |
||
250 | * @return DateTime|null |
||
251 | */ |
||
252 | public function ts() |
||
256 | |||
257 | /** |
||
258 | * Event called before _creating_ the object. |
||
259 | * |
||
260 | * @see Charcoal\Source\StorableTrait::preSave() For the "create" Event. |
||
261 | * @return boolean |
||
262 | */ |
||
263 | public function preSave() |
||
279 | } |
||
280 |
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.