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() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * {@inheritdoc} |
||
| 191 | * |
||
| 192 | * @param array $analysis Configuration items |
||
| 193 | * |
||
| 194 | * @return array The configuration items |
||
| 195 | * |
||
| 196 | * @since 0.2.4 |
||
| 197 | * @codeCoverageIgnore |
||
| 198 | */ |
||
| 199 | private function unescapeProperties($analysis) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * {@inheritdoc} |
||
| 210 | * |
||
| 211 | * @param string $field Field name |
||
| 212 | * @param array $analysis Configuration items |
||
| 213 | * |
||
| 214 | * @return array Configuration items after deletion |
||
| 215 | * |
||
| 216 | * @since 0.2.4 |
||
| 217 | * @codeCoverageIgnore |
||
| 218 | */ |
||
| 219 | private function deleteFields($field, $analysis) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritdoc} |
||
| 232 | * |
||
| 233 | * @param string|null $file File path |
||
| 234 | * |
||
| 235 | * @return array Configuration items |
||
| 236 | * |
||
| 237 | * @since 0.2.4 |
||
| 238 | * @codeCoverageIgnore |
||
| 239 | */ |
||
| 240 | public function getProperties($file = null) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Loads in the given file and parses it. |
||
| 260 | * |
||
| 261 | * @param string|bool|null $file File to load |
||
| 262 | * |
||
| 263 | * @return array The parsed file data |
||
| 264 | * |
||
| 265 | * @since 0.2.4 |
||
| 266 | * @codeCoverageIgnore |
||
| 267 | */ |
||
| 268 | protected function loadFile($file = null) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Returns the formatted configuration file contents. |
||
| 283 | * |
||
| 284 | * @param array $contents configuration array |
||
| 285 | * |
||
| 286 | * @return string formatted configuration file contents |
||
| 287 | * |
||
| 288 | * @since 0.2.4 |
||
| 289 | * @codeCoverageIgnore |
||
| 290 | */ |
||
| 291 | protected function exportFormat($contents = null) |
||
| 297 | } |
||
| 298 | |||
| 300 |
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.