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 |
||
| 18 | abstract class Query implements \IteratorAggregate |
||
| 19 | { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Return an array containing all the values from a specific column. If no column is set, then the first will be |
||
| 23 | * returned |
||
| 24 | * |
||
| 25 | * @param string $column |
||
| 26 | * @return array |
||
| 27 | */ |
||
| 28 | public function column($column = null) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Return an array containing all values in the leftmost column, where the keys are the |
||
| 45 | * same as the values. |
||
| 46 | * |
||
| 47 | * @return array |
||
| 48 | */ |
||
| 49 | View Code Duplication | public function keyedColumn() |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Return a map from the first column to the second column. |
||
| 62 | * |
||
| 63 | * @return array |
||
| 64 | */ |
||
| 65 | View Code Duplication | public function map() |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Returns the first record in the result |
||
| 78 | * |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | public function record() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @deprecated Use record() instead |
||
| 88 | * @return array |
||
| 89 | */ |
||
| 90 | public function first() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Returns the first column of the first record. |
||
| 97 | * |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | public function value() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Return an HTML table containing the full result-set |
||
| 111 | * |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | public function table() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Return the next record in the query result. |
||
| 146 | * |
||
| 147 | * @return array |
||
| 148 | */ |
||
| 149 | abstract public function getIterator(); |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Return the total number of items in the query result. |
||
| 153 | * |
||
| 154 | * @return int |
||
| 155 | */ |
||
| 156 | abstract public function numRecords(); |
||
| 157 | } |
||
| 158 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: