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:
Complex classes like Statement often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Statement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class Statement implements \IteratorAggregate, \Doctrine\DBAL\Driver\Statement |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var integer |
||
| 35 | */ |
||
| 36 | private $portability; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var \Doctrine\DBAL\Driver\Statement |
||
| 40 | */ |
||
| 41 | private $stmt; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var integer |
||
| 45 | */ |
||
| 46 | private $case; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var integer |
||
| 50 | */ |
||
| 51 | private $defaultFetchMode = PDO::FETCH_BOTH; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Wraps <tt>Statement</tt> and applies portability measures. |
||
| 55 | * |
||
| 56 | * @param \Doctrine\DBAL\Driver\Statement $stmt |
||
| 57 | * @param \Doctrine\DBAL\Portability\Connection $conn |
||
| 58 | */ |
||
| 59 | 15 | public function __construct($stmt, Connection $conn) |
|
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | 1 | public function bindParam($column, &$variable, $type = null, $length = null) |
|
| 73 | /** |
||
| 74 | * {@inheritdoc} |
||
| 75 | */ |
||
| 76 | |||
| 77 | 1 | public function bindValue($param, $value, $type = null) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | */ |
||
| 85 | 1 | public function closeCursor() |
|
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritdoc} |
||
| 92 | */ |
||
| 93 | 1 | public function columnCount() |
|
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | */ |
||
| 101 | 1 | public function errorCode() |
|
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | */ |
||
| 109 | 1 | public function errorInfo() |
|
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritdoc} |
||
| 116 | */ |
||
| 117 | 3 | public function execute($params = null) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | */ |
||
| 125 | 6 | public function setFetchMode($fetchMode, $arg1 = null, $arg2 = null) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | 3 | public function getIterator() |
|
| 141 | |||
| 142 | /** |
||
| 143 | * {@inheritdoc} |
||
| 144 | */ |
||
| 145 | 2 | public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset = 0) |
|
| 156 | 2 | ||
| 157 | private function shouldFixCase(int $fetchMode) : bool |
||
| 162 | 6 | ||
| 163 | /** |
||
| 164 | 6 | * {@inheritdoc} |
|
| 165 | */ |
||
| 166 | 6 | public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @param mixed $row |
||
| 204 | 5 | * @param integer $iterateRow |
|
| 205 | * @param boolean $fixCase |
||
| 206 | 5 | * |
|
| 207 | 2 | * @return array |
|
| 208 | */ |
||
| 209 | protected function fixRow($row, $iterateRow, $fixCase) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * {@inheritdoc} |
||
| 234 | */ |
||
| 235 | public function fetchColumn($columnIndex = 0) |
||
| 249 | |||
| 250 | 1 | /** |
|
| 251 | * {@inheritdoc} |
||
| 252 | */ |
||
| 253 | public function rowCount() |
||
| 257 | } |
||
| 258 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: