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 |
||
33 | class ResultIterator implements Result, \ArrayAccess { |
||
34 | |||
35 | /** |
||
36 | * |
||
37 | * @var Statement |
||
38 | */ |
||
39 | protected $statement; |
||
40 | |||
41 | protected $fetchStarted = false; |
||
42 | private $objectStorage; |
||
43 | private $className; |
||
44 | |||
45 | |||
46 | private $tdbmService; |
||
47 | private $magicSql; |
||
48 | private $magicSqlCount; |
||
49 | private $parameters; |
||
50 | private $columnDescriptors; |
||
51 | private $magicQuery; |
||
52 | |||
53 | /** |
||
54 | * @var InnerResultIterator |
||
55 | */ |
||
56 | private $innerResultIterator; |
||
57 | |||
58 | /** |
||
59 | * The key of the current retrieved object. |
||
60 | * |
||
61 | * @var int |
||
62 | */ |
||
63 | protected $key = -1; |
||
64 | |||
65 | protected $current = null; |
||
66 | |||
67 | private $databasePlatform; |
||
68 | |||
69 | private $totalCount; |
||
70 | |||
71 | private $mode; |
||
72 | |||
73 | View Code Duplication | public function __construct($magicSql, $magicSqlCount, array $parameters, array $columnDescriptors, $objectStorage, $className, TDBMService $tdbmService, MagicQuery $magicQuery, $mode) |
|
86 | |||
87 | protected function executeCountQuery() { |
||
91 | |||
92 | /** |
||
93 | * Counts found records (this is the number of records fetched, taking into account the LIMIT and OFFSET settings) |
||
94 | * @return int |
||
95 | */ |
||
96 | public function count() |
||
103 | |||
104 | /** |
||
105 | * Casts the result set to a PHP array. |
||
106 | * |
||
107 | * @return array |
||
108 | */ |
||
109 | public function toArray() { |
||
112 | |||
113 | /** |
||
114 | * Returns a new iterator mapping any call using the $callable function. |
||
115 | * |
||
116 | * @param callable $callable |
||
117 | * @return MapIterator |
||
118 | */ |
||
119 | public function map(callable $callable) { |
||
122 | |||
123 | /** |
||
124 | * Fetches all records (this could impact into your site performance) and rewinds the cursor |
||
125 | * @param boolean $asRecords Bind into record class? |
||
126 | * @return array[Record_PDO]|array[array] Array of records or arrays (depends on $asRecords) |
||
127 | */ |
||
128 | /*public function getAll($asRecords = true) |
||
129 | { |
||
130 | $all = array(); |
||
131 | $this->rewind(); |
||
132 | foreach ($this->pdoStatement as $id => $doc) { |
||
133 | if ($asRecords) |
||
134 | $all[$id] = $this->cast($doc); |
||
135 | else |
||
136 | $all[$id] = $doc; |
||
137 | } |
||
138 | return $all; |
||
139 | }*/ |
||
140 | /** |
||
141 | * @return PDOStatement |
||
142 | */ |
||
143 | /*public function getPDOStatement() |
||
144 | { |
||
145 | return $this->pdoStatement; |
||
146 | }*/ |
||
147 | |||
148 | /** |
||
149 | * Retrieve an external iterator |
||
150 | * @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
151 | * @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or |
||
152 | * <b>Traversable</b> |
||
153 | * @since 5.0.0 |
||
154 | */ |
||
155 | public function getIterator() |
||
166 | |||
167 | /** |
||
168 | * @param int $offset |
||
169 | * @return PageIterator |
||
170 | */ |
||
171 | public function take($offset, $limit) |
||
175 | |||
176 | /** |
||
177 | * Whether a offset exists |
||
178 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
179 | * @param mixed $offset <p> |
||
180 | * An offset to check for. |
||
181 | * </p> |
||
182 | * @return boolean true on success or false on failure. |
||
183 | * </p> |
||
184 | * <p> |
||
185 | * The return value will be casted to boolean if non-boolean was returned. |
||
186 | * @since 5.0.0 |
||
187 | */ |
||
188 | public function offsetExists($offset) |
||
192 | |||
193 | /** |
||
194 | * Offset to retrieve |
||
195 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
196 | * @param mixed $offset <p> |
||
197 | * The offset to retrieve. |
||
198 | * </p> |
||
199 | * @return mixed Can return all value types. |
||
200 | * @since 5.0.0 |
||
201 | */ |
||
202 | public function offsetGet($offset) |
||
206 | |||
207 | /** |
||
208 | * Offset to set |
||
209 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
210 | * @param mixed $offset <p> |
||
211 | * The offset to assign the value to. |
||
212 | * </p> |
||
213 | * @param mixed $value <p> |
||
214 | * The value to set. |
||
215 | * </p> |
||
216 | * @return void |
||
217 | * @since 5.0.0 |
||
218 | */ |
||
219 | public function offsetSet($offset, $value) |
||
223 | |||
224 | /** |
||
225 | * Offset to unset |
||
226 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
227 | * @param mixed $offset <p> |
||
228 | * The offset to unset. |
||
229 | * </p> |
||
230 | * @return void |
||
231 | * @since 5.0.0 |
||
232 | */ |
||
233 | public function offsetUnset($offset) |
||
237 | } |
||
238 |
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.