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 |
||
42 | class AnyDataset |
||
43 | { |
||
44 | |||
45 | /** |
||
46 | * Internal structure represent the current Row |
||
47 | * |
||
48 | * @var Row[] |
||
49 | */ |
||
50 | private $collection; |
||
51 | |||
52 | /** |
||
53 | * Current node anydataset works |
||
54 | * @var int |
||
55 | */ |
||
56 | private $currentRow; |
||
57 | |||
58 | /** |
||
59 | * Path to anydataset file |
||
60 | * @var string |
||
61 | */ |
||
62 | private $filename; |
||
63 | |||
64 | /** |
||
65 | * @param string $filename |
||
66 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
67 | * @throws \ByJG\Util\Exception\XmlUtilException |
||
68 | */ |
||
69 | 14 | public function __construct($filename = null) |
|
81 | |||
82 | public function getFilename() |
||
86 | |||
87 | 14 | private function defineSavePath($file, $closure) |
|
103 | |||
104 | /** |
||
105 | * Private method used to read and populate anydataset class from specified file |
||
106 | * |
||
107 | * @param string $filepath Path and Filename to be read |
||
108 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
109 | * @throws \ByJG\Util\Exception\XmlUtilException |
||
110 | */ |
||
111 | 7 | private function createFrom($filepath) |
|
135 | |||
136 | /** |
||
137 | * Returns the AnyDataset XML representative structure. |
||
138 | * |
||
139 | * @return string XML String |
||
140 | * @throws \ByJG\Util\Exception\XmlUtilException |
||
141 | */ |
||
142 | 1 | public function xml() |
|
146 | |||
147 | /** |
||
148 | * Returns the AnyDataset XmlDocument representive object |
||
149 | * |
||
150 | * @return \DOMDocument XmlDocument object |
||
151 | * @throws \ByJG\Util\Exception\XmlUtilException |
||
152 | */ |
||
153 | 3 | public function getAsDom() |
|
166 | |||
167 | /** |
||
168 | * @param string $filename |
||
169 | * @throws DatabaseException |
||
170 | * @throws \ByJG\Util\Exception\XmlUtilException |
||
171 | */ |
||
172 | 2 | public function save($filename = null) |
|
182 | |||
183 | /** |
||
184 | * Append one row to AnyDataset. |
||
185 | * |
||
186 | * @param Row $singleRow |
||
187 | * @return void |
||
188 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
189 | */ |
||
190 | 10 | public function appendRow($singleRow = null) |
|
208 | |||
209 | /** |
||
210 | * Enter description here... |
||
211 | * |
||
212 | * @param GenericIterator $iterator |
||
213 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
214 | */ |
||
215 | 2 | public function import($iterator) |
|
221 | |||
222 | /** |
||
223 | * Insert one row before specified position. |
||
224 | * |
||
225 | * @param int $rowNumber |
||
226 | * @param mixed $row |
||
227 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
228 | */ |
||
229 | 1 | public function insertRowBefore($rowNumber, $row = null) |
|
242 | |||
243 | /** |
||
244 | * |
||
245 | * @param mixed $row |
||
246 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
247 | */ |
||
248 | 2 | public function removeRow($row = null) |
|
271 | |||
272 | /** |
||
273 | * Add a single string field to an existing row |
||
274 | * |
||
275 | * @param string $name - Field name |
||
276 | * @param string $value - Field value |
||
277 | * @return void |
||
278 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
279 | */ |
||
280 | 5 | public function addField($name, $value) |
|
287 | |||
288 | /** |
||
289 | * Get an Iterator filtered by an IteratorFilter |
||
290 | * @param IteratorFilter $itf |
||
291 | * @return GenericIterator |
||
292 | */ |
||
293 | 13 | public function getIterator(IteratorFilter $itf = null) |
|
301 | |||
302 | /** |
||
303 | * @desc |
||
304 | * @param IteratorFilter $itf |
||
305 | * @param string $fieldName |
||
306 | * @return array |
||
307 | */ |
||
308 | 1 | public function getArray($itf, $fieldName) |
|
318 | |||
319 | /** |
||
320 | * |
||
321 | * @param string $field |
||
322 | * @return void |
||
323 | */ |
||
324 | 1 | public function sort($field) |
|
334 | |||
335 | /** |
||
336 | * @param Row[] $seq |
||
337 | * @param $field |
||
338 | * @return array |
||
339 | */ |
||
340 | 1 | protected function quickSortExec($seq, $field) |
|
364 | } |
||
365 |