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 | * @return string Four-letter ISO 15924 script code |
||
| 142 | * @see http://www.unicode.org/iso15924/iso15924-codes.html |
||
| 143 | */ |
||
| 144 | public function script(): ?string |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param null|string $script Four-letter ISO 15924 script code |
||
| 151 | * @see http://www.unicode.org/iso15924/iso15924-codes.html |
||
| 152 | * @return self |
||
| 153 | */ |
||
| 154 | public function withScript(?string $script): self |
||
| 160 | |||
| 161 | |||
| 162 | /** |
||
| 163 | * @return string variant of language conventions to use |
||
| 164 | */ |
||
| 165 | public function variant(): ?string |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param null|string $variant variant of language conventions to use |
||
| 172 | * @return self |
||
| 173 | */ |
||
| 174 | public function withVariant(?string $variant): self |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @return string|null Two-letter ISO-639-2 language code |
||
| 183 | * @see http://www.loc.gov/standards/iso639-2/ |
||
| 184 | */ |
||
| 185 | public function language(): string |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param null|string $language Two-letter ISO-639-2 language code |
||
| 192 | * @see http://www.loc.gov/standards/iso639-2/ |
||
| 193 | * @return self |
||
| 194 | */ |
||
| 195 | public function withLanguage(?string $language): self |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @return null|string ICU calendar |
||
| 204 | */ |
||
| 205 | public function calendar(): ?string |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param null|string $calendar ICU calendar |
||
| 212 | * @return self |
||
| 213 | */ |
||
| 214 | public function withCalendar(?string $calendar): self |
||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * @return null|string ICU collation |
||
| 224 | */ |
||
| 225 | public function collation(): ?string |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param null|string $collation ICU collation |
||
| 232 | * @return self |
||
| 233 | */ |
||
| 234 | public function withCollation(?string $collation): self |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @return null|string ICU numbers |
||
| 243 | */ |
||
| 244 | public function numbers(): ?string |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param null|string $numbers ICU numbers |
||
| 251 | * @return self |
||
| 252 | */ |
||
| 253 | public function withNumbers(?string $numbers): self |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @return string Two-letter ISO 3166-1 country code |
||
| 262 | * @see https://www.iso.org/iso-3166-country-codes.html |
||
| 263 | */ |
||
| 264 | public function region(): ?string |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param null|string $region Two-letter ISO 3166-1 country code |
||
| 271 | * @see https://www.iso.org/iso-3166-country-codes.html |
||
| 272 | * @return self |
||
| 273 | */ |
||
| 274 | public function withRegion(?string $region): self |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @return string ICU currency |
||
| 283 | */ |
||
| 284 | public function currency(): ?string |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param null|string $currency ICU currency |
||
| 291 | * @return self |
||
| 292 | */ |
||
| 293 | public function withCurrency(?string $currency): self |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @return null|string extended language subtags |
||
| 303 | */ |
||
| 304 | public function extendedLanguage(): ?string |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param null|string $extendedLanguage extended language subtags |
||
| 311 | * @return self |
||
| 312 | */ |
||
| 313 | public function withExtendedLanguage(?string $extendedLanguage): self |
||
| 320 | |||
| 321 | |||
| 322 | /** |
||
| 323 | * @return null|string |
||
| 324 | */ |
||
| 325 | public function private(): ?string |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param null|string $private |
||
| 332 | * @return self |
||
| 333 | */ |
||
| 334 | public function withPrivate(?string $private): self |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @return string regular expression for parsing BCP 47 |
||
| 344 | * @see https://tools.ietf.org/html/bcp47 |
||
| 345 | */ |
||
| 346 | private static function getBCP47Regex(): string |
||
| 363 | |||
| 364 | public function __toString(): string |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @return string |
||
| 371 | */ |
||
| 372 | public function asString(): string |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Returns fallback locale |
||
| 432 | * |
||
| 433 | * @return self fallback locale |
||
| 434 | */ |
||
| 435 | public function fallbackLocale(): self |
||
| 459 | } |
||
| 460 |
Late static binding only has effect in subclasses. A
finalclass 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.