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 AnyDataset 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 AnyDataset, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | class AnyDataset |
||
42 | { |
||
43 | |||
44 | /** |
||
45 | * Internal structure represent the current SingleRow |
||
46 | * @var SingleRow[] |
||
47 | */ |
||
48 | private $_collection; |
||
49 | |||
50 | /** |
||
51 | * Current node anydataset works |
||
52 | * @var int |
||
53 | */ |
||
54 | private $_currentRow; |
||
55 | |||
56 | /** |
||
57 | * Path to anydataset file |
||
58 | * @var string |
||
59 | */ |
||
60 | private $_path; |
||
61 | |||
62 | /** |
||
63 | * |
||
64 | * @param string $file |
||
65 | * @throws InvalidArgumentException |
||
66 | */ |
||
67 | 8 | public function __construct($file = null) |
|
86 | |||
87 | /** |
||
88 | * Private method used to read and populate anydataset class from specified file |
||
89 | * @param string $filepath Path and Filename to be read |
||
90 | * @return null |
||
91 | */ |
||
92 | private function createFrom($filepath) |
||
116 | |||
117 | /** |
||
118 | * Returns the AnyDataset XML representative structure. |
||
119 | * @return string XML String |
||
120 | */ |
||
121 | 1 | public function xml() |
|
125 | |||
126 | /** |
||
127 | * Returns the AnyDataset XmlDocument representive object |
||
128 | * @return \DOMDocument XmlDocument object |
||
129 | */ |
||
130 | 1 | public function getDomObject() |
|
143 | |||
144 | /** |
||
145 | * |
||
146 | * @param string $file |
||
147 | * @throws DatabaseException |
||
148 | */ |
||
149 | 1 | public function save($file = null) |
|
165 | |||
166 | /** |
||
167 | * Append one row to AnyDataset. |
||
168 | * @param SingleRow $sr |
||
169 | * @return void |
||
170 | */ |
||
171 | 8 | public function appendRow($sr = null) |
|
189 | |||
190 | /** |
||
191 | * Enter description here... |
||
192 | * |
||
193 | * @param IteratorInterface $it |
||
194 | */ |
||
195 | 1 | public function import(IteratorInterface $it) |
|
201 | |||
202 | /** |
||
203 | * Insert one row before specified position. |
||
204 | * @param int $rowNumber |
||
205 | * @param mixed $row |
||
206 | */ |
||
207 | 1 | public function insertRowBefore($rowNumber, $row = null) |
|
220 | |||
221 | /** |
||
222 | * |
||
223 | * @param mixed $row |
||
224 | * @return null |
||
225 | */ |
||
226 | 2 | public function removeRow($row = null) |
|
249 | |||
250 | /** |
||
251 | * Add a single string field to an existing row |
||
252 | * @param string $name - Field name |
||
253 | * @param string $value - Field value |
||
254 | * @return void |
||
255 | */ |
||
256 | 7 | public function addField($name, $value) |
|
263 | |||
264 | /** |
||
265 | * Get an Iterator filtered by an IteratorFilter |
||
266 | * @param IteratorFilter $itf |
||
267 | * @return IteratorInterface |
||
268 | */ |
||
269 | 7 | public function getIterator(IteratorFilter $itf = null) |
|
277 | |||
278 | /** |
||
279 | * @desc |
||
280 | * @param IteratorFilter $itf |
||
281 | * @param string $fieldName |
||
282 | * @return array |
||
283 | */ |
||
284 | 1 | View Code Duplication | public function getArray($itf, $fieldName) |
294 | |||
295 | /** |
||
296 | * |
||
297 | * @param string $field |
||
298 | * @return void |
||
299 | */ |
||
300 | 1 | public function sort($field) |
|
310 | |||
311 | protected function quickSortExec($seq, $field) |
||
329 | |||
330 | /** |
||
331 | * @param $document |
||
332 | * @return array|string |
||
333 | */ |
||
334 | public static function fixUTF8($document) |
||
338 | } |
||
339 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.