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) |
|
| 284 | { |
||
| 285 | 49 | $resource = $this->explodeResourceIfElse($resource); |
|
| 286 | 49 | $resource_pieces = array(); |
|
| 287 | |||
| 288 | 49 | foreach ($resource as $r) { |
|
| 289 | 49 | $parsed_r = $this->trimFlags($r); |
|
| 290 | 49 | $parsed_r = sprintf('%s/%s', dirname($this->file), $parsed_r); |
|
| 291 | 49 | $parsed_r = $this->replicateFlags($parsed_r, $r); |
|
| 292 | |||
| 293 | 49 | $resource_pieces[] = $parsed_r; |
|
| 294 | 49 | } |
|
| 295 | |||
| 296 | 49 | return $this->implodeResourceIfElse($resource_pieces); |
|
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Import resource into the imported resources and merge contents |
||
| 301 | * |
||
| 302 | * @param ResourceProvider $provider The new imported resource |
||
| 303 | * @param array $imported_resources The imported resources |
||
| 304 | * |
||
| 305 | * @return array The modified imported resources |
||
| 306 | */ |
||
| 307 | 47 | private function importResource(ResourceProvider $provider, $imported_resources) |
|
| 308 | { |
||
| 309 | 47 | $content = $provider->getContent(); |
|
| 310 | 47 | $parent_content = $provider->getParentContent(); |
|
| 311 | |||
| 312 | 47 | if (!empty($content)) { |
|
| 313 | 44 | $imported_resources = array_replace_recursive($imported_resources, $content); |
|
| 314 | 44 | } |
|
| 315 | |||
| 316 | 47 | if (!empty($parent_content)) { |
|
| 317 | 7 | $this->provider->addParentContent($parent_content); |
|
| 318 | 7 | } |
|
| 319 | |||
| 320 | 47 | return $imported_resources; |
|
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Returns whether the passed array is associative |
||
| 325 | * |
||
| 326 | * @param array $array The passed array |
||
| 327 | * |
||
| 328 | * @return bool Is the passed array associative |
||
| 329 | */ |
||
| 330 | 11 | private function isAssoc(array $array) |
|
| 331 | { |
||
| 332 | 11 | return array_keys($array) !== range(0, count($array) - 1); |
|
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Checks if the passed boolean value is true or false |
||
| 337 | * |
||
| 338 | * @param string $value The value to check |
||
| 339 | * @param mixed $import The passed import |
||
| 340 | * |
||
| 341 | * @return bool Returns the value of the boolean |
||
| 342 | */ |
||
| 343 | 10 | public function checkBooleanValue($value, $import) |
|
| 344 | { |
||
| 345 | 10 | $default = false; |
|
| 346 | |||
| 347 | 10 | if ($value === 'relative') { |
|
| 348 | 10 | $default = true; |
|
| 349 | 10 | } |
|
| 350 | |||
| 351 | 10 | $value = (isset($import[$value])) ? $import[$value] : $default; |
|
| 352 | |||
| 353 | 10 | return $this->getBooleanValue($value); |
|
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Gets the boolean value from the string |
||
| 358 | * |
||
| 359 | * @param string $value The value to check |
||
| 360 | * |
||
| 361 | * @return bool Returns the value of the boolean |
||
| 362 | */ |
||
| 363 | 10 | private function getBooleanValue($value) |
|
| 364 | { |
||
| 365 | 10 | $value = strtolower($value); |
|
| 366 | |||
| 367 | 10 | if (!$value || $value === "false" || $value === "no") { |
|
| 368 | 10 | return false; |
|
| 369 | } |
||
| 370 | |||
| 371 | 9 | return true; |
|
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Returns the filename of the resource |
||
| 376 | * |
||
| 377 | * @return mixed The filename |
||
| 378 | */ |
||
| 379 | 3 | public function getFilename() |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Returns the raw content of the resource |
||
| 386 | * |
||
| 387 | * @return array|mixed The raw content |
||
| 388 | */ |
||
| 389 | 1 | public function getRawContent() |
|
| 393 | } |
||
| 394 |
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.