Complex classes like Locale 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 Locale, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | final class Locale |
||
9 | { |
||
10 | /** |
||
11 | * @var string|null Two-letter ISO-639-2 language code |
||
12 | * @see http://www.loc.gov/standards/iso639-2/ |
||
13 | */ |
||
14 | private $language; |
||
15 | |||
16 | /** |
||
17 | * @var string|null extended language subtags |
||
18 | */ |
||
19 | private $extendedLanguage; |
||
20 | |||
21 | /** |
||
22 | * @var string|null |
||
23 | */ |
||
24 | private $extension; |
||
25 | |||
26 | /** |
||
27 | * @var string|null Four-letter ISO 15924 script code |
||
28 | * @see http://www.unicode.org/iso15924/iso15924-codes.html |
||
29 | */ |
||
30 | private $script; |
||
31 | |||
32 | /** |
||
33 | * @var string|null Two-letter ISO 3166-1 country code |
||
34 | * @see https://www.iso.org/iso-3166-country-codes.html |
||
35 | */ |
||
36 | private $region; |
||
37 | |||
38 | /** |
||
39 | * @var string|null variant of language conventions to use |
||
40 | */ |
||
41 | private $variant; |
||
42 | |||
43 | /** |
||
44 | * @var string|null ICU currency |
||
45 | */ |
||
46 | private $currency; |
||
47 | |||
48 | /** |
||
49 | * @var string|null ICU calendar |
||
50 | */ |
||
51 | private $calendar; |
||
52 | |||
53 | /** |
||
54 | * @var string ICU collation |
||
55 | */ |
||
56 | private $collation; |
||
57 | |||
58 | /** |
||
59 | * @var string|null ICU numbers |
||
60 | */ |
||
61 | private $numbers; |
||
62 | |||
63 | /** |
||
64 | * @var string|null |
||
65 | */ |
||
66 | private $grandfathered; |
||
67 | |||
68 | /** |
||
69 | * @var string|null |
||
70 | */ |
||
71 | private $private; |
||
72 | |||
73 | /** |
||
74 | * Locale constructor. |
||
75 | * @param string $localeString BCP 47 formatted locale string |
||
76 | * @see https://tools.ietf.org/html/bcp47 |
||
77 | * @throws \InvalidArgumentException |
||
78 | */ |
||
79 | public function __construct(string $localeString) |
||
139 | |||
140 | /** |
||
141 | * Returns Locale. |
||
142 | * @param Locale|string $locale |
||
143 | * @return self |
||
144 | * @throws \InvalidArgumentException |
||
145 | */ |
||
146 | public static function create($locale): self |
||
154 | |||
155 | /** |
||
156 | * @return string Four-letter ISO 15924 script code |
||
157 | * @see http://www.unicode.org/iso15924/iso15924-codes.html |
||
158 | */ |
||
159 | public function getScript(): ?string |
||
163 | |||
164 | /** |
||
165 | * @param null|string $script Four-letter ISO 15924 script code |
||
166 | * @see http://www.unicode.org/iso15924/iso15924-codes.html |
||
167 | * @return self |
||
168 | */ |
||
169 | public function withScript(?string $script): self |
||
175 | |||
176 | |||
177 | /** |
||
178 | * @return string variant of language conventions to use |
||
179 | */ |
||
180 | public function getVariant(): ?string |
||
184 | |||
185 | /** |
||
186 | * @param null|string $variant variant of language conventions to use |
||
187 | * @return self |
||
188 | */ |
||
189 | public function withVariant(?string $variant): self |
||
195 | |||
196 | /** |
||
197 | * @return string|null Two-letter ISO-639-2 language code |
||
198 | * @see http://www.loc.gov/standards/iso639-2/ |
||
199 | */ |
||
200 | public function getLanguage(): string |
||
204 | |||
205 | /** |
||
206 | * @param null|string $language Two-letter ISO-639-2 language code |
||
207 | * @see http://www.loc.gov/standards/iso639-2/ |
||
208 | * @return self |
||
209 | */ |
||
210 | public function withLanguage(?string $language): self |
||
216 | |||
217 | /** |
||
218 | * @return null|string ICU calendar |
||
219 | */ |
||
220 | public function getCalendar(): ?string |
||
224 | |||
225 | /** |
||
226 | * @param null|string $calendar ICU calendar |
||
227 | * @return self |
||
228 | */ |
||
229 | public function withCalendar(?string $calendar): self |
||
235 | |||
236 | |||
237 | /** |
||
238 | * @return null|string ICU collation |
||
239 | */ |
||
240 | public function getCollation(): ?string |
||
244 | |||
245 | /** |
||
246 | * @param null|string $collation ICU collation |
||
247 | * @return self |
||
248 | */ |
||
249 | public function withCollation(?string $collation): self |
||
255 | |||
256 | /** |
||
257 | * @return null|string ICU numbers |
||
258 | */ |
||
259 | public function getNumbers(): ?string |
||
263 | |||
264 | /** |
||
265 | * @param null|string $numbers ICU numbers |
||
266 | * @return self |
||
267 | */ |
||
268 | public function withNumbers(?string $numbers): self |
||
274 | |||
275 | /** |
||
276 | * @return string Two-letter ISO 3166-1 country code |
||
277 | * @see https://www.iso.org/iso-3166-country-codes.html |
||
278 | */ |
||
279 | public function getRegion(): ?string |
||
283 | |||
284 | /** |
||
285 | * @param null|string $region Two-letter ISO 3166-1 country code |
||
286 | * @see https://www.iso.org/iso-3166-country-codes.html |
||
287 | * @return self |
||
288 | */ |
||
289 | public function withRegion(?string $region): self |
||
295 | |||
296 | /** |
||
297 | * @return string ICU currency |
||
298 | */ |
||
299 | public function getCurrency(): ?string |
||
303 | |||
304 | /** |
||
305 | * @param null|string $currency ICU currency |
||
306 | * @return self |
||
307 | */ |
||
308 | public function withCurrency(?string $currency): self |
||
315 | |||
316 | /** |
||
317 | * @return null|string extended language subtags |
||
318 | */ |
||
319 | public function getExtendedLanguage(): ?string |
||
323 | |||
324 | /** |
||
325 | * @param null|string $extendedLanguage extended language subtags |
||
326 | * @return self |
||
327 | */ |
||
328 | public function withExtendedLanguage(?string $extendedLanguage): self |
||
335 | |||
336 | |||
337 | /** |
||
338 | * @return null|string |
||
339 | */ |
||
340 | public function getPrivate(): ?string |
||
344 | |||
345 | /** |
||
346 | * @param null|string $private |
||
347 | * @return self |
||
348 | */ |
||
349 | public function withPrivate(?string $private): self |
||
356 | |||
357 | /** |
||
358 | * @return string regular expression for parsing BCP 47 |
||
359 | * @see https://tools.ietf.org/html/bcp47 |
||
360 | */ |
||
361 | private static function getBCP47Regex(): string |
||
378 | |||
379 | public function __toString(): string |
||
383 | |||
384 | /** |
||
385 | * @return string |
||
386 | */ |
||
387 | public function asString(): string |
||
444 | |||
445 | /** |
||
446 | * Returns fallback locale |
||
447 | * |
||
448 | * @return self fallback locale |
||
449 | */ |
||
450 | public function getFallbackLocale(): self |
||
466 | } |
||
467 |
Late static binding only has effect in subclasses. A
final
class cannot be extended anymore so late static binding cannot occurr. Consider replacingstatic::
withself::
.To learn more about late static binding, please refer to the PHP core documentation.