Complex classes like CountryLoader 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 CountryLoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class CountryLoader |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * The countries array. |
||
| 13 | * |
||
| 14 | * @var array |
||
| 15 | */ |
||
| 16 | protected static $countries; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Get the country by it's ISO 3166-1 alpha-2. |
||
| 20 | * |
||
| 21 | * @param string $code |
||
| 22 | * @param bool $hydrate |
||
| 23 | * |
||
| 24 | * @throws \Rinvex\Country\CountryLoaderException |
||
| 25 | * |
||
| 26 | * @return \Rinvex\Country\Country|array |
||
| 27 | */ |
||
| 28 | public static function country($code, $hydrate = true) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Get all countries short-listed. |
||
| 41 | * |
||
| 42 | * @param bool $longlist |
||
| 43 | * @param bool $hydrate |
||
| 44 | * |
||
| 45 | * @throws \Rinvex\Country\CountryLoaderException |
||
| 46 | * |
||
| 47 | * @return array |
||
| 48 | */ |
||
| 49 | public static function countries($longlist = false, $hydrate = false) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Filter items by the given key value pair. |
||
| 64 | * |
||
| 65 | * @param string $key |
||
| 66 | * @param mixed $operator |
||
| 67 | * @param mixed $value |
||
| 68 | * |
||
| 69 | * @throws \Rinvex\Country\CountryLoaderException |
||
| 70 | * |
||
| 71 | * @return array |
||
| 72 | */ |
||
| 73 | public static function where($key, $operator, $value = null) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Get an operator checker callback. |
||
| 89 | * |
||
| 90 | * @param string $key |
||
| 91 | * @param string $operator |
||
| 92 | * @param mixed $value |
||
| 93 | * |
||
| 94 | * @return \Closure |
||
| 95 | */ |
||
| 96 | protected static function operatorForWhere($key, $operator, $value) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Run a filter over each of the items. |
||
| 119 | * |
||
| 120 | * @param array $items |
||
| 121 | * @param callable|null $callback |
||
| 122 | * |
||
| 123 | * @return array |
||
| 124 | */ |
||
| 125 | protected static function filter($items, callable $callback = null) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get an item from an array or object using "dot" notation. |
||
| 136 | * |
||
| 137 | * @param mixed $target |
||
| 138 | * @param string|array $key |
||
| 139 | * @param mixed $default |
||
| 140 | * |
||
| 141 | * @return mixed |
||
| 142 | */ |
||
| 143 | protected static function get($target, $key, $default = null) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Pluck an array of values from an array. |
||
| 176 | * |
||
| 177 | * @param array $array |
||
| 178 | * @param string|array $value |
||
| 179 | * @param string|array|null $key |
||
| 180 | * |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | protected static function pluck($array, $value, $key = null) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Collapse an array of arrays into a single array. |
||
| 211 | * |
||
| 212 | * @param array $array |
||
| 213 | * |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | protected static function collapse($array) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Get contents of the given file path. |
||
| 233 | * |
||
| 234 | * @param string $filePath |
||
| 235 | * |
||
| 236 | * @throws \Rinvex\Country\CountryLoaderException |
||
| 237 | * |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | protected static function getFile($filePath) |
||
| 248 | } |
||
| 249 |