Complex classes like Properties 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 Properties, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class Properties extends AbstractFileParser |
||
37 | { |
||
38 | protected $parsedFile; |
||
39 | |||
40 | /** |
||
41 | * Loads a PROPERTIES file as an array. |
||
42 | * |
||
43 | * @param string $path File path |
||
44 | * |
||
45 | * @throws ParseException If there is an error parsing PROPERTIES file |
||
46 | * |
||
47 | * @return array The parsed data |
||
48 | * |
||
49 | * @since 0.2.4 |
||
50 | */ |
||
51 | 6 | public function parse($path) |
|
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | * |
||
72 | * @return array Supported extensions |
||
73 | * |
||
74 | * @since 0.1.0 |
||
75 | */ |
||
76 | 3 | public function getSupportedFileExtensions() |
|
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | * |
||
84 | * @return array The exteacted data |
||
85 | * |
||
86 | * @since 0.2.4 |
||
87 | * @codeCoverageIgnore |
||
88 | */ |
||
89 | public function extractData() |
||
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | * |
||
194 | * @param array|null $analysis Configuration items |
||
195 | * |
||
196 | * @return array The configuration items |
||
197 | * |
||
198 | * @since 0.2.4 |
||
199 | * @codeCoverageIgnore |
||
200 | */ |
||
201 | private function unescapeProperties($analysis) |
||
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | * |
||
213 | * @param string $field Field name |
||
214 | * @param array|null $analysis Configuration items |
||
215 | * |
||
216 | * @return array Configuration items after deletion |
||
217 | * |
||
218 | * @since 0.2.4 |
||
219 | * @codeCoverageIgnore |
||
220 | */ |
||
221 | private function deleteFields($field, $analysis) |
||
231 | |||
232 | /** |
||
233 | * {@inheritdoc} |
||
234 | * |
||
235 | * @param string|bool|null $file File path |
||
236 | * |
||
237 | * @return array Configuration items |
||
238 | * |
||
239 | * @since 0.2.4 |
||
240 | * @codeCoverageIgnore |
||
241 | */ |
||
242 | public function getProperties($file = null) |
||
259 | |||
260 | /** |
||
261 | * Loads in the given file and parses it. |
||
262 | * |
||
263 | * @param string|bool|null $file File to load |
||
264 | * |
||
265 | * @return array The parsed file data |
||
266 | * |
||
267 | * @since 0.2.4 |
||
268 | * @codeCoverageIgnore |
||
269 | */ |
||
270 | protected function loadFile($file = null) |
||
282 | |||
283 | /** |
||
284 | * Returns the formatted configuration file contents. |
||
285 | * |
||
286 | * @param array $contents configuration array |
||
287 | * |
||
288 | * @return string formatted configuration file contents |
||
289 | * |
||
290 | * @since 0.2.4 |
||
291 | * @codeCoverageIgnore |
||
292 | */ |
||
293 | protected function exportFormat($contents = null) |
||
299 | } |
||
300 | |||
302 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.