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 |
||
22 | class CountryLoader |
||
23 | { |
||
24 | /** |
||
25 | * The countries array. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected static $countries; |
||
30 | |||
31 | /** |
||
32 | * Get the country by it's ISO 3166-1 alpha-2. |
||
33 | * |
||
34 | * @param string $key |
||
35 | * @param bool $hydrate |
||
36 | * |
||
37 | * @return \Rinvex\Country\Country|array |
||
38 | */ |
||
39 | public static function country($key, $hydrate = true) |
||
49 | |||
50 | /** |
||
51 | * Get all countries short-listed. |
||
52 | * |
||
53 | * @param bool $longlist |
||
54 | * @param bool $hydrate |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | public static function countries($longlist = false, $hydrate = false) |
||
70 | |||
71 | /** |
||
72 | * Filter items by the given key value pair. |
||
73 | * |
||
74 | * @param string $key |
||
75 | * @param mixed $operator |
||
76 | * @param mixed $value |
||
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | public static function where($key, $operator, $value = null) |
||
93 | |||
94 | /** |
||
95 | * Get an operator checker callback. |
||
96 | * |
||
97 | * @param string $key |
||
98 | * @param string $operator |
||
99 | * @param mixed $value |
||
100 | * |
||
101 | * @return \Closure |
||
102 | */ |
||
103 | protected static function operatorForWhere($key, $operator, $value) |
||
123 | |||
124 | /** |
||
125 | * Run a filter over each of the items. |
||
126 | * |
||
127 | * @param array $items |
||
128 | * @param callable|null $callback |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | protected static function filter($items, callable $callback = null) |
||
140 | |||
141 | /** |
||
142 | * Get an item from an array or object using "dot" notation. |
||
143 | * |
||
144 | * @param mixed $target |
||
145 | * @param string|array $key |
||
146 | * @param mixed $default |
||
147 | * |
||
148 | * @return mixed |
||
149 | */ |
||
150 | protected static function get($target, $key, $default = null) |
||
180 | |||
181 | /** |
||
182 | * Pluck an array of values from an array. |
||
183 | * |
||
184 | * @param array $array |
||
185 | * @param string|array $value |
||
186 | * @param string|array|null $key |
||
187 | * |
||
188 | * @return array |
||
189 | */ |
||
190 | protected static function pluck($array, $value, $key = null) |
||
215 | |||
216 | /** |
||
217 | * Collapse an array of arrays into a single array. |
||
218 | * |
||
219 | * @param array $array |
||
220 | * |
||
221 | * @return array |
||
222 | */ |
||
223 | protected static function collapse($array) |
||
237 | |||
238 | /** |
||
239 | * Get contents of the given file path. |
||
240 | * |
||
241 | * @param string $filePath |
||
242 | * |
||
243 | * @throws \Rinvex\Country\CountryLoaderException |
||
244 | * |
||
245 | * @return string |
||
246 | */ |
||
247 | protected static function getFile($filePath) |
||
255 | } |
||
256 |
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.