Complex classes like BaseResource 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 BaseResource, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | abstract class BaseResource implements \Iterator |
||
| 13 | { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * BaseResource constructor. |
||
| 17 | * |
||
| 18 | * @param object $descriptor |
||
| 19 | * @param null|string $basePath |
||
| 20 | * |
||
| 21 | * @param bool $skipValidations |
||
| 22 | * |
||
| 23 | * @throws \frictionlessdata\datapackage\Exceptions\ResourceValidationFailedException |
||
| 24 | */ |
||
| 25 | public function __construct($descriptor, $basePath, $skipValidations = false) |
||
| 37 | |||
| 38 | public static function handlesDescriptor($descriptor) |
||
| 42 | |||
| 43 | public function read($readOptions = null) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Loads $this->dataStreams based on $this->path() and $this->data |
||
| 80 | * @return BaseDataStream[]|null |
||
| 81 | */ |
||
| 82 | public function dataStreams() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @return object |
||
| 100 | */ |
||
| 101 | public function descriptor() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | public function name() |
||
| 113 | |||
| 114 | public function path() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * If the resource's $path is local (non-http) |
||
| 130 | * @return bool |
||
| 131 | */ |
||
| 132 | public function isLocal() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * If the resource's $path is remote (http) |
||
| 139 | * @return bool |
||
| 140 | */ |
||
| 141 | public function isRemote() |
||
| 146 | |||
| 147 | public function data() |
||
| 151 | |||
| 152 | // standard iterator functions - to iterate over the data sources |
||
| 153 | public function rewind() |
||
| 161 | |||
| 162 | public function current() |
||
| 166 | |||
| 167 | public function key() |
||
| 171 | |||
| 172 | public function next() |
||
| 176 | |||
| 177 | public function valid() |
||
| 194 | |||
| 195 | public function getFileExtension() |
||
| 199 | |||
| 200 | public function save($baseFilename) |
||
| 219 | |||
| 220 | public static function validateDataSource($dataSource, $basePath = null) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * allows extending classes to add custom sources |
||
| 236 | * used by unit tests to add a mock http source. |
||
| 237 | * |
||
| 238 | * @param string $dataSource |
||
| 239 | * @param string|null $basePath |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public static function normalizeDataSource($dataSource, string $basePath = null) |
||
| 255 | |||
| 256 | protected $descriptor; |
||
| 257 | protected $basePath; |
||
| 258 | protected $skipValidations = false; |
||
| 259 | protected $currentDataPosition = 0; |
||
| 260 | protected $currentDataStream = 0; |
||
| 261 | protected $dataStreams = null; |
||
| 262 | |||
| 263 | protected function validateResource() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param string $dataSource |
||
| 270 | * |
||
| 271 | * @return BaseDataStream |
||
| 272 | */ |
||
| 273 | abstract protected function getDataStream($dataSource); |
||
| 274 | |||
| 275 | abstract protected function getInlineDataStream($data); |
||
| 276 | |||
| 277 | protected static function handlesProfile($profile) |
||
| 281 | } |
||
| 282 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.