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 |
||
20 | abstract class Query implements Iterator { |
||
21 | |||
22 | /** |
||
23 | * The current record in the interator. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $currentRecord = null; |
||
28 | |||
29 | /** |
||
30 | * The number of the current row in the interator. |
||
31 | * |
||
32 | * @var int |
||
33 | */ |
||
34 | protected $rowNum = -1; |
||
35 | |||
36 | /** |
||
37 | * Flag to keep track of whether iteration has begun, to prevent unnecessary seeks |
||
38 | * |
||
39 | * @var bool |
||
40 | */ |
||
41 | protected $queryHasBegun = false; |
||
42 | |||
43 | /** |
||
44 | * Return an array containing all the values from a specific column. If no column is set, then the first will be |
||
45 | * returned |
||
46 | * |
||
47 | * @param string $column |
||
48 | * @return array |
||
49 | */ |
||
50 | public function column($column = null) { |
||
63 | |||
64 | /** |
||
65 | * Return an array containing all values in the leftmost column, where the keys are the |
||
66 | * same as the values. |
||
67 | * |
||
68 | * @return array |
||
69 | */ |
||
70 | View Code Duplication | public function keyedColumn() { |
|
78 | |||
79 | /** |
||
80 | * Return a map from the first column to the second column. |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | View Code Duplication | public function map() { |
|
93 | |||
94 | /** |
||
95 | * Returns the next record in the iterator. |
||
96 | * |
||
97 | * @return array |
||
98 | */ |
||
99 | public function record() { |
||
102 | |||
103 | /** |
||
104 | * Returns the first column of the first record. |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | public function value() { |
||
112 | |||
113 | /** |
||
114 | * Return an HTML table containing the full result-set |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | public function table() { |
||
144 | |||
145 | /** |
||
146 | * Iterator function implementation. Rewind the iterator to the first item and return it. |
||
147 | * Makes use of {@link seek()} and {@link numRecords()}, takes care of the plumbing. |
||
148 | * |
||
149 | * @return array |
||
150 | */ |
||
151 | public function rewind() { |
||
158 | |||
159 | /** |
||
160 | * Iterator function implementation. Return the current item of the iterator. |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | public function current() { |
||
171 | |||
172 | /** |
||
173 | * Iterator function implementation. Return the first item of this iterator. |
||
174 | * |
||
175 | * @return array |
||
176 | */ |
||
177 | public function first() { |
||
181 | |||
182 | /** |
||
183 | * Iterator function implementation. Return the row number of the current item. |
||
184 | * |
||
185 | * @return int |
||
186 | */ |
||
187 | public function key() { |
||
190 | |||
191 | /** |
||
192 | * Iterator function implementation. Return the next record in the iterator. |
||
193 | * Makes use of {@link nextRecord()}, takes care of the plumbing. |
||
194 | * |
||
195 | * @return array |
||
196 | */ |
||
197 | public function next() { |
||
203 | |||
204 | /** |
||
205 | * Iterator function implementation. Check if the iterator is pointing to a valid item. |
||
206 | * |
||
207 | * @return bool |
||
208 | */ |
||
209 | public function valid() { |
||
213 | |||
214 | /** |
||
215 | * Return the next record in the query result. |
||
216 | * |
||
217 | * @return array |
||
218 | */ |
||
219 | abstract public function nextRecord(); |
||
220 | |||
221 | /** |
||
222 | * Return the total number of items in the query result. |
||
223 | * |
||
224 | * @return int |
||
225 | */ |
||
226 | abstract public function numRecords(); |
||
227 | |||
228 | /** |
||
229 | * Go to a specific row number in the query result and return the record. |
||
230 | * |
||
231 | * @param int $rowNum Row number to go to. |
||
232 | * @return array |
||
233 | */ |
||
234 | abstract public function seek($rowNum); |
||
235 | } |
||
236 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: