Complex classes like LanguageLoader 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 LanguageLoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class LanguageLoader |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * The languages array. |
||
| 13 | * |
||
| 14 | * @var array |
||
| 15 | */ |
||
| 16 | protected static $languages; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Get the language by it's ISO ISO 639-1 code. |
||
| 20 | * |
||
| 21 | * @param string $code |
||
| 22 | * @param bool $hydrate |
||
| 23 | * |
||
| 24 | * @throws \Rinvex\Language\LanguageLoaderException |
||
| 25 | * |
||
| 26 | * @return \Rinvex\Language\Language|array |
||
| 27 | */ |
||
| 28 | public static function language($code, $hydrate = true) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Get all languages. |
||
| 45 | * |
||
| 46 | * @param bool $hydrate |
||
| 47 | * |
||
| 48 | * @return array |
||
| 49 | */ |
||
| 50 | public static function languages($hydrate = false) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get all language scripts. |
||
| 63 | * |
||
| 64 | * @return array |
||
| 65 | */ |
||
| 66 | public static function scripts() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Get all language families. |
||
| 77 | * |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | public static function families() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Filter items by the given key value pair. |
||
| 91 | * |
||
| 92 | * @param string $key |
||
| 93 | * @param mixed $operator |
||
| 94 | * @param mixed $value |
||
| 95 | * |
||
| 96 | * @return array |
||
| 97 | */ |
||
| 98 | public static function where($key, $operator, $value = null) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Get an operator checker callback. |
||
| 114 | * |
||
| 115 | * @param string $key |
||
| 116 | * @param string $operator |
||
| 117 | * @param mixed $value |
||
| 118 | * |
||
| 119 | * @return \Closure |
||
| 120 | */ |
||
| 121 | protected static function operatorForWhere($key, $operator, $value) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Run a filter over each of the items. |
||
| 144 | * |
||
| 145 | * @param array $items |
||
| 146 | * @param callable|null $callback |
||
| 147 | * |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | protected static function filter($items, callable $callback = null) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get an item from an array or object using "dot" notation. |
||
| 161 | * |
||
| 162 | * @param mixed $target |
||
| 163 | * @param string|array $key |
||
| 164 | * @param mixed $default |
||
| 165 | * |
||
| 166 | * @return mixed |
||
| 167 | */ |
||
| 168 | protected static function get($target, $key, $default = null) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Pluck an array of values from an array. |
||
| 201 | * |
||
| 202 | * @param array $array |
||
| 203 | * @param string|array $value |
||
| 204 | * @param string|array|null $key |
||
| 205 | * |
||
| 206 | * @return array |
||
| 207 | */ |
||
| 208 | protected static function pluck($array, $value, $key = null) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Collapse an array of arrays into a single array. |
||
| 236 | * |
||
| 237 | * @param array $array |
||
| 238 | * |
||
| 239 | * @return array |
||
| 240 | */ |
||
| 241 | protected static function collapse($array) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Get contents of the given file path. |
||
| 258 | * |
||
| 259 | * @param string $filePath |
||
| 260 | * |
||
| 261 | * @throws \Rinvex\Language\LanguageLoaderException |
||
| 262 | * |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public static function getFile($filePath) |
||
| 273 | } |
||
| 274 |