Complex classes like Loader 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 Loader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Loader |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * The countries array. |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected static $countries; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Get the country by it's ISO 3166-1 alpha-2. |
||
| 31 | * |
||
| 32 | * @param string $code |
||
| 33 | * @param bool $hydrate |
||
| 34 | * |
||
| 35 | * @return \Rinvex\Country\Country|array |
||
| 36 | */ |
||
| 37 | public static function country($code, $hydrate = true) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Get all countries short-listed. |
||
| 48 | * |
||
| 49 | * @param bool $longlist |
||
| 50 | * @param bool $hydrate |
||
| 51 | * |
||
| 52 | * @return array |
||
| 53 | */ |
||
| 54 | public static function countries($longlist = false, $hydrate = false) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Filter items by the given key value pair. |
||
| 69 | * |
||
| 70 | * @param string $key |
||
| 71 | * @param mixed $operator |
||
| 72 | * @param mixed $value |
||
| 73 | * |
||
| 74 | * @return array |
||
| 75 | */ |
||
| 76 | public static function where($key, $operator, $value = null) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Get an operator checker callback. |
||
| 92 | * |
||
| 93 | * @param string $key |
||
| 94 | * @param string $operator |
||
| 95 | * @param mixed $value |
||
| 96 | * |
||
| 97 | * @return \Closure |
||
| 98 | */ |
||
| 99 | public static function operatorForWhere($key, $operator, $value) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Run a filter over each of the items. |
||
| 122 | * |
||
| 123 | * @param array $items |
||
| 124 | * @param callable|null $callback |
||
| 125 | * |
||
| 126 | * @return array |
||
| 127 | */ |
||
| 128 | public static function filter($items, callable $callback = null) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Get an item from an array or object using "dot" notation. |
||
| 139 | * |
||
| 140 | * @param mixed $target |
||
| 141 | * @param string|array $key |
||
| 142 | * @param mixed $default |
||
| 143 | * |
||
| 144 | * @return mixed |
||
| 145 | */ |
||
| 146 | public static function get($target, $key, $default = null) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Pluck an array of values from an array. |
||
| 179 | * |
||
| 180 | * @param array $array |
||
| 181 | * @param string|array $value |
||
| 182 | * @param string|array|null $key |
||
| 183 | * |
||
| 184 | * @return array |
||
| 185 | */ |
||
| 186 | public static function pluck($array, $value, $key = null) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Collapse an array of arrays into a single array. |
||
| 214 | * |
||
| 215 | * @param array $array |
||
| 216 | * |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | public static function collapse($array) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Get contents of the given file path. |
||
| 236 | * |
||
| 237 | * @param string $filePath |
||
| 238 | * |
||
| 239 | * @throws \Rinvex\Country\Exceptions\CountryLoaderException |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public static function getFile($filePath) |
||
| 251 | } |
||
| 252 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.