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 |
||
| 12 | View Code Duplication | class Loan extends LazyResource |
|
|
|
|||
| 13 | { |
||
| 14 | /* @var User */ |
||
| 15 | protected $user; |
||
| 16 | |||
| 17 | /* @var string */ |
||
| 18 | protected $loan_id; |
||
| 19 | |||
| 20 | public function __construct(Client $client, User $user, $loan_id) |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Generate the base URL for this resource. |
||
| 29 | * |
||
| 30 | * @return string |
||
| 31 | */ |
||
| 32 | protected function urlBase() |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Check if we have the full representation of our data object. |
||
| 39 | * |
||
| 40 | * @param \stdClass $data |
||
| 41 | * |
||
| 42 | * @return bool |
||
| 43 | */ |
||
| 44 | protected function isInitialized($data) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Get the Item on loan. Since the response from the loan(s) API does not |
||
| 51 | * include the `holding_id` and `item_pid`, we cannot initiate an Item object |
||
| 52 | * directly, so we have to lookup the barcode instead. |
||
| 53 | * |
||
| 54 | * @see https://developers.exlibrisgroup.com/discussions#!/forum/posts/list/1397.page |
||
| 55 | * |
||
| 56 | * @return Item |
||
| 57 | */ |
||
| 58 | public function getItem() |
||
| 62 | } |
||
| 63 |
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.