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 $path; |
||
63 | |||
64 | /** |
||
65 | * @param string $file |
||
66 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
67 | * @throws \ByJG\Util\Exception\XmlUtilException |
||
68 | */ |
||
69 | 19 | public function __construct($file = null) |
|
70 | { |
||
71 | 19 | $this->collection = array(); |
|
72 | 19 | $this->currentRow = -1; |
|
73 | |||
74 | 19 | $this->path = null; |
|
75 | 19 | if (!is_null($file)) { |
|
76 | 7 | if (!is_string($file)) { |
|
77 | throw new \InvalidArgumentException('I expected a string as a file name'); |
||
78 | } |
||
79 | |||
80 | 7 | $ext = pathinfo($file, PATHINFO_EXTENSION); |
|
81 | 7 | if (empty($ext)) { |
|
82 | 7 | $file .= '.anydata.xml'; |
|
83 | } |
||
84 | 7 | $this->path = $file; |
|
85 | |||
86 | 7 | $this->createFrom($this->path); |
|
87 | } |
||
88 | 19 | } |
|
89 | |||
90 | /** |
||
91 | * Private method used to read and populate anydataset class from specified file |
||
92 | * |
||
93 | * @param string $filepath Path and Filename to be read |
||
94 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
95 | * @throws \ByJG\Util\Exception\XmlUtilException |
||
96 | */ |
||
97 | 7 | private function createFrom($filepath) |
|
98 | { |
||
99 | 7 | if (file_exists($filepath)) { |
|
100 | 7 | $anyDataSet = XmlUtil::createXmlDocumentFromFile($filepath); |
|
101 | 7 | $this->collection = array(); |
|
102 | |||
103 | 7 | $rows = $anyDataSet->getElementsByTagName("row"); |
|
104 | 7 | foreach ($rows as $row) { |
|
105 | 7 | $sr = new Row(); |
|
106 | 7 | $fields = $row->getElementsByTagName("field"); |
|
107 | 7 | foreach ($fields as $field) { |
|
108 | 7 | $attr = $field->attributes->getNamedItem("name"); |
|
109 | 7 | if (is_null($attr)) { |
|
110 | throw new \InvalidArgumentException('Malformed anydataset file ' . basename($filepath)); |
||
111 | } |
||
112 | |||
113 | 7 | $sr->addField($attr->nodeValue, $field->nodeValue); |
|
114 | } |
||
115 | 7 | $sr->acceptChanges(); |
|
116 | 7 | $this->collection[] = $sr; |
|
117 | } |
||
118 | 7 | $this->currentRow = count($this->collection) - 1; |
|
119 | } |
||
120 | 7 | } |
|
121 | |||
122 | /** |
||
123 | * Returns the AnyDataset XML representative structure. |
||
124 | * |
||
125 | * @return string XML String |
||
126 | * @throws \ByJG\Util\Exception\XmlUtilException |
||
127 | */ |
||
128 | 1 | public function xml() |
|
132 | |||
133 | /** |
||
134 | * Returns the AnyDataset XmlDocument representive object |
||
135 | * |
||
136 | * @return \DOMDocument XmlDocument object |
||
137 | * @throws \ByJG\Util\Exception\XmlUtilException |
||
138 | */ |
||
139 | 2 | public function getAsDom() |
|
152 | |||
153 | /** |
||
154 | * @param string $file |
||
155 | * @throws DatabaseException |
||
156 | * @throws \ByJG\Util\Exception\XmlUtilException |
||
157 | */ |
||
158 | 1 | public function save($file = null) |
|
174 | |||
175 | /** |
||
176 | * Append one row to AnyDataset. |
||
177 | * |
||
178 | * @param Row $singleRow |
||
179 | * @return void |
||
180 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
181 | */ |
||
182 | 14 | public function appendRow($singleRow = null) |
|
200 | |||
201 | /** |
||
202 | * Enter description here... |
||
203 | * |
||
204 | * @param GenericIterator $iterator |
||
205 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
206 | */ |
||
207 | 1 | public function import($iterator) |
|
213 | |||
214 | /** |
||
215 | * Insert one row before specified position. |
||
216 | * |
||
217 | * @param int $rowNumber |
||
218 | * @param mixed $row |
||
219 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
220 | */ |
||
221 | 1 | public function insertRowBefore($rowNumber, $row = null) |
|
234 | |||
235 | /** |
||
236 | * |
||
237 | * @param mixed $row |
||
238 | */ |
||
239 | 2 | public function removeRow($row = null) |
|
262 | |||
263 | /** |
||
264 | * Add a single string field to an existing row |
||
265 | * |
||
266 | * @param string $name - Field name |
||
267 | * @param string $value - Field value |
||
268 | * @return void |
||
269 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
270 | */ |
||
271 | 10 | public function addField($name, $value) |
|
278 | |||
279 | /** |
||
280 | * Get an Iterator filtered by an IteratorFilter |
||
281 | * @param IteratorFilter $itf |
||
282 | * @return GenericIterator |
||
283 | */ |
||
284 | 18 | public function getIterator(IteratorFilter $itf = null) |
|
292 | |||
293 | /** |
||
294 | * @desc |
||
295 | * @param IteratorFilter $itf |
||
296 | * @param string $fieldName |
||
297 | * @return array |
||
298 | */ |
||
299 | 1 | public function getArray($itf, $fieldName) |
|
309 | |||
310 | /** |
||
311 | * |
||
312 | * @param string $field |
||
313 | * @return void |
||
314 | */ |
||
315 | 1 | public function sort($field) |
|
325 | |||
326 | /** |
||
327 | * @param Row[] $seq |
||
328 | * @param $field |
||
329 | * @return array |
||
330 | */ |
||
331 | 1 | protected function quickSortExec($seq, $field) |
|
355 | } |
||
356 |