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 | * BaseResource constructor. |
||
16 | * |
||
17 | * @param object $descriptor |
||
18 | * @param null|string $basePath |
||
19 | * |
||
20 | * @throws ResourceValidationFailedException |
||
21 | */ |
||
22 | public function __construct($descriptor, $basePath, $skipValidations = false) |
||
34 | |||
35 | public static function handlesDescriptor($descriptor) |
||
39 | |||
40 | public function read($readOptions=null) |
||
67 | |||
68 | public function dataStreams() |
||
83 | |||
84 | /** |
||
85 | * @return object |
||
86 | */ |
||
87 | public function descriptor() |
||
91 | |||
92 | /** |
||
93 | * @return string |
||
94 | */ |
||
95 | public function name() |
||
99 | |||
100 | public function path() |
||
113 | |||
114 | public function data() |
||
118 | |||
119 | // standard iterator functions - to iterate over the data sources |
||
120 | public function rewind() |
||
128 | |||
129 | public function current() |
||
133 | |||
134 | public function key() |
||
138 | |||
139 | public function next() |
||
143 | |||
144 | public function valid() |
||
161 | |||
162 | public function getFileExtension() |
||
166 | |||
167 | public function save($baseFilename) |
||
186 | |||
187 | public static function validateDataSource($dataSource, $basePath = null) |
||
200 | |||
201 | /** |
||
202 | * allows extending classes to add custom sources |
||
203 | * used by unit tests to add a mock http source. |
||
204 | * |
||
205 | * @param string $dataSource |
||
206 | * @param string $basePath |
||
207 | * |
||
208 | * @return string |
||
209 | */ |
||
210 | public static function normalizeDataSource($dataSource, $basePath = null) |
||
222 | |||
223 | protected $descriptor; |
||
224 | protected $basePath; |
||
225 | protected $skipValidations = false; |
||
226 | protected $currentDataPosition = 0; |
||
227 | protected $currentDataStream = 0; |
||
228 | protected $dataStreams = null; |
||
229 | |||
230 | protected function validateResource() |
||
234 | |||
235 | /** |
||
236 | * @param string $dataSource |
||
237 | * |
||
238 | * @return BaseDataStream |
||
239 | */ |
||
240 | abstract protected function getDataStream($dataSource); |
||
241 | |||
242 | abstract protected function getInlineDataStream($data); |
||
243 | |||
244 | protected static function handlesProfile($profile) |
||
248 | } |
||
249 |