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 |
||
9 | class Collection extends BaseCollection |
||
10 | { |
||
11 | /** |
||
12 | * Cassandra rows instance |
||
13 | * |
||
14 | * @var \Cassandra\Rows |
||
15 | */ |
||
16 | private $rows; |
||
17 | |||
18 | /** |
||
19 | * Set Cassandra rows instance related to the |
||
20 | * collection items. |
||
21 | * |
||
22 | * Required for fetching next pages |
||
23 | * |
||
24 | * @param Rows $rows |
||
25 | * |
||
26 | * @return $this |
||
27 | */ |
||
28 | 56 | public function setRowsInstance(Rows $rows) |
|
34 | |||
35 | /** |
||
36 | * Next page token |
||
37 | * |
||
38 | * @return mixed |
||
39 | */ |
||
40 | 3 | public function getNextPageToken() |
|
48 | |||
49 | /** |
||
50 | * Last page indicator |
||
51 | * @return bool |
||
52 | */ |
||
53 | 6 | public function isLastPage() |
|
61 | |||
62 | /** |
||
63 | * Get next page |
||
64 | * |
||
65 | * @return Collection |
||
66 | */ |
||
67 | 5 | public function nextPage() |
|
81 | |||
82 | /** |
||
83 | * Get rows instance |
||
84 | * |
||
85 | * @return \Cassandra\Rows |
||
86 | */ |
||
87 | 50 | public function getRows() |
|
91 | |||
92 | /** |
||
93 | * Update current collection with results from |
||
94 | * the next page |
||
95 | * |
||
96 | * @return Collection |
||
97 | */ |
||
98 | 2 | public function appendNextPage() |
|
109 | |||
110 | /** |
||
111 | * Merge the collection with the given items. |
||
112 | * |
||
113 | * @param \ArrayAccess|array $items |
||
114 | * @return static |
||
115 | */ |
||
116 | 2 | public function merge($items) |
|
126 | |||
127 | /** |
||
128 | * Reload a fresh model instance from the database for all the entities. |
||
129 | * |
||
130 | * @param array|string $with |
||
131 | * @return static |
||
132 | */ |
||
133 | 4 | public function fresh($with = []) |
|
154 | |||
155 | /** |
||
156 | * Diff the collection with the given items. |
||
157 | * |
||
158 | * @param \ArrayAccess|array $items |
||
159 | * @return static |
||
160 | */ |
||
161 | 1 | View Code Duplication | public function diff($items) |
175 | |||
176 | /** |
||
177 | * Intersect the collection with the given items. |
||
178 | * |
||
179 | * @param \ArrayAccess|array $items |
||
180 | * @return static |
||
181 | */ |
||
182 | 1 | View Code Duplication | public function intersect($items) |
196 | |||
197 | /** |
||
198 | * Get a dictionary keyed by primary keys. |
||
199 | * |
||
200 | * @param \ArrayAccess|array|null $items |
||
201 | * @return array |
||
202 | */ |
||
203 | 11 | public function getDictionary($items = null) |
|
215 | } |
||
216 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.