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 |
||
31 | class ResultIterator implements Result, \ArrayAccess, \JsonSerializable |
||
32 | { |
||
33 | /** |
||
34 | * @var Statement |
||
35 | */ |
||
36 | protected $statement; |
||
37 | |||
38 | protected $fetchStarted = false; |
||
39 | private $objectStorage; |
||
40 | private $className; |
||
41 | |||
42 | private $tdbmService; |
||
43 | private $magicSql; |
||
44 | private $magicSqlCount; |
||
45 | private $parameters; |
||
46 | private $columnDescriptors; |
||
47 | private $magicQuery; |
||
48 | |||
49 | /** |
||
50 | * @var InnerResultIterator |
||
51 | */ |
||
52 | private $innerResultIterator; |
||
53 | |||
54 | /** |
||
55 | * The key of the current retrieved object. |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | protected $key = -1; |
||
60 | |||
61 | protected $current = null; |
||
62 | |||
63 | private $databasePlatform; |
||
64 | |||
65 | private $totalCount; |
||
66 | |||
67 | private $mode; |
||
68 | |||
69 | View Code Duplication | public function __construct($magicSql, $magicSqlCount, array $parameters, array $columnDescriptors, $objectStorage, $className, TDBMService $tdbmService, MagicQuery $magicQuery, $mode) |
|
82 | |||
83 | protected function executeCountQuery() |
||
88 | |||
89 | /** |
||
90 | * Counts found records (this is the number of records fetched, taking into account the LIMIT and OFFSET settings). |
||
91 | * |
||
92 | * @return int |
||
93 | */ |
||
94 | public function count() |
||
102 | |||
103 | /** |
||
104 | * Casts the result set to a PHP array. |
||
105 | * |
||
106 | * @return array |
||
107 | */ |
||
108 | public function toArray() |
||
112 | |||
113 | /** |
||
114 | * Returns a new iterator mapping any call using the $callable function. |
||
115 | * |
||
116 | * @param callable $callable |
||
117 | * |
||
118 | * @return MapIterator |
||
119 | */ |
||
120 | public function map(callable $callable) |
||
124 | |||
125 | /** |
||
126 | * Retrieve an external iterator. |
||
127 | * |
||
128 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
129 | * |
||
130 | * @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or |
||
131 | * <b>Traversable</b> |
||
132 | * |
||
133 | * @since 5.0.0 |
||
134 | */ |
||
135 | public function getIterator() |
||
147 | |||
148 | /** |
||
149 | * @param int $offset |
||
150 | * |
||
151 | * @return PageIterator |
||
152 | */ |
||
153 | public function take($offset, $limit) |
||
157 | |||
158 | /** |
||
159 | * Whether a offset exists. |
||
160 | * |
||
161 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
162 | * |
||
163 | * @param mixed $offset <p> |
||
164 | * An offset to check for. |
||
165 | * </p> |
||
166 | * |
||
167 | * @return bool true on success or false on failure. |
||
168 | * </p> |
||
169 | * <p> |
||
170 | * The return value will be casted to boolean if non-boolean was returned. |
||
171 | * |
||
172 | * @since 5.0.0 |
||
173 | */ |
||
174 | public function offsetExists($offset) |
||
178 | |||
179 | /** |
||
180 | * Offset to retrieve. |
||
181 | * |
||
182 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
183 | * |
||
184 | * @param mixed $offset <p> |
||
185 | * The offset to retrieve. |
||
186 | * </p> |
||
187 | * |
||
188 | * @return mixed Can return all value types. |
||
189 | * |
||
190 | * @since 5.0.0 |
||
191 | */ |
||
192 | public function offsetGet($offset) |
||
196 | |||
197 | /** |
||
198 | * Offset to set. |
||
199 | * |
||
200 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
201 | * |
||
202 | * @param mixed $offset <p> |
||
203 | * The offset to assign the value to. |
||
204 | * </p> |
||
205 | * @param mixed $value <p> |
||
206 | * The value to set. |
||
207 | * </p> |
||
208 | * |
||
209 | * @since 5.0.0 |
||
210 | */ |
||
211 | public function offsetSet($offset, $value) |
||
215 | |||
216 | /** |
||
217 | * Offset to unset. |
||
218 | * |
||
219 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
220 | * |
||
221 | * @param mixed $offset <p> |
||
222 | * The offset to unset. |
||
223 | * </p> |
||
224 | * |
||
225 | * @since 5.0.0 |
||
226 | */ |
||
227 | public function offsetUnset($offset) |
||
231 | |||
232 | /** |
||
233 | * Specify data which should be serialized to JSON. |
||
234 | * |
||
235 | * @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
||
236 | * |
||
237 | * @return mixed data which can be serialized by <b>json_encode</b>, |
||
238 | * which is a value of any type other than a resource. |
||
239 | * |
||
240 | * @since 5.4.0 |
||
241 | */ |
||
242 | public function jsonSerialize() |
||
248 | |||
249 | /** |
||
250 | * Returns only one value (the first) of the result set. |
||
251 | * Returns null if no value exists. |
||
252 | * |
||
253 | * @return mixed|null |
||
254 | */ |
||
255 | public function first() |
||
263 | } |
||
264 |
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.