Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Factory 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 Factory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Factory |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * how many lines to validate sample when validating data streams. |
||
| 17 | */ |
||
| 18 | const VALIDATE_PEEK_LINES = 10; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * load, validate and create a datapackage object |
||
| 22 | * supports loading from the following sources: |
||
| 23 | * - native PHP object containing the descriptor |
||
| 24 | * - JSON encoded object |
||
| 25 | * - URL (must be in either 'http' or 'https' schemes) |
||
| 26 | * - local filesystem (POSIX) path. |
||
| 27 | * - local or remote zip file |
||
| 28 | * |
||
| 29 | * @param mixed $source |
||
| 30 | * @param null|string $basePath optional, required only if you want to use relative paths |
||
| 31 | * |
||
| 32 | * @return Datapackages\BaseDatapackage |
||
| 33 | * |
||
| 34 | * @throws Exceptions\DatapackageInvalidSourceException |
||
| 35 | * @throws Exceptions\DatapackageValidationFailedException |
||
| 36 | */ |
||
| 37 | public static function datapackage($source, $basePath = null) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * create a resource object. |
||
| 50 | * |
||
| 51 | * @param object $descriptor |
||
| 52 | * @param null|string $basePath |
||
| 53 | * @param bool $skipValidations |
||
| 54 | * |
||
| 55 | * @return Resources\BaseResource |
||
| 56 | * |
||
| 57 | * @throws Exceptions\ResourceValidationFailedException |
||
| 58 | */ |
||
| 59 | public static function resource($descriptor, $basePath = null, $skipValidations = false) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * validates a given datapackage descriptor |
||
| 69 | * will load all resources, and sample 10 lines of data from each data source. |
||
| 70 | * |
||
| 71 | * @param mixed $source datapackage source - same as in datapackage function |
||
| 72 | * @param null|string $basePath same as in datapackage function |
||
| 73 | * |
||
| 74 | * @return Validators\DatapackageValidationError[] |
||
| 75 | */ |
||
| 76 | public static function validate($source, $basePath = null) |
||
| 142 | |||
| 143 | public static function registerDatapackageClass($datapackageClass) |
||
| 147 | |||
| 148 | public static function clearRegisteredDatapackageClasses() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param $descriptor |
||
| 155 | * |
||
| 156 | * @return BaseDatapackage::class |
||
|
|
|||
| 157 | */ |
||
| 158 | View Code Duplication | public static function getDatapackageClass($descriptor) |
|
| 183 | |||
| 184 | public static function registerResourceClass($resourceClass) |
||
| 188 | |||
| 189 | public static function clearRegisteredResourceClasses() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param $descriptor |
||
| 196 | * |
||
| 197 | * @return BaseResource::class |
||
| 198 | */ |
||
| 199 | View Code Duplication | public static function getResourceClass($descriptor) |
|
| 225 | |||
| 226 | protected static $registeredDatapackageClasses = []; |
||
| 227 | protected static $registeredResourceClasses = []; |
||
| 228 | |||
| 229 | /** |
||
| 230 | * allows extending classes to add custom sources |
||
| 231 | * used by unit tests to add a mock http source. |
||
| 232 | */ |
||
| 233 | protected static function normalizeHttpSource($source) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * allows extending classes to add custom sources |
||
| 240 | * used by unit tests to add a mock http source. |
||
| 241 | */ |
||
| 242 | protected static function isHttpSource($source) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * loads the datapackage descriptor from different sources |
||
| 249 | * returns an object containing: |
||
| 250 | * - the datapackage descriptor as native php object |
||
| 251 | * - normalized basePath. |
||
| 252 | * |
||
| 253 | * @param $source |
||
| 254 | * @param $basePath |
||
| 255 | * |
||
| 256 | * @return object |
||
| 257 | * |
||
| 258 | * @throws Exceptions\DatapackageInvalidSourceException |
||
| 259 | */ |
||
| 260 | protected static function loadSource($source, $basePath) |
||
| 323 | |||
| 324 | protected static function isHttpZipSource($source) |
||
| 328 | |||
| 329 | protected static function isFileZipSource($source) |
||
| 333 | |||
| 334 | protected static function loadHttpZipSource($source) |
||
| 343 | |||
| 344 | protected static function loadFileZipSource($source) |
||
| 357 | } |
||
| 358 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.