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 |
||
28 | class ArrayStore implements SortableStore, CountableStore |
||
29 | { |
||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $array = array(); |
||
34 | |||
35 | /** |
||
36 | * Creates a new store. |
||
37 | * |
||
38 | * @param array $array The values to set initially in the store. |
||
39 | */ |
||
40 | 192 | public function __construct(array $array = array()) |
|
41 | { |
||
42 | 192 | foreach ($array as $key => $value) { |
|
43 | 2 | $this->set($key, $value); |
|
44 | } |
||
45 | 192 | } |
|
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | 132 | public function set($key, $value) |
|
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | 9 | View Code Duplication | public function get($key, $default = null) |
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | 64 | public function getOrFail($key) |
|
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | 26 | public function getMultiple(array $keys, $default = null) |
|
89 | { |
||
90 | 26 | KeyUtil::validateMultiple($keys); |
|
91 | |||
92 | 22 | $values = array(); |
|
93 | |||
94 | 22 | foreach ($keys as $key) { |
|
95 | 22 | $values[$key] = array_key_exists($key, $this->array) |
|
96 | 22 | ? $this->array[$key] |
|
97 | 22 | : $default; |
|
98 | } |
||
99 | |||
100 | 22 | return $values; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | 60 | public function getMultipleOrFail(array $keys) |
|
107 | { |
||
108 | 60 | KeyUtil::validateMultiple($keys); |
|
109 | |||
110 | 56 | $values = array(); |
|
111 | |||
112 | 56 | foreach ($keys as $key) { |
|
113 | 56 | $values[$key] = $this->getOrFail($key); |
|
114 | } |
||
115 | |||
116 | 54 | return $values; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | 20 | View Code Duplication | public function remove($key) |
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | 74 | public function exists($key) |
|
142 | |||
143 | /** |
||
144 | * {@inheritdoc} |
||
145 | */ |
||
146 | 186 | public function clear() |
|
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | 24 | public function keys() |
|
158 | |||
159 | /** |
||
160 | * Returns the contents of the store as array. |
||
161 | * |
||
162 | * @return array The keys and values in the store. |
||
163 | */ |
||
164 | public function toArray() |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | 12 | public function sort($flags = SORT_REGULAR) |
|
176 | |||
177 | /** |
||
178 | * {@inheritdoc} |
||
179 | */ |
||
180 | 2 | public function count() |
|
184 | } |
||
185 |
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.