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 |
||
43 | class AnyDataset |
||
44 | { |
||
45 | |||
46 | /** |
||
47 | * Internal structure represent the current Row |
||
48 | * |
||
49 | *@var Row[] |
||
50 | */ |
||
51 | private $collection; |
||
52 | |||
53 | /** |
||
54 | * Current node anydataset works |
||
55 | * @var int |
||
56 | */ |
||
57 | private $currentRow; |
||
58 | |||
59 | /** |
||
60 | * Path to anydataset file |
||
61 | * @var string |
||
62 | */ |
||
63 | private $path; |
||
64 | |||
65 | /** |
||
66 | * |
||
67 | * @param string $file |
||
68 | * @throws InvalidArgumentException |
||
69 | */ |
||
70 | 19 | public function __construct($file = null) |
|
71 | { |
||
72 | 19 | $this->collection = array(); |
|
73 | 19 | $this->currentRow = -1; |
|
74 | |||
75 | 19 | $this->path = null; |
|
76 | 19 | if (!is_null($file)) { |
|
77 | 7 | if (!is_string($file)) { |
|
78 | throw new \InvalidArgumentException('I expected a string as a file name'); |
||
79 | } |
||
80 | |||
81 | 7 | $ext = pathinfo($file, PATHINFO_EXTENSION); |
|
82 | 7 | if (empty($ext)) { |
|
83 | 7 | $file .= '.anydata.xml'; |
|
84 | 7 | } |
|
85 | 7 | $this->path = $file; |
|
86 | |||
87 | 7 | $this->createFrom($this->path); |
|
88 | 7 | } |
|
89 | 19 | } |
|
90 | |||
91 | /** |
||
92 | * Private method used to read and populate anydataset class from specified file |
||
93 | * @param string $filepath Path and Filename to be read |
||
94 | */ |
||
95 | 7 | private function createFrom($filepath) |
|
119 | |||
120 | /** |
||
121 | * Returns the AnyDataset XML representative structure. |
||
122 | * @return string XML String |
||
123 | */ |
||
124 | 1 | public function xml() |
|
128 | |||
129 | /** |
||
130 | * Returns the AnyDataset XmlDocument representive object |
||
131 | * @return \DOMDocument XmlDocument object |
||
132 | */ |
||
133 | 2 | public function getAsDom() |
|
134 | { |
||
135 | 2 | $anyDataSet = XmlUtil::createXmlDocumentFromStr("<anydataset></anydataset>"); |
|
136 | 2 | $nodeRoot = $anyDataSet->getElementsByTagName("anydataset")->item(0); |
|
137 | 2 | foreach ($this->collection as $sr) { |
|
138 | 2 | $row = $sr->getAsDom(); |
|
139 | 2 | $nodeRow = $row->getElementsByTagName("row")->item(0); |
|
140 | 2 | $newRow = XmlUtil::createChild($nodeRoot, "row"); |
|
141 | 2 | XmlUtil::addNodeFromNode($newRow, $nodeRow); |
|
142 | 2 | } |
|
143 | |||
144 | 2 | return $anyDataSet; |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * |
||
149 | * @param string $file |
||
150 | * @throws DatabaseException |
||
151 | */ |
||
152 | 1 | public function save($file = null) |
|
168 | |||
169 | /** |
||
170 | * Append one row to AnyDataset. |
||
171 | * |
||
172 | * @param Row $singleRow |
||
173 | * @return void |
||
174 | */ |
||
175 | 14 | public function appendRow($singleRow = null) |
|
193 | |||
194 | /** |
||
195 | * Enter description here... |
||
196 | * |
||
197 | * @param GenericIterator $iterator |
||
198 | */ |
||
199 | 1 | public function import($iterator) |
|
205 | |||
206 | /** |
||
207 | * Insert one row before specified position. |
||
208 | * @param int $rowNumber |
||
209 | * @param mixed $row |
||
210 | */ |
||
211 | 1 | public function insertRowBefore($rowNumber, $row = null) |
|
224 | |||
225 | /** |
||
226 | * |
||
227 | * @param mixed $row |
||
228 | */ |
||
229 | 2 | public function removeRow($row = null) |
|
252 | |||
253 | /** |
||
254 | * Add a single string field to an existing row |
||
255 | * @param string $name - Field name |
||
256 | * @param string $value - Field value |
||
257 | * @return void |
||
258 | */ |
||
259 | 10 | public function addField($name, $value) |
|
266 | |||
267 | /** |
||
268 | * Get an Iterator filtered by an IteratorFilter |
||
269 | * @param IteratorFilter $itf |
||
270 | * @return GenericIterator |
||
271 | */ |
||
272 | 18 | public function getIterator(IteratorFilter $itf = null) |
|
280 | |||
281 | /** |
||
282 | * @desc |
||
283 | * @param IteratorFilter $itf |
||
284 | * @param string $fieldName |
||
285 | * @return array |
||
286 | */ |
||
287 | 1 | public function getArray($itf, $fieldName) |
|
297 | |||
298 | /** |
||
299 | * |
||
300 | * @param string $field |
||
301 | * @return void |
||
302 | */ |
||
303 | 1 | public function sort($field) |
|
313 | |||
314 | /** |
||
315 | * @param Row[] $seq |
||
316 | * @param $field |
||
317 | * @return array |
||
318 | */ |
||
319 | 1 | protected function quickSortExec($seq, $field) |
|
343 | |||
344 | /** |
||
345 | * @param $document |
||
346 | * @return array|string |
||
347 | */ |
||
348 | public static function fixUTF8($document) |
||
352 | } |
||
353 |