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 |
||
18 | class HashSet extends AbstractSet |
||
19 | { |
||
20 | /** |
||
21 | * The data storage |
||
22 | * |
||
23 | * @var MapInterface |
||
24 | */ |
||
25 | protected $map; |
||
26 | |||
27 | /** |
||
28 | * Constructor |
||
29 | * |
||
30 | * Use [@see AbstractSet::add] to ensure uniqueness |
||
31 | * |
||
32 | * @param CollectionInterface $elements |
||
33 | */ |
||
34 | 9 | public function __construct(CollectionInterface $elements = null) |
|
42 | |||
43 | /** |
||
44 | * Adds the element to the collection if it doesn't exist |
||
45 | * |
||
46 | * By default this method will use strict comparison checking, passing false |
||
47 | * in will use a double equals (==) instead. |
||
48 | * |
||
49 | * @param mixed $element |
||
50 | * @param bool $strict |
||
51 | * @return bool |
||
52 | */ |
||
53 | 19 | public function add($element, bool $strict = true): bool |
|
63 | |||
64 | /** |
||
65 | * Adds any elements from specified collection that do not already exist |
||
66 | * |
||
67 | * By default this method will use strict comparison checking, passing false |
||
68 | * in will use a double equals (==) instead. |
||
69 | * |
||
70 | * @param CollectionInterface $collection |
||
71 | * @param bool $strict |
||
72 | * @return bool |
||
73 | */ |
||
74 | 11 | View Code Duplication | public function addAll(CollectionInterface $collection, bool $strict = true): bool |
83 | |||
84 | /** |
||
85 | * Removes all elements from a collection |
||
86 | * |
||
87 | * @return void |
||
88 | */ |
||
89 | 1 | public function clear() |
|
93 | |||
94 | /** |
||
95 | * Removes object if it exists |
||
96 | * |
||
97 | * By default this method will use strict comparison checking, passing false |
||
98 | * in will use a double equals (==) instead. |
||
99 | * |
||
100 | * Returns true if the element was removed |
||
101 | * |
||
102 | * @param mixed $element |
||
103 | * @param bool $strict |
||
104 | * @return bool |
||
105 | */ |
||
106 | 3 | public function remove($element, bool $strict = true): bool |
|
113 | |||
114 | /** |
||
115 | * Returns true if the collection contains element |
||
116 | * |
||
117 | * By default this method will use strict comparison checking, passing false |
||
118 | * in will use a double equals (==) instead. |
||
119 | * |
||
120 | * @param mixed $element |
||
121 | * @param bool $strict |
||
122 | * @return bool |
||
123 | */ |
||
124 | 19 | public function contains($element, bool $strict = true): bool |
|
128 | |||
129 | /** |
||
130 | * Returns an array of all elements in the collection |
||
131 | * |
||
132 | * @return array |
||
133 | */ |
||
134 | 5 | public function toArray(): array |
|
138 | |||
139 | /** |
||
140 | * Filter the collection using closure |
||
141 | * |
||
142 | * The closure will get passed each element. Returning true from the |
||
143 | * closure will include that element in the new collection. |
||
144 | * |
||
145 | * @param callable $filter |
||
146 | * @return CollectionInterface |
||
147 | */ |
||
148 | 1 | public function filter(callable $filter): CollectionInterface |
|
152 | |||
153 | /** |
||
154 | * Retrieve an external iterator |
||
155 | * |
||
156 | * @return ArrayIterator |
||
157 | */ |
||
158 | 1 | public function getIterator(): ArrayIterator |
|
162 | |||
163 | /** |
||
164 | * Returns the size of the collection |
||
165 | * |
||
166 | * @return int |
||
167 | */ |
||
168 | 14 | public function count(): int |
|
172 | } |
||
173 |
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.