| Total Complexity | 41 |
| Total Lines | 208 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Importer 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.
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 Importer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class Importer |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private static $recipeFilename; |
||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | private static $recipeSource; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param string|string[] $paths |
||
| 47 | */ |
||
| 48 | public static function import($paths) |
||
| 49 | { |
||
| 50 | if (!is_array($paths)) { |
||
| 51 | $paths = [$paths]; |
||
| 52 | } |
||
| 53 | foreach ($paths as $path) { |
||
| 54 | if (preg_match('/\.php$/i', $path)) { |
||
| 55 | // Prevent variable leak into deploy.php file |
||
| 56 | call_user_func(function () use ($path) { |
||
| 57 | // Reorder autoload stack |
||
| 58 | $originStack = spl_autoload_functions(); |
||
| 59 | |||
| 60 | require $path; |
||
| 61 | |||
| 62 | $newStack = spl_autoload_functions(); |
||
| 63 | if ($originStack[0] !== $newStack[0]) { |
||
| 64 | foreach (array_reverse($originStack) as $loader) { |
||
| 65 | spl_autoload_unregister($loader); |
||
| 66 | spl_autoload_register($loader, true, true); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | }); |
||
| 70 | } elseif (preg_match('/\.ya?ml$/i', $path)) { |
||
| 71 | self::$recipeFilename = basename($path); |
||
| 72 | self::$recipeSource = file_get_contents($path, true); |
||
| 73 | |||
| 74 | $root = array_filter(Yaml::parse(self::$recipeSource), static function (string $key) { |
||
| 75 | return !str_starts_with($key, '.'); |
||
| 76 | }, ARRAY_FILTER_USE_KEY); |
||
| 77 | |||
| 78 | foreach (array_keys($root) as $key) { |
||
| 79 | static::$key($root[$key]); |
||
| 80 | } |
||
| 81 | } else { |
||
| 82 | throw new Exception("Unknown file format: $path\nOnly .php and .yaml supported."); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | protected static function hosts(array $hosts) |
||
| 88 | { |
||
| 89 | foreach ($hosts as $alias => $config) { |
||
| 90 | if ($config['local'] ?? false) { |
||
| 91 | $host = localhost($alias); |
||
| 92 | } else { |
||
| 93 | $host = host($alias); |
||
| 94 | } |
||
| 95 | if (is_array($config)) { |
||
| 96 | foreach ($config as $key => $value) { |
||
| 97 | $host->set($key, $value); |
||
|
|
|||
| 98 | } |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | protected static function config(array $config) |
||
| 104 | { |
||
| 105 | foreach ($config as $key => $value) { |
||
| 106 | set($key, $value); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | protected static function tasks(array $tasks) |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | protected static function after(array $after) |
||
| 221 | { |
||
| 222 | foreach ($after as $key => $value) { |
||
| 223 | if (is_array($value)) { |
||
| 224 | foreach (array_reverse($value) as $v) { |
||
| 225 | after($key, $v); |
||
| 226 | } |
||
| 227 | } else { |
||
| 228 | after($key, $value); |
||
| 229 | } |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | protected static function before(array $before) |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | } |
||
| 246 |