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 | class Tuple extends CollectionAbstract implements ListInterface, ImmutableInterface |
||
15 | { |
||
16 | use ImmutableListTrait; |
||
17 | |||
18 | /** |
||
19 | * An internal, immutable array containing values |
||
20 | * @var array |
||
21 | */ |
||
22 | private $values; |
||
23 | |||
24 | public function __construct() |
||
28 | |||
29 | public function hashCode() |
||
44 | |||
45 | /******************************************************** |
||
46 | ** Collection methods |
||
47 | ********************************************************/ |
||
48 | |||
49 | /** |
||
50 | * Returns an iterator over the elements in this collection. |
||
51 | * @return IteratorInterface |
||
52 | */ |
||
53 | public function iterator() |
||
57 | |||
58 | /** |
||
59 | * Returns the number of elements in this collection. |
||
60 | */ |
||
61 | public function size() |
||
65 | |||
66 | /** |
||
67 | * Returns an array containing all of the elements in this collection. |
||
68 | */ |
||
69 | public function toArray() |
||
73 | |||
74 | /******************************************************** |
||
75 | ** List methods |
||
76 | ********************************************************/ |
||
77 | |||
78 | /** |
||
79 | * Returns the element at the specified position in this list. |
||
80 | * @param int $index |
||
81 | */ |
||
82 | public function get($index) |
||
86 | |||
87 | /** |
||
88 | * Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. |
||
89 | * @param multitype $element |
||
90 | */ |
||
91 | View Code Duplication | public function indexOf($element) |
|
99 | |||
100 | /** |
||
101 | * Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. |
||
102 | * @param multitype $element |
||
103 | */ |
||
104 | View Code Duplication | public function lastIndexOf($element) |
|
112 | |||
113 | /** |
||
114 | * Returns a list iterator over the elements in this list (in proper sequence) |
||
115 | */ |
||
116 | public function listIterator() |
||
120 | |||
121 | /** |
||
122 | * Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. |
||
123 | * @param int $index |
||
124 | */ |
||
125 | public function listIteratorAt($index) |
||
129 | |||
130 | |||
131 | /** |
||
132 | * Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. |
||
133 | * @param int $from |
||
134 | * @param int $to |
||
135 | * @return ArrayList |
||
136 | */ |
||
137 | public function subList($from, $to) |
||
141 | |||
142 | /********************************************* |
||
143 | ** Array Access Methods |
||
144 | *********************************************/ |
||
145 | |||
146 | /** |
||
147 | * |
||
148 | * {@inheritDoc} |
||
149 | * @see ArrayAccess::offsetExists() |
||
150 | */ |
||
151 | View Code Duplication | public function offsetExists($offset) |
|
159 | |||
160 | /** |
||
161 | * |
||
162 | * {@inheritDoc} |
||
163 | * @see ArrayAccess::offsetGet() |
||
164 | */ |
||
165 | public function offsetGet($offset) |
||
169 | } |
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.