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 |
||
14 | abstract class CollectionAbstract implements CollectionInterface |
||
15 | { |
||
|
|||
16 | /** |
||
17 | * Adds all of the elements in the specified collection to this collection (optional operation). |
||
18 | * @param CollectionInterface $collection |
||
19 | * @return boolean $changed |
||
20 | */ |
||
21 | View Code Duplication | public function addAll(CollectionInterface $collection) |
|
34 | |||
35 | /** |
||
36 | * Returns true if this collection contains the specified element. |
||
37 | * @return boolean |
||
38 | */ |
||
39 | public function contains($element) |
||
52 | |||
53 | /** |
||
54 | * Returns true if this collection contains all of the elements in the specified collection. |
||
55 | * @param Collection $collection |
||
56 | * @return boolean |
||
57 | */ |
||
58 | public function containsAll(CollectionInterface $collection) |
||
72 | |||
73 | /** |
||
74 | * Compares the specified object with this collection for equality. |
||
75 | * @param unknown $object |
||
76 | * @return boolean |
||
77 | */ |
||
78 | public function equals($object) |
||
86 | |||
87 | /** |
||
88 | * Returns true if this collection contains no elements. |
||
89 | * @return boolean |
||
90 | */ |
||
91 | public function isEmpty() |
||
95 | |||
96 | /** |
||
97 | * Retains only the elements in this collection that are contained in the specified collection (optional operation). |
||
98 | * @param CollectionInterface $collection |
||
99 | * @return boolean |
||
100 | */ |
||
101 | View Code Duplication | public function retainAll(CollectionInterface $collection) |
|
116 | |||
117 | /** |
||
118 | * Returns a json string representing all the elements in this collection |
||
119 | * @return string |
||
120 | */ |
||
121 | public function toJson() |
||
130 | |||
131 | /** |
||
132 | * Ensures that this collection contains the specified element (optional operation). |
||
133 | * @param multitype $element |
||
134 | * @return boolean |
||
135 | */ |
||
136 | public abstract function add($element); |
||
137 | |||
138 | /** |
||
139 | * Removes all of the elements from this collection (optional operation). |
||
140 | * @return void |
||
141 | */ |
||
142 | public abstract function clear(); |
||
143 | |||
144 | /** |
||
145 | * Returns an iterator over the elements in this collection. |
||
146 | * @return IteratorInterface |
||
147 | */ |
||
148 | public abstract function iterator(); |
||
149 | |||
150 | /** |
||
151 | * Removes a single instance of the specified element from this collection, if it is present (optional operation). |
||
152 | * @param multitype $element |
||
153 | * @return boolean |
||
154 | */ |
||
155 | public abstract function remove($element); |
||
156 | |||
157 | /** |
||
158 | * Removes all of this collection's elements that are also contained in the specified collection (optional operation). |
||
159 | * @param CollectionInterface $collection |
||
160 | * @return boolean |
||
161 | */ |
||
162 | public abstract function removeAll(CollectionInterface $collection); |
||
163 | |||
164 | /** |
||
165 | * Returns the number of elements in this collection. |
||
166 | * @return int |
||
167 | */ |
||
168 | public abstract function size(); |
||
169 | |||
170 | /** |
||
171 | * Returns an array containing all of the elements in this collection. |
||
172 | * @return array |
||
173 | */ |
||
174 | public abstract function toArray(); |
||
175 | |||
176 | /** |
||
177 | * Returns the hash code value for this collection. |
||
178 | * @return string |
||
179 | */ |
||
180 | public abstract function hashCode(); |
||
181 | } |