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:
| 1 | <?php  | 
            ||
| 12 | class Factory  | 
            ||
| 13 | { | 
            ||
| 14 | /**  | 
            ||
| 15 | * how many lines to validate sample when validating data streams.  | 
            ||
| 16 | */  | 
            ||
| 17 | const VALIDATE_PEEK_LINES = 10;  | 
            ||
| 18 | |||
| 19 | /**  | 
            ||
| 20 | * load, validate and create a datapackage object  | 
            ||
| 21 | * supports loading from the following sources:  | 
            ||
| 22 | * - native PHP object containing the descriptor  | 
            ||
| 23 | * - JSON encoded object  | 
            ||
| 24 | * - URL (must be in either 'http' or 'https' schemes)  | 
            ||
| 25 | * - local filesystem (POSIX) path.  | 
            ||
| 26 | *  | 
            ||
| 27 | * @param mixed $source  | 
            ||
| 28 | * @param null|string $basePath optional, required only if you want to use relative paths  | 
            ||
| 29 | *  | 
            ||
| 30 | * @return Datapackages\BaseDatapackage  | 
            ||
| 31 | *  | 
            ||
| 32 | * @throws Exceptions\DatapackageInvalidSourceException  | 
            ||
| 33 | * @throws Exceptions\DatapackageValidationFailedException  | 
            ||
| 34 | */  | 
            ||
| 35 | public static function datapackage($source, $basePath = null)  | 
            ||
| 45 | |||
| 46 | /**  | 
            ||
| 47 | * create a resource object.  | 
            ||
| 48 | *  | 
            ||
| 49 | * @param object $descriptor  | 
            ||
| 50 | * @param null|string $basePath  | 
            ||
| 51 | * @param bool $skipValidations  | 
            ||
| 52 | *  | 
            ||
| 53 | * @return Resources\BaseResource  | 
            ||
| 54 | *  | 
            ||
| 55 | * @throws Exceptions\ResourceValidationFailedException  | 
            ||
| 56 | */  | 
            ||
| 57 | public static function resource($descriptor, $basePath = null, $skipValidations = false)  | 
            ||
| 64 | |||
| 65 | /**  | 
            ||
| 66 | * validates a given datapackage descriptor  | 
            ||
| 67 | * will load all resources, and sample 10 lines of data from each data source.  | 
            ||
| 68 | *  | 
            ||
| 69 | * @param mixed $source datapackage source - same as in datapackage function  | 
            ||
| 70 | * @param null|string $basePath same as in datapackage function  | 
            ||
| 71 | *  | 
            ||
| 72 | * @return Validators\DatapackageValidationError[]  | 
            ||
| 73 | */  | 
            ||
| 74 | public static function validate($source, $basePath = null)  | 
            ||
| 140 | |||
| 141 | public static function registerDatapackageClass($datapackageClass)  | 
            ||
| 145 | |||
| 146 | public static function clearRegisteredDatapackageClasses()  | 
            ||
| 150 | |||
| 151 | /**  | 
            ||
| 152 | * @param $descriptor  | 
            ||
| 153 | *  | 
            ||
| 154 | * @return BaseDatapackage::class  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 155 | */  | 
            ||
| 156 | View Code Duplication | public static function getDatapackageClass($descriptor)  | 
            |
| 181 | |||
| 182 | public static function registerResourceClass($resourceClass)  | 
            ||
| 186 | |||
| 187 | public static function clearRegisteredResourceClasses()  | 
            ||
| 191 | |||
| 192 | /**  | 
            ||
| 193 | * @param $descriptor  | 
            ||
| 194 | *  | 
            ||
| 195 | * @return BaseResource::class  | 
            ||
| 196 | */  | 
            ||
| 197 | View Code Duplication | public static function getResourceClass($descriptor)  | 
            |
| 223 | |||
| 224 | protected static $registeredDatapackageClasses = [];  | 
            ||
| 225 | protected static $registeredResourceClasses = [];  | 
            ||
| 226 | |||
| 227 | /**  | 
            ||
| 228 | * allows extending classes to add custom sources  | 
            ||
| 229 | * used by unit tests to add a mock http source.  | 
            ||
| 230 | */  | 
            ||
| 231 | protected static function normalizeHttpSource($source)  | 
            ||
| 235 | |||
| 236 | /**  | 
            ||
| 237 | * allows extending classes to add custom sources  | 
            ||
| 238 | * used by unit tests to add a mock http source.  | 
            ||
| 239 | */  | 
            ||
| 240 | protected static function isHttpSource($source)  | 
            ||
| 244 | |||
| 245 | /**  | 
            ||
| 246 | * loads the datapackage descriptor from different sources  | 
            ||
| 247 | * returns an object containing:  | 
            ||
| 248 | * - the datapackage descriptor as native php object  | 
            ||
| 249 | * - normalized basePath.  | 
            ||
| 250 | *  | 
            ||
| 251 | * @param $source  | 
            ||
| 252 | * @param $basePath  | 
            ||
| 253 | *  | 
            ||
| 254 | * @return object  | 
            ||
| 255 | *  | 
            ||
| 256 | * @throws Exceptions\DatapackageInvalidSourceException  | 
            ||
| 257 | */  | 
            ||
| 258 | protected static function loadSource($source, $basePath)  | 
            ||
| 313 | }  | 
            ||
| 314 | 
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.