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 |
||
10 | class IdiormResultSet implements \Countable, \IteratorAggregate, \ArrayAccess, \Serializable |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * The current result set as an array |
||
15 | * |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $_results = array(); |
||
19 | |||
20 | /** |
||
21 | * Optionally set the contents of the result set by passing in array |
||
22 | * |
||
23 | * @param array $results |
||
24 | */ |
||
25 | 14 | public function __construct(array $results = array()) |
|
29 | |||
30 | /** |
||
31 | * Set the contents of the result set by passing in array |
||
32 | * |
||
33 | * @param array $results |
||
34 | */ |
||
35 | 14 | public function set_results(array $results) |
|
39 | |||
40 | /** |
||
41 | * Get the current result set as an array |
||
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | 4 | public function get_results() |
|
49 | |||
50 | /** |
||
51 | * Return the content of the result set |
||
52 | * as an array of associative arrays. Column |
||
53 | * names may optionally be supplied as arguments, |
||
54 | * if so, only those keys will be returned. |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | 1 | public function as_array() |
|
79 | |||
80 | /** |
||
81 | * Get the current result set as an json |
||
82 | * |
||
83 | * @param int $options |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | 1 | public function as_json($options = 0) |
|
91 | |||
92 | /** |
||
93 | * Get the number of records in the result set |
||
94 | * |
||
95 | * @return int |
||
96 | */ |
||
97 | 3 | public function count() |
|
101 | |||
102 | /** |
||
103 | * Get an iterator for this object. In this case it supports foreaching |
||
104 | * over the result set. |
||
105 | * |
||
106 | * @return \ArrayIterator |
||
107 | */ |
||
108 | 3 | public function getIterator() |
|
112 | |||
113 | /** |
||
114 | * ArrayAccess |
||
115 | * |
||
116 | * @param int|string $offset |
||
117 | * |
||
118 | * @return bool |
||
119 | */ |
||
120 | 1 | public function offsetExists($offset) |
|
124 | |||
125 | /** |
||
126 | * ArrayAccess |
||
127 | * |
||
128 | * @param int|string $offset |
||
129 | * |
||
130 | * @return mixed |
||
131 | */ |
||
132 | 1 | public function offsetGet($offset) |
|
136 | |||
137 | /** |
||
138 | * ArrayAccess |
||
139 | * |
||
140 | * @param int|string $offset |
||
141 | * @param mixed $value |
||
142 | */ |
||
143 | 1 | public function offsetSet($offset, $value) |
|
147 | |||
148 | /** |
||
149 | * ArrayAccess |
||
150 | * |
||
151 | * @param int|string $offset |
||
152 | */ |
||
153 | 1 | public function offsetUnset($offset) |
|
157 | |||
158 | /** |
||
159 | * Serializable |
||
160 | * |
||
161 | * @return string |
||
162 | */ |
||
163 | 1 | public function serialize() |
|
167 | |||
168 | /** |
||
169 | * Serializable |
||
170 | * |
||
171 | * @param string $serialized |
||
172 | * |
||
173 | * @return array |
||
174 | */ |
||
175 | 1 | public function unserialize($serialized) |
|
179 | |||
180 | /** |
||
181 | * Call a method on all models in a result set. This allows for method |
||
182 | * chaining such as setting a property on all models in a result set or |
||
183 | * any other batch operation across models. |
||
184 | * |
||
185 | * @example ORM::for_table('Widget')->find_many()->set('field', 'value')->save(); |
||
186 | * |
||
187 | * @param string $method |
||
188 | * @param array $params |
||
189 | * |
||
190 | * @return $this |
||
191 | * |
||
192 | * @throws IdiormMethodMissingException |
||
193 | */ |
||
194 | 2 | public function __call($method, $params = array()) |
|
206 | |||
207 | } |
||
208 |
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.