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 | /** |
||
| 39 | * Loads a PROPERTIES file as an array. |
||
| 40 | * |
||
| 41 | * @param string $path File path |
||
| 42 | * |
||
| 43 | * @throws ParseException If there is an error parsing PROPERTIES file |
||
| 44 | * |
||
| 45 | * @return array The parsed data |
||
| 46 | * |
||
| 47 | * @since 0.2.4 |
||
| 48 | */ |
||
| 49 | public function parse($path) |
||
| 50 | { |
||
| 51 | 6 | $data = null; |
|
| 52 | |||
| 53 | 6 | $this->loadFile($path); |
|
| 54 | |||
| 55 | 6 | try { |
|
| 56 | $data = $this->getProperties(); |
||
| 57 | 6 | } catch (Exception $ex) { |
|
| 58 | 3 | throw new ParseException( |
|
| 59 | [ |
||
| 60 | 3 | 'message' => 'Error parsing PROPERTIES file', |
|
| 61 | 3 | 'exception' => $ex |
|
| 62 | ] |
||
| 63 | 2 | ); |
|
| 64 | } |
||
| 65 | |||
| 66 | 3 | return $data; |
|
| 67 | } |
||
| 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 | * Parse Java-Properties |
||
| 83 | * |
||
| 84 | * @return array The parsed data |
||
| 85 | * @since 0.2.6 |
||
| 86 | * @codeCoverageIgnore |
||
| 87 | */ |
||
| 88 | private function parseProperties() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * {@inheritdoc} |
||
| 236 | * |
||
| 237 | * @param array|null $analysis Configuration items |
||
| 238 | * |
||
| 239 | * @return array The configuration items |
||
| 240 | * |
||
| 241 | * @since 0.2.4 |
||
| 242 | * @codeCoverageIgnore |
||
| 243 | */ |
||
| 244 | private function unescapeProperties($analysis) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * {@inheritdoc} |
||
| 255 | * |
||
| 256 | * @param string $field Field name |
||
| 257 | * @param array|null $analysis Configuration items |
||
| 258 | * |
||
| 259 | * @return array Configuration items after deletion |
||
| 260 | * |
||
| 261 | * @since 0.2.4 |
||
| 262 | * @codeCoverageIgnore |
||
| 263 | */ |
||
| 264 | private function deleteFields($field, $analysis) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * {@inheritdoc} |
||
| 277 | * |
||
| 278 | * @param string|bool|null $file File path |
||
| 279 | * |
||
| 280 | * @return array Configuration items |
||
| 281 | * |
||
| 282 | * @since 0.2.4 |
||
| 283 | * @codeCoverageIgnore |
||
| 284 | */ |
||
| 285 | public function getProperties($file = null) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Loads in the given file and parses it. |
||
| 305 | * |
||
| 306 | * @param string|bool|null $file File to load |
||
| 307 | * |
||
| 308 | * @return array The parsed file data |
||
| 309 | * |
||
| 310 | * @since 0.2.4 |
||
| 311 | * @codeCoverageIgnore |
||
| 312 | */ |
||
| 313 | protected function loadFile($file = null) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Returns the formatted configuration file contents. |
||
| 326 | * |
||
| 327 | * @param array $contents configuration array |
||
| 328 | * |
||
| 329 | * @return string formatted configuration file contents |
||
| 330 | * |
||
| 331 | * @since 0.2.4 |
||
| 332 | * @codeCoverageIgnore |
||
| 333 | */ |
||
| 334 | protected function exportFormat($contents = null) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * __toString. |
||
| 343 | * |
||
| 344 | * @return string |
||
| 345 | * @since 0.1.2 |
||
| 346 | * @codeCoverageIgnore |
||
| 347 | */ |
||
| 348 | public function __toString() |
||
| 352 | } |
||
| 353 | |||
| 355 |