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 |
||
4 | class MetadataCollection extends BaseModel |
||
5 | { |
||
6 | |||
7 | /** |
||
8 | * Collection Items Key |
||
9 | * |
||
10 | * @const string |
||
11 | */ |
||
12 | const COLLECTION_ITEMS_KEY = 'entries'; |
||
13 | |||
14 | /** |
||
15 | * Collection Cursor Key |
||
16 | * |
||
17 | * @const string |
||
18 | */ |
||
19 | const COLLECTION_CURSOR_KEY = 'cursor'; |
||
20 | |||
21 | /** |
||
22 | * Collection has-more-items Key |
||
23 | * |
||
24 | * @const string |
||
25 | */ |
||
26 | const COLLECTION_HAS_MORE_ITEMS_KEY = 'has_more'; |
||
27 | |||
28 | /** |
||
29 | * Collection Data |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $data; |
||
34 | |||
35 | /** |
||
36 | * List of Files/Folder Metadata |
||
37 | * |
||
38 | * @var \Dropbox\Models\ModelCollection |
||
39 | */ |
||
40 | protected $items = null; |
||
41 | |||
42 | /** |
||
43 | * Cursor for pagination and updates |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $cursor; |
||
48 | |||
49 | /** |
||
50 | * If more items are available |
||
51 | * |
||
52 | * @var boolean |
||
53 | */ |
||
54 | protected $hasMoreItems; |
||
55 | |||
56 | /** |
||
57 | * Create a new Metadata Collection |
||
58 | * |
||
59 | * @param array $data Collection Data |
||
60 | */ |
||
61 | 7 | public function __construct(array $data) |
|
71 | |||
72 | /** |
||
73 | * Get the Collection Items Key |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | 7 | public function getCollectionItemsKey() |
|
81 | |||
82 | /** |
||
83 | * Get the Collection has-more-items Key |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | 7 | public function getCollectionHasMoreItemsKey() |
|
91 | |||
92 | /** |
||
93 | * Get the Collection Cursor Key |
||
94 | * |
||
95 | * @return string |
||
96 | */ |
||
97 | 7 | public function getCollectionCursorKey() |
|
101 | |||
102 | /** |
||
103 | * Get the Items |
||
104 | * |
||
105 | * @return \Dropbox\Models\ModelCollection |
||
106 | */ |
||
107 | 7 | public function getItems() |
|
111 | |||
112 | /** |
||
113 | * Get the cursor |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | 1 | public function getCursor() |
|
121 | |||
122 | /** |
||
123 | * More items are available |
||
124 | * |
||
125 | * @return boolean |
||
126 | */ |
||
127 | public function hasMoreItems() |
||
131 | |||
132 | /** |
||
133 | * Process items and cast them |
||
134 | * to their respective Models |
||
135 | * |
||
136 | * @param array $items Unprocessed Items |
||
137 | * |
||
138 | * @return void |
||
139 | */ |
||
140 | 4 | View Code Duplication | protected function processItems(array $items) |
150 | } |
||
151 |
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.