Complex classes like MultiLang 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 MultiLang, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class MultiLang |
||
23 | { |
||
24 | /** |
||
25 | * Language/Locale. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $lang; |
||
30 | |||
31 | /** |
||
32 | * System environment |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $environment; |
||
37 | |||
38 | /** |
||
39 | * Config. |
||
40 | * |
||
41 | * @var \Longman\LaravelMultiLang\Config |
||
42 | */ |
||
43 | protected $config; |
||
44 | |||
45 | /** |
||
46 | * Repository |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $repository; |
||
51 | |||
52 | /** |
||
53 | * Texts. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $texts; |
||
58 | |||
59 | /** |
||
60 | * Missing texts. |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $new_texts; |
||
65 | |||
66 | /** |
||
67 | * Create a new MultiLang instance. |
||
68 | * |
||
69 | * @param string $environment |
||
70 | * @param array $config |
||
71 | * @param \Illuminate\Cache\CacheManager $cache |
||
72 | * @param \Illuminate\Database\DatabaseManager $db |
||
73 | */ |
||
74 | 23 | public function __construct($environment, array $config, Cache $cache, Database $db) |
|
84 | |||
85 | 23 | public function setConfig(array $config) |
|
90 | |||
91 | 23 | public function setRepository(Repository $repository) |
|
96 | |||
97 | 2 | public function getRepository() |
|
101 | |||
102 | /** |
||
103 | * Set locale and load texts |
||
104 | * |
||
105 | * @param string $lang |
||
106 | * @param array $texts |
||
107 | * @return void |
||
108 | */ |
||
109 | 21 | public function setLocale($lang, array $texts = null) |
|
110 | { |
||
111 | 21 | if (!$lang) { |
|
112 | 1 | throw new InvalidArgumentException('Locale is empty'); |
|
113 | } |
||
114 | 20 | $this->lang = $lang; |
|
115 | |||
116 | 20 | if (!is_array($texts)) { |
|
117 | 17 | $texts = $this->loadTexts($this->getLocale()); |
|
118 | 17 | } |
|
119 | |||
120 | 20 | $this->texts = $texts; |
|
121 | 20 | } |
|
122 | |||
123 | /** |
||
124 | * Load texts |
||
125 | * |
||
126 | * @param string $lang |
||
127 | * @return array |
||
128 | */ |
||
129 | 17 | public function loadTexts($lang) |
|
130 | { |
||
131 | 17 | if ($this->environment != 'production' || $this->config->get('cache.enabled', true) === false) { |
|
132 | 15 | $texts = $this->repository->loadFromDatabase($lang); |
|
133 | 15 | return $texts; |
|
134 | } |
||
135 | |||
136 | 3 | if ($this->repository->existsInCache($lang)) { |
|
137 | $texts = $this->repository->loadFromCache($lang); |
||
138 | } else { |
||
139 | 3 | $texts = $this->repository->loadFromDatabase($lang); |
|
140 | 3 | $this->repository->storeInCache($lang, $texts); |
|
141 | } |
||
142 | |||
143 | 3 | return $texts; |
|
144 | } |
||
145 | |||
146 | /** |
||
147 | * Get translated text |
||
148 | * |
||
149 | * @param string $key |
||
150 | * @return string |
||
151 | */ |
||
152 | 8 | public function get($key) |
|
172 | |||
173 | /** |
||
174 | * Get texts |
||
175 | * |
||
176 | * @return array |
||
177 | */ |
||
178 | 4 | public function getRedirectUrl(Request $request) |
|
179 | { |
||
180 | 4 | $locale = $request->segment(1); |
|
181 | 4 | $fallback_locale = $this->config->get('default_locale', 'en'); |
|
182 | 4 | $exclude_segments = $this->config->get('exclude_segments', []); |
|
183 | 4 | if (in_array($locale, $exclude_segments)) { |
|
184 | return null; |
||
185 | } |
||
186 | |||
187 | 4 | if (strlen($locale) == 2) { |
|
188 | 3 | $locales = $this->config->get('locales', []); |
|
189 | |||
190 | 3 | if (!isset($locales[$locale])) { |
|
191 | 2 | $segments = $request->segments(); |
|
192 | 2 | $segments[0] = $fallback_locale; |
|
193 | 2 | $url = implode('/', $segments); |
|
194 | 2 | if ($query_string = $request->server->get('QUERY_STRING')) { |
|
195 | 1 | $url .= '?' . $query_string; |
|
196 | 1 | } |
|
197 | |||
198 | 2 | return $url; |
|
199 | } |
||
200 | 1 | } else { |
|
201 | 1 | $segments = $request->segments(); |
|
202 | 1 | $url = $fallback_locale . '/' . implode('/', $segments); |
|
203 | 1 | if ($query_string = $request->server->get('QUERY_STRING')) { |
|
204 | $url .= '?' . $query_string; |
||
205 | } |
||
206 | 1 | return $url; |
|
207 | } |
||
208 | |||
209 | 1 | return null; |
|
210 | } |
||
211 | |||
212 | 1 | public function detectLocale(Request $request) |
|
223 | |||
224 | public function routeGroup(Closure $callback) |
||
225 | { |
||
226 | $router = app('router'); |
||
227 | |||
228 | $locales = $this->config->get('locales', []); |
||
229 | |||
230 | foreach ($locales as $locale => $val) { |
||
231 | $router->group([ |
||
232 | 'prefix' => $locale, |
||
233 | 'as' => $locale.'.', |
||
234 | ], $callback); |
||
235 | } |
||
236 | |||
237 | } |
||
238 | |||
239 | public function manageTextsRoutes() |
||
240 | { |
||
241 | $router = app('router'); |
||
242 | $route = $this->config->get('text-route.route', 'texts'); |
||
243 | $controller = $this->config->get('text-route.controller', '\Longman\LaravelMultiLang\Controllers\TextsController'); |
||
244 | |||
245 | $router->get( |
||
246 | $route, |
||
247 | ['uses' => $controller . '@index'] |
||
248 | ); |
||
249 | $router->post( |
||
250 | $route, |
||
251 | ['uses' => $controller . '@save'] |
||
252 | ); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Get texts |
||
257 | * |
||
258 | * @return array |
||
259 | */ |
||
260 | 4 | public function getTexts() |
|
265 | |||
266 | /** |
||
267 | * Set texts manually |
||
268 | * |
||
269 | * @param array $texts_array |
||
270 | * @return \Longman\LaravelMultiLang\MultiLang |
||
271 | */ |
||
272 | 3 | public function setTexts(array $texts_array) |
|
273 | { |
||
274 | 3 | $texts = []; |
|
275 | 3 | foreach ($texts_array as $key => $value) { |
|
276 | 3 | $texts[$key] = $value; |
|
277 | 3 | } |
|
278 | |||
279 | 3 | $this->texts = $texts; |
|
280 | |||
281 | 3 | return $this; |
|
282 | } |
||
283 | |||
284 | /** |
||
285 | * Queue missing texts |
||
286 | * |
||
287 | * @param string $key |
||
288 | * @return void |
||
289 | */ |
||
290 | 4 | protected function queueToSave($key) |
|
294 | |||
295 | 2 | public function getUrl($path) |
|
296 | { |
||
297 | 2 | $locale = $this->getLocale(); |
|
298 | 2 | if ($locale) { |
|
299 | 2 | $path = $locale . '/' . $path; |
|
300 | 2 | } |
|
301 | 2 | return $path; |
|
302 | } |
||
303 | |||
304 | 5 | public function autoSaveIsAllowed() |
|
311 | |||
312 | 18 | public function getLocale() |
|
316 | |||
317 | 1 | public function getLocales() |
|
321 | |||
322 | 3 | public function saveTexts() |
|
331 | } |
||
332 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: