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 | /** |
||
| 38 | * Enables the resource flags logic |
||
| 39 | */ |
||
| 40 | use ResourceFlagsTrait; |
||
| 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 VariableProvider |
||
| 65 | * |
||
| 66 | * @var \M1\Vars\Variables\VariableProvider |
||
| 67 | */ |
||
| 68 | private $variables; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The file resource constructor to get and parse the content from files |
||
| 72 | * |
||
| 73 | * @param \M1\Vars\Resource\ResourceProvider $provider The parent ResourceProvider |
||
| 74 | * @param string $file The passed file |
||
| 75 | */ |
||
| 76 | 75 | public function __construct(ResourceProvider $provider, $file) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Make the paths used for the filename variable |
||
| 97 | * |
||
| 98 | * @param string $file The passed file |
||
| 99 | */ |
||
| 100 | 75 | private function makePaths($file) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Search for imports in the files and does the replacement variables |
||
| 118 | * |
||
| 119 | * @param mixed $content The file content received from the loader |
||
| 120 | * @param string $prefix The array prefix for the entity |
||
| 121 | * |
||
| 122 | * @return array Returns the parsed content |
||
| 123 | */ |
||
| 124 | 63 | private function searchForResources($content = array(), $prefix = '') |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Parses the contents inside the content array |
||
| 138 | * |
||
| 139 | * @param mixed $key The key of the content array |
||
| 140 | * @param mixed $value The value of the key |
||
| 141 | * @param array $returned_content The modified content array to return |
||
| 142 | * @param string $prefix The array prefix for the entity |
||
| 143 | * |
||
| 144 | * @return array Returns the modified content array |
||
| 145 | */ |
||
| 146 | 63 | private function parseContent($key, $value, $returned_content, $prefix) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Parses the text for option and environment replacements and replaces the text |
||
| 170 | * |
||
| 171 | * @param string $text The text to be parsed |
||
| 172 | * |
||
| 173 | * @return string|null The parsed string |
||
| 174 | */ |
||
| 175 | 63 | private function parseText($text) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Use the import arrays to import resources |
||
| 186 | * |
||
| 187 | * @param mixed $imports The resources wanting to be imported |
||
| 188 | * |
||
| 189 | * @return array The parsed imported resources |
||
| 190 | */ |
||
| 191 | 53 | private function useImports($imports) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Processes the import and gets individual import if set and passes them off to import2Resources() |
||
| 208 | * |
||
| 209 | * @param mixed $import The import to be processed |
||
| 210 | * @param array $imported_resources The array of imported resources |
||
| 211 | * |
||
| 212 | * @return array The parsed imported resources |
||
| 213 | */ |
||
| 214 | 53 | private function processImport($import, array $imported_resources) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Creates the resource from the import then imports it |
||
| 235 | * |
||
| 236 | * @param array|string $import The string|array to be converted to a resource |
||
| 237 | * @param array $imported_resources The array of imported resources |
||
| 238 | * |
||
| 239 | * @return array The imported resources |
||
| 240 | */ |
||
| 241 | 53 | private function import2Resource($import, array $imported_resources) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Creates resource from the import |
||
| 254 | * |
||
| 255 | * @param array|string $import The import to create a resource from |
||
| 256 | * |
||
| 257 | * @return \M1\Vars\Resource\ResourceProvider The resource of the import |
||
| 258 | */ |
||
| 259 | 53 | private function createResource($import) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Creates the correctly formatted resource name with paths |
||
| 281 | * |
||
| 282 | * @param string $resource The resource to create the import name for |
||
| 283 | * |
||
| 284 | * @return string The parsed resource |
||
| 285 | */ |
||
| 286 | 53 | private function createImportName($resource) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Import resource into the imported resources and merge contents |
||
| 304 | * |
||
| 305 | * @param ResourceProvider $provider The new imported resource |
||
| 306 | * @param array $imported_resources The imported resources |
||
| 307 | * |
||
| 308 | * @return array The modified imported resources |
||
| 309 | */ |
||
| 310 | 51 | private function importResource(ResourceProvider $provider, $imported_resources) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Returns whether the passed array is associative |
||
| 328 | * |
||
| 329 | * @param array $array The passed array |
||
| 330 | * |
||
| 331 | * @return bool Is the passed array associative |
||
| 332 | */ |
||
| 333 | 11 | private function isAssoc(array $array) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Checks if the passed boolean value is true or false |
||
| 340 | * |
||
| 341 | * @param string $value The value to check |
||
| 342 | * @param mixed $import The passed import |
||
| 343 | * |
||
| 344 | * @return bool Returns the value of the boolean |
||
| 345 | */ |
||
| 346 | 10 | public function checkBooleanValue($value, $import) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Gets the boolean value from the string |
||
| 361 | * |
||
| 362 | * @param string $value The value to check |
||
| 363 | * |
||
| 364 | * @return bool Returns the value of the boolean |
||
| 365 | */ |
||
| 366 | 10 | private function getBooleanValue($value) |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Returns the filename of the resource |
||
| 379 | * |
||
| 380 | * @return string The filename |
||
| 381 | */ |
||
| 382 | 3 | public function getFilename() |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Returns the raw content of the resource |
||
| 389 | * |
||
| 390 | * @return array|mixed The raw content |
||
| 391 | */ |
||
| 392 | 1 | public function getRawContent() |
|
| 396 | } |
||
| 397 |
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.