Complex classes like BaseDatapackage 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 BaseDatapackage, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | abstract class BaseDatapackage implements \Iterator |
||
13 | { |
||
14 | /** |
||
15 | * BaseDatapackage constructor. |
||
16 | * |
||
17 | * @param object $descriptor |
||
18 | * @param null|string $basePath |
||
19 | * |
||
20 | * @throws DatapackageValidationFailedException |
||
21 | */ |
||
22 | public function __construct($descriptor, $basePath = null, $skipValidations = false) |
||
31 | |||
32 | public static function create($name, $resources, $basePath = null) |
||
44 | |||
45 | public function revalidate() |
||
53 | |||
54 | public static function handlesDescriptor($descriptor) |
||
58 | |||
59 | /** |
||
60 | * returns the descriptor as-is, without adding default values or normalizing. |
||
61 | * |
||
62 | * @return object |
||
63 | */ |
||
64 | public function descriptor() |
||
68 | |||
69 | public function resources() |
||
78 | |||
79 | public function getResource($name) |
||
88 | |||
89 | public function addResource($name, $resource) |
||
115 | |||
116 | // TODO: remove this function and use the getResource / addResource directly (will need to modify a lot of tests code) |
||
117 | public function resource($name, $resource = null) |
||
125 | |||
126 | public function removeResource($name) |
||
139 | |||
140 | public function saveDescriptor($filename) |
||
144 | |||
145 | // standard iterator functions - to iterate over the resources |
||
146 | public function rewind() |
||
150 | |||
151 | public function current() |
||
155 | |||
156 | public function key() |
||
160 | |||
161 | public function next() |
||
165 | |||
166 | public function valid() |
||
170 | |||
171 | public function save($zip_filename) |
||
203 | |||
204 | public function copy() |
||
205 | { |
||
206 | return new static($this->descriptor, $this->basePath, true); |
||
207 | } |
||
208 | |||
209 | protected $descriptor; |
||
210 | protected $currentResourcePosition = 0; |
||
211 | protected $basePath; |
||
212 | protected $skipValidations = false; |
||
213 | |||
214 | /** |
||
215 | * called by the resources iterator for each iteration. |
||
216 | * |
||
217 | * @param object $descriptor |
||
218 | * |
||
219 | * @return \frictionlessdata\datapackage\Resources\BaseResource |
||
220 | */ |
||
221 | protected function initResource($descriptor) |
||
225 | |||
226 | protected function datapackageValidate() |
||
230 | |||
231 | protected static function handlesProfile($profile) |
||
235 | } |
||
236 |
This check looks for function calls that miss required arguments.