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 |
||
| 28 | class FileResource extends AbstractResource |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * Basic file interaction logic |
||
| 32 | */ |
||
| 33 | use FileTrait; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The env separator for environment replacements |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | private static $env_separator = '_ENV::'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The filename of the loaded file |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $filename; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The parent ResourceProvider |
||
| 51 | * |
||
| 52 | * @var \M1\Vars\Resource\ResourceProvider |
||
| 53 | */ |
||
| 54 | private $provider; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The raw content from the passed file |
||
| 58 | * |
||
| 59 | * @var mixed |
||
| 60 | */ |
||
| 61 | private $raw_content = array(); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The file resource constructor to get and parse the content from files |
||
| 65 | * |
||
| 66 | * @param \M1\Vars\Resource\ResourceProvider $provider The parent ResourceProvider |
||
| 67 | * @param string $file The passed file |
||
| 68 | */ |
||
| 69 | 56 | public function __construct(ResourceProvider $provider, $file) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Make the paths used for the filename variable |
||
| 87 | * |
||
| 88 | * @param string $file The passed file |
||
| 89 | */ |
||
| 90 | 56 | private function makePaths($file) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Search for imports in the files and does the replacement variables |
||
| 108 | * |
||
| 109 | * @param mixed $content The file content received from the loader |
||
| 110 | * |
||
| 111 | * @return array Returns the parsed content |
||
| 112 | */ |
||
| 113 | 46 | private function searchForResources($content = array()) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Parses the text for option and environment replacements and replaces the text |
||
| 137 | * |
||
| 138 | * @param string $text The text to be parsed |
||
| 139 | * |
||
| 140 | * @return string|null The parsed string |
||
| 141 | */ |
||
| 142 | 44 | private function parseText($text) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Use the import arrays to import resources |
||
| 163 | * |
||
| 164 | * @param mixed $imports The resources wanting to be imported |
||
| 165 | * |
||
| 166 | * @return array The parsed imported resources |
||
| 167 | */ |
||
| 168 | 38 | private function useImports($imports) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Processes the imports and gets individual imports and passes them off to import2Resources() |
||
| 185 | * |
||
| 186 | * @param mixed $imports The imports to be processed |
||
| 187 | * @param array $imported_resources The array of imported resourcesg |
||
| 188 | * |
||
| 189 | * @return array The parsed imported resources |
||
| 190 | */ |
||
| 191 | 38 | private function processImport($import, array $imported_resources) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Creates the resource from the import then imports it |
||
| 211 | * |
||
| 212 | * @param array|string $import The string|array to be converted to a resource |
||
| 213 | * @param array $imported_resources The array of imported resources |
||
| 214 | * |
||
| 215 | * @return array The imported resources |
||
| 216 | */ |
||
| 217 | 38 | private function import2Resource($import, array $imported_resources) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Creates resource from the import |
||
| 230 | * |
||
| 231 | * @param array|string $import The import to create a resource from |
||
| 232 | * |
||
| 233 | * @return array|\M1\Vars\Resource\ResourceProvider The resource of the import |
||
| 234 | */ |
||
| 235 | 38 | private function createResource($import) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Import resource into the imported resources and merge contents |
||
| 257 | * |
||
| 258 | * @param ResourceProvider $provider The new imported resource |
||
| 259 | * @param array $imported_resources The imported resources |
||
| 260 | * |
||
| 261 | * @return array The modified imported resources |
||
| 262 | */ |
||
| 263 | 37 | private function importResource(ResourceProvider $provider, $imported_resources) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Returns whether the passed array is associative |
||
| 281 | * |
||
| 282 | * @param array $array The passed array |
||
| 283 | * |
||
| 284 | * @return bool Is the passed array associative |
||
| 285 | */ |
||
| 286 | 9 | private function isAssoc(array $array) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Checks if the passed import is wanting to be merged into the parent content or relative content |
||
| 293 | * |
||
| 294 | * @param mixed $import The passed import |
||
| 295 | * |
||
| 296 | * @return bool Returns whether wanting to be a relative import |
||
| 297 | */ |
||
| 298 | 8 | public function isRelative($import) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Returns the filename of the resource |
||
| 322 | * |
||
| 323 | * @return mixed The filename |
||
| 324 | */ |
||
| 325 | 3 | public function getFilename() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Returns the raw content of the resource |
||
| 332 | * |
||
| 333 | * @return array|mixed The raw content |
||
| 334 | */ |
||
| 335 | 1 | public function getRawContent() |
|
| 339 | } |
||
| 340 |
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.