Complex classes like FileResource 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 FileResource, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class FileResource extends AbstractResource |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * Basic file interaction logic |
||
| 34 | */ |
||
| 35 | use FileTrait; |
||
| 36 | |||
| 37 | use ResourceFlagsTrait; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The env separator for environment replacements |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private static $env_separator = '_ENV::'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The filename of the loaded file |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | private $filename; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The parent ResourceProvider |
||
| 55 | * |
||
| 56 | * @var \M1\Vars\Resource\ResourceProvider |
||
| 57 | */ |
||
| 58 | private $provider; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The raw content from the passed file |
||
| 62 | * |
||
| 63 | * @var mixed |
||
| 64 | */ |
||
| 65 | private $raw_content = array(); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The file resource constructor to get and parse the content from files |
||
| 69 | * |
||
| 70 | * @param \M1\Vars\Resource\ResourceProvider $provider The parent ResourceProvider |
||
| 71 | * @param string $file The passed file |
||
| 72 | */ |
||
| 73 | 69 | public function __construct(ResourceProvider $provider, $file) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Make the paths used for the filename variable |
||
| 91 | * |
||
| 92 | * @param string $file The passed file |
||
| 93 | */ |
||
| 94 | 69 | private function makePaths($file) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Search for imports in the files and does the replacement variables |
||
| 112 | * |
||
| 113 | * @param mixed $content The file content received from the loader |
||
| 114 | * |
||
| 115 | * @return array Returns the parsed content |
||
| 116 | */ |
||
| 117 | 58 | private function searchForResources($content = array()) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Parses the contents inside the content array |
||
| 130 | * |
||
| 131 | * @param mixed $key The key of the content array |
||
| 132 | * @param mixed $value The value of the key |
||
| 133 | * @param array $returned_content The modified content array to return |
||
| 134 | * |
||
| 135 | * @return array Returns the modified content array |
||
| 136 | */ |
||
| 137 | 58 | private function parseContent($key, $value, $returned_content) |
|
| 154 | /** |
||
| 155 | * Parses the text for option and environment replacements and replaces the text |
||
| 156 | * |
||
| 157 | * @param string $text The text to be parsed |
||
| 158 | * |
||
| 159 | * @return string|null The parsed string |
||
| 160 | */ |
||
| 161 | 53 | private function parseText($text) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Use the import arrays to import resources |
||
| 182 | * |
||
| 183 | * @param mixed $imports The resources wanting to be imported |
||
| 184 | * |
||
| 185 | * @return array The parsed imported resources |
||
| 186 | */ |
||
| 187 | 49 | private function useImports($imports) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Processes the import and gets individual import if set and passes them off to import2Resources() |
||
| 204 | * |
||
| 205 | * @param mixed $import The import to be processed |
||
| 206 | * @param array $imported_resources The array of imported resources |
||
| 207 | * |
||
| 208 | * @return array The parsed imported resources |
||
| 209 | */ |
||
| 210 | 49 | private function processImport($import, array $imported_resources) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Creates the resource from the import then imports it |
||
| 231 | * |
||
| 232 | * @param array|string $import The string|array to be converted to a resource |
||
| 233 | * @param array $imported_resources The array of imported resources |
||
| 234 | * |
||
| 235 | * @return array The imported resources |
||
| 236 | */ |
||
| 237 | 49 | private function import2Resource($import, array $imported_resources) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Creates resource from the import |
||
| 250 | * |
||
| 251 | * @param array|string $import The import to create a resource from |
||
| 252 | * |
||
| 253 | * @return array|\M1\Vars\Resource\ResourceProvider The resource of the import |
||
| 254 | */ |
||
| 255 | 49 | private function createResource($import) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Creates the correctly formatted resource name with paths |
||
| 278 | * |
||
| 279 | * @param string $resource The resource to create the import name for |
||
| 280 | * |
||
| 281 | * @return string The parsed resource |
||
| 282 | */ |
||
| 283 | 49 | private function createImportName($resource) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Import resource into the imported resources and merge contents |
||
| 317 | * |
||
| 318 | * @param ResourceProvider $provider The new imported resource |
||
| 319 | * @param array $imported_resources The imported resources |
||
| 320 | * |
||
| 321 | * @return array The modified imported resources |
||
| 322 | */ |
||
| 323 | 47 | private function importResource(ResourceProvider $provider, $imported_resources) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Returns whether the passed array is associative |
||
| 341 | * |
||
| 342 | * @param array $array The passed array |
||
| 343 | * |
||
| 344 | * @return bool Is the passed array associative |
||
| 345 | */ |
||
| 346 | 11 | private function isAssoc(array $array) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Checks if the passed boolean value is true or false |
||
| 353 | * |
||
| 354 | * @param string $value The value to check |
||
| 355 | * @param mixed $import The passed import |
||
| 356 | * |
||
| 357 | * @return bool Returns the value of the boolean |
||
| 358 | */ |
||
| 359 | 10 | public function checkBooleanValue($value, $import) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Gets the boolean value from the string |
||
| 374 | * |
||
| 375 | * @param string $value The value to check |
||
| 376 | * |
||
| 377 | * @return bool Returns the value of the boolean |
||
| 378 | */ |
||
| 379 | 10 | private function getBooleanValue($value) |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Returns the filename of the resource |
||
| 392 | * |
||
| 393 | * @return mixed The filename |
||
| 394 | */ |
||
| 395 | 3 | public function getFilename() |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Returns the raw content of the resource |
||
| 402 | * |
||
| 403 | * @return array|mixed The raw content |
||
| 404 | */ |
||
| 405 | 1 | public function getRawContent() |
|
| 409 | } |
||
| 410 |
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.