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 |
||
10 | View Code Duplication | class Portfolios extends LazyResourceList implements \Countable, \Iterator, \ArrayAccess |
|
|
|||
11 | { |
||
12 | use ReadOnlyArrayAccess; |
||
13 | use IterableCollection; |
||
14 | |||
15 | /** |
||
16 | * The Bib this Portfolios list belongs to. |
||
17 | * |
||
18 | * @var Bib |
||
19 | */ |
||
20 | public $bib; |
||
21 | |||
22 | /** |
||
23 | * Portfolios constructor. |
||
24 | * |
||
25 | * @param Client $client |
||
26 | * @param Bib $bib |
||
27 | */ |
||
28 | public function __construct(Client $client, Bib $bib) |
||
33 | |||
34 | /** |
||
35 | * Get a single portfolio record by id. |
||
36 | * |
||
37 | * @param string $id |
||
38 | * |
||
39 | * @return Portfolio |
||
40 | */ |
||
41 | public function get($id) |
||
45 | |||
46 | /** |
||
47 | * Convert a data element to a resource object. |
||
48 | * |
||
49 | * @param $data |
||
50 | * |
||
51 | * @return Portfolio |
||
52 | */ |
||
53 | protected function convertToResource($data) |
||
58 | |||
59 | /** |
||
60 | * Generate the base URL for this resource. |
||
61 | * |
||
62 | * @return string |
||
63 | */ |
||
64 | protected function urlBase() |
||
68 | } |
||
69 |
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.