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 |
||
| 29 | class FileResource extends AbstractResource |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * Basic file interaction logic |
||
| 33 | */ |
||
| 34 | use FileTrait; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The env separator for environment replacements |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private static $env_separator = '_ENV::'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The filename of the loaded file |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | private $filename; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The parent ResourceProvider |
||
| 52 | * |
||
| 53 | * @var \M1\Vars\Resource\ResourceProvider |
||
| 54 | */ |
||
| 55 | private $provider; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * The raw content from the passed file |
||
| 59 | * |
||
| 60 | * @var mixed |
||
| 61 | */ |
||
| 62 | private $raw_content = array(); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The file resource constructor to get and parse the content from files |
||
| 66 | * |
||
| 67 | * @param \M1\Vars\Resource\ResourceProvider $provider The parent ResourceProvider |
||
| 68 | * @param string $file The passed file |
||
| 69 | */ |
||
| 70 | 60 | public function __construct(ResourceProvider $provider, $file) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Make the paths used for the filename variable |
||
| 88 | * |
||
| 89 | * @param string $file The passed file |
||
| 90 | */ |
||
| 91 | 60 | private function makePaths($file) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Search for imports in the files and does the replacement variables |
||
| 109 | * |
||
| 110 | * @param mixed $content The file content received from the loader |
||
| 111 | * |
||
| 112 | * @return array Returns the parsed content |
||
| 113 | */ |
||
| 114 | 49 | private function searchForResources($content = array()) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Parses the contents inside the content array |
||
| 127 | * |
||
| 128 | * @param mixed $key The key of the content array |
||
| 129 | * @param mixed $value The value of the key |
||
| 130 | * @param array $returned_content The modified content array to return |
||
| 131 | * |
||
| 132 | * @return array Returns the modified content array |
||
| 133 | */ |
||
| 134 | 49 | private function parseContent($key, $value, $returned_content) |
|
| 151 | /** |
||
| 152 | * Parses the text for option and environment replacements and replaces the text |
||
| 153 | * |
||
| 154 | * @param string $text The text to be parsed |
||
| 155 | * |
||
| 156 | * @return string|null The parsed string |
||
| 157 | */ |
||
| 158 | 47 | private function parseText($text) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Use the import arrays to import resources |
||
| 179 | * |
||
| 180 | * @param mixed $imports The resources wanting to be imported |
||
| 181 | * |
||
| 182 | * @return array The parsed imported resources |
||
| 183 | */ |
||
| 184 | 40 | private function useImports($imports) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Processes the import and gets individual import if set and passes them off to import2Resources() |
||
| 201 | * |
||
| 202 | * @param mixed $import The import to be processed |
||
| 203 | * @param array $imported_resources The array of imported resources |
||
| 204 | * |
||
| 205 | * @return array The parsed imported resources |
||
| 206 | */ |
||
| 207 | 40 | private function processImport($import, array $imported_resources) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Creates the resource from the import then imports it |
||
| 227 | * |
||
| 228 | * @param array|string $import The string|array to be converted to a resource |
||
| 229 | * @param array $imported_resources The array of imported resources |
||
| 230 | * |
||
| 231 | * @return array The imported resources |
||
| 232 | */ |
||
| 233 | 40 | private function import2Resource($import, array $imported_resources) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Creates resource from the import |
||
| 246 | * |
||
| 247 | * @param array|string $import The import to create a resource from |
||
| 248 | * |
||
| 249 | * @return array|\M1\Vars\Resource\ResourceProvider The resource of the import |
||
| 250 | */ |
||
| 251 | 40 | private function createResource($import) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Import resource into the imported resources and merge contents |
||
| 273 | * |
||
| 274 | * @param ResourceProvider $provider The new imported resource |
||
| 275 | * @param array $imported_resources The imported resources |
||
| 276 | * |
||
| 277 | * @return array The modified imported resources |
||
| 278 | */ |
||
| 279 | 39 | private function importResource(ResourceProvider $provider, $imported_resources) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Returns whether the passed array is associative |
||
| 297 | * |
||
| 298 | * @param array $array The passed array |
||
| 299 | * |
||
| 300 | * @return bool Is the passed array associative |
||
| 301 | */ |
||
| 302 | 9 | private function isAssoc(array $array) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Checks if the passed import is wanting to be merged into the parent content or relative content |
||
| 309 | * |
||
| 310 | * @param mixed $import The passed import |
||
| 311 | * |
||
| 312 | * @return bool Returns whether wanting to be a relative import |
||
| 313 | */ |
||
| 314 | 8 | public function isRelative($import) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Returns the filename of the resource |
||
| 337 | * |
||
| 338 | * @return mixed The filename |
||
| 339 | */ |
||
| 340 | 3 | public function getFilename() |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Returns the raw content of the resource |
||
| 347 | * |
||
| 348 | * @return array|mixed The raw content |
||
| 349 | */ |
||
| 350 | 1 | public function getRawContent() |
|
| 354 | } |
||
| 355 |
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.