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 InnerResultIterator implements \Iterator, \Countable, \ArrayAccess { |
||
| 32 | |||
| 33 | /** |
||
| 34 | * |
||
| 35 | * @var Statement |
||
| 36 | */ |
||
| 37 | protected $statement; |
||
| 38 | |||
| 39 | protected $fetchStarted = false; |
||
| 40 | private $objectStorage; |
||
| 41 | private $className; |
||
| 42 | |||
| 43 | private $tdbmService; |
||
| 44 | private $magicSql; |
||
| 45 | private $parameters; |
||
| 46 | private $limit; |
||
| 47 | private $offset; |
||
| 48 | private $columnDescriptors; |
||
| 49 | private $magicQuery; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The key of the current retrieved object. |
||
| 53 | * |
||
| 54 | * @var int |
||
| 55 | */ |
||
| 56 | protected $key = -1; |
||
| 57 | |||
| 58 | protected $current = null; |
||
| 59 | |||
| 60 | private $databasePlatform; |
||
| 61 | |||
| 62 | private $totalCount; |
||
|
|
|||
| 63 | |||
| 64 | View Code Duplication | public function __construct($magicSql, array $parameters, $limit, $offset, array $columnDescriptors, $objectStorage, $className, TDBMService $tdbmService, MagicQuery $magicQuery) |
|
| 77 | |||
| 78 | protected function executeQuery() { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Counts found records (this is the number of records fetched, taking into account the LIMIT and OFFSET settings) |
||
| 89 | * @return int |
||
| 90 | */ |
||
| 91 | public function count() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Fetches record at current cursor. |
||
| 101 | * @return AbstractTDBMObject|null |
||
| 102 | */ |
||
| 103 | public function current() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Returns the current result's key |
||
| 110 | * @return int |
||
| 111 | */ |
||
| 112 | public function key() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Advances the cursor to the next result. |
||
| 119 | * Casts the database result into one (or several) beans. |
||
| 120 | */ |
||
| 121 | public function next() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Moves the cursor to the beginning of the result set |
||
| 183 | */ |
||
| 184 | public function rewind() |
||
| 190 | /** |
||
| 191 | * Checks if the cursor is reading a valid result. |
||
| 192 | * |
||
| 193 | * @return boolean |
||
| 194 | */ |
||
| 195 | public function valid() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Fetches all records (this could impact into your site performance) and rewinds the cursor |
||
| 202 | * @param boolean $asRecords Bind into record class? |
||
| 203 | * @return array[Record_PDO]|array[array] Array of records or arrays (depends on $asRecords) |
||
| 204 | */ |
||
| 205 | /*public function getAll($asRecords = true) |
||
| 206 | { |
||
| 207 | $all = array(); |
||
| 208 | $this->rewind(); |
||
| 209 | foreach ($this->pdoStatement as $id => $doc) { |
||
| 210 | if ($asRecords) |
||
| 211 | $all[$id] = $this->cast($doc); |
||
| 212 | else |
||
| 213 | $all[$id] = $doc; |
||
| 214 | } |
||
| 215 | return $all; |
||
| 216 | }*/ |
||
| 217 | /** |
||
| 218 | * @return PDOStatement |
||
| 219 | */ |
||
| 220 | /*public function getPDOStatement() |
||
| 221 | { |
||
| 222 | return $this->pdoStatement; |
||
| 223 | }*/ |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Whether a offset exists |
||
| 227 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
| 228 | * @param mixed $offset <p> |
||
| 229 | * An offset to check for. |
||
| 230 | * </p> |
||
| 231 | * @return boolean true on success or false on failure. |
||
| 232 | * </p> |
||
| 233 | * <p> |
||
| 234 | * The return value will be casted to boolean if non-boolean was returned. |
||
| 235 | * @since 5.0.0 |
||
| 236 | */ |
||
| 237 | public function offsetExists($offset) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Offset to retrieve |
||
| 244 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
| 245 | * @param mixed $offset <p> |
||
| 246 | * The offset to retrieve. |
||
| 247 | * </p> |
||
| 248 | * @return mixed Can return all value types. |
||
| 249 | * @since 5.0.0 |
||
| 250 | */ |
||
| 251 | public function offsetGet($offset) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Offset to set |
||
| 258 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
| 259 | * @param mixed $offset <p> |
||
| 260 | * The offset to assign the value to. |
||
| 261 | * </p> |
||
| 262 | * @param mixed $value <p> |
||
| 263 | * The value to set. |
||
| 264 | * </p> |
||
| 265 | * @return void |
||
| 266 | * @since 5.0.0 |
||
| 267 | */ |
||
| 268 | public function offsetSet($offset, $value) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Offset to unset |
||
| 275 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
| 276 | * @param mixed $offset <p> |
||
| 277 | * The offset to unset. |
||
| 278 | * </p> |
||
| 279 | * @return void |
||
| 280 | * @since 5.0.0 |
||
| 281 | */ |
||
| 282 | public function offsetUnset($offset) |
||
| 286 | } |
||
| 287 |
This check marks private properties in classes that are never used. Those properties can be removed.