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 |
||
11 | class User extends LazyResource |
||
12 | { |
||
13 | /** |
||
14 | * The primary id or some other id that can be used to fetch user information. |
||
15 | * |
||
16 | * @var string |
||
17 | */ |
||
18 | public $id; |
||
19 | |||
20 | /** |
||
21 | * @var UserIdentifiers |
||
22 | */ |
||
23 | protected $_identifiers; |
||
24 | |||
25 | /** |
||
26 | * @var Loans |
||
27 | */ |
||
28 | public $loans; |
||
29 | |||
30 | /** |
||
31 | * @var Fees |
||
32 | */ |
||
33 | public $fees; |
||
34 | |||
35 | /** |
||
36 | * @var Requests |
||
37 | */ |
||
38 | public $requests; |
||
39 | |||
40 | /** |
||
41 | * User constructor. |
||
42 | * |
||
43 | * @param Client $client |
||
44 | * @param string $id |
||
45 | */ |
||
46 | public function __construct(Client $client, $id) |
||
54 | |||
55 | /** |
||
56 | * Get the user id the object was constructed with. This might or might not be the primary id. |
||
57 | * The only usefulness of this method over getPrimaryId() is that it will not trigger loading of the full object. |
||
58 | * |
||
59 | * @return string |
||
60 | */ |
||
61 | public function getUserId() |
||
65 | |||
66 | /** |
||
67 | * Get the primary id. No need to load the full record for this. |
||
68 | * |
||
69 | * @return string|null |
||
70 | */ |
||
71 | public function getPrimaryId() |
||
75 | |||
76 | /** |
||
77 | * Get the user identifiers. |
||
78 | * |
||
79 | * @return UserIdentifiers |
||
80 | */ |
||
81 | public function getIdentifiers() |
||
85 | |||
86 | /** |
||
87 | * Get the user's preferred SMS number. |
||
88 | * |
||
89 | * @return string|null |
||
90 | */ |
||
91 | View Code Duplication | public function getSmsNumber() |
|
103 | |||
104 | /** |
||
105 | * Remove the preferred SMS flag from any number. |
||
106 | */ |
||
107 | View Code Duplication | public function unsetSmsNumber() |
|
118 | |||
119 | /** |
||
120 | * Set the user's preferred SMS number, creating a new internal mobile number if needed |
||
121 | * @param $number string The SMS-capable mobile phone number |
||
122 | */ |
||
123 | public function setSmsNumber($number) |
||
142 | |||
143 | /** |
||
144 | * Add the user's preferred SMS number as a new internal mobile number. |
||
145 | * @param $number string The SMS-capable mobile phone number |
||
146 | */ |
||
147 | public function addSmsNumber($number) |
||
157 | |||
158 | /** |
||
159 | * Save the user |
||
160 | * |
||
161 | * @return string The API response body |
||
162 | */ |
||
163 | public function save() |
||
168 | |||
169 | /** |
||
170 | * Check if we have the full representation of our data object. |
||
171 | * |
||
172 | * @param \stdClass $data |
||
173 | * |
||
174 | * @return bool |
||
175 | */ |
||
176 | protected function isInitialized($data) |
||
180 | |||
181 | /** |
||
182 | * Called when data is available to be processed. |
||
183 | * |
||
184 | * @param mixed $data |
||
185 | */ |
||
186 | protected function onData($data) |
||
190 | |||
191 | /** |
||
192 | * Generate the base URL for this resource. |
||
193 | * |
||
194 | * @return string |
||
195 | */ |
||
196 | protected function urlBase() |
||
200 | |||
201 | public function __get($key) |
||
228 | } |
||
229 |
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.