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 |
||
| 14 | abstract class BaseDatapackage implements \Iterator |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * BaseDatapackage constructor. |
||
| 19 | * |
||
| 20 | * @param object $descriptor |
||
| 21 | * @param null|string $basePath |
||
| 22 | * |
||
| 23 | * @param bool $skipValidations |
||
| 24 | * |
||
| 25 | * @throws \frictionlessdata\datapackage\Exceptions\DatapackageValidationFailedException |
||
| 26 | */ |
||
| 27 | public function __construct($descriptor, $basePath = null, $skipValidations = false) |
||
| 36 | |||
| 37 | public static function create($name, $resources, $basePath = null) |
||
| 49 | |||
| 50 | public function revalidate() |
||
| 58 | |||
| 59 | public static function handlesDescriptor($descriptor) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * returns the descriptor as-is, without adding default values or normalizing. |
||
| 66 | * |
||
| 67 | * @return object |
||
| 68 | */ |
||
| 69 | public function descriptor() |
||
| 73 | |||
| 74 | public function resources() |
||
| 83 | |||
| 84 | public function getResource($name) |
||
| 93 | |||
| 94 | public function addResource($name, $resource) |
||
| 120 | |||
| 121 | // TODO: remove this function and use the getResource / addResource directly (will need to modify a lot of tests code) |
||
| 122 | public function resource($name, $resource = null) |
||
| 130 | |||
| 131 | public function removeResource($name) |
||
| 144 | |||
| 145 | public function saveDescriptor($filename) |
||
| 149 | |||
| 150 | // standard iterator functions - to iterate over the resources |
||
| 151 | public function rewind() |
||
| 155 | |||
| 156 | public function current() |
||
| 160 | |||
| 161 | public function key() |
||
| 165 | |||
| 166 | public function next() |
||
| 170 | |||
| 171 | public function valid() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param $zip_filename |
||
| 178 | * |
||
| 179 | * @throws \frictionlessdata\datapackage\Exceptions\DatapackageInvalidSourceException |
||
| 180 | * @throws \Exception |
||
| 181 | */ |
||
| 182 | public function save($zip_filename) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Make a new Datapackage object that is a copy of the current Datapackage. Bypasses validation. |
||
| 226 | * @return $this |
||
| 227 | * @throws DatapackageValidationFailedException |
||
| 228 | */ |
||
| 229 | protected function copy() |
||
| 233 | |||
| 234 | protected $descriptor; |
||
| 235 | protected $currentResourcePosition = 0; |
||
| 236 | protected $basePath; |
||
| 237 | protected $skipValidations = false; |
||
| 238 | |||
| 239 | /** |
||
| 240 | * called by the resources iterator for each iteration. |
||
| 241 | * |
||
| 242 | * @param object $descriptor |
||
| 243 | * |
||
| 244 | * @return \frictionlessdata\datapackage\Resources\BaseResource |
||
| 245 | * @throws \frictionlessdata\datapackage\Exceptions\ResourceValidationFailedException |
||
| 246 | */ |
||
| 247 | protected function initResource($descriptor) |
||
| 251 | |||
| 252 | protected function datapackageValidate() |
||
| 256 | |||
| 257 | protected static function handlesProfile($profile) |
||
| 261 | } |
||
| 262 |
This check looks for function calls that miss required arguments.