Complex classes like DateLocalizationCapabilities 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 DateLocalizationCapabilities, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | trait DateLocalizationCapabilities |
||
15 | { |
||
16 | /** |
||
17 | * A translator to ... er ... translate stuff. |
||
18 | * |
||
19 | * @var TranslatorInterface |
||
20 | */ |
||
21 | protected static $translator; |
||
22 | |||
23 | /////////////////////////////////////////////////////////////////// |
||
24 | /////////////////////// LOCALIZATION ////////////////////////////// |
||
25 | /////////////////////////////////////////////////////////////////// |
||
26 | |||
27 | /** |
||
28 | * Intialize the translator instance if necessary. |
||
29 | * |
||
30 | * @return TranslatorInterface |
||
31 | */ |
||
32 | protected static function translator() |
||
43 | |||
44 | /** |
||
45 | * Get the translator instance in use. |
||
46 | * |
||
47 | * @return TranslatorInterface |
||
48 | */ |
||
49 | public static function getTranslator() |
||
53 | |||
54 | /** |
||
55 | * Set the translator instance to use. |
||
56 | * |
||
57 | * @param TranslatorInterface $translator |
||
58 | */ |
||
59 | public static function setTranslator(TranslatorInterface $translator) |
||
63 | |||
64 | /** |
||
65 | * Get the current translator locale. |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | public static function getLocale() |
||
73 | |||
74 | /** |
||
75 | * Set the current translator locale. |
||
76 | * |
||
77 | * @param string $locale |
||
78 | * |
||
79 | * @return bool |
||
80 | */ |
||
81 | public static function setLocale($locale) |
||
87 | |||
88 | /** |
||
89 | * @param string $locale |
||
90 | * |
||
91 | * @return bool |
||
92 | */ |
||
93 | protected static function loadLocaleResource($locale) |
||
110 | |||
111 | /** |
||
112 | * @param $locale |
||
113 | * |
||
114 | * @return string[] |
||
115 | */ |
||
116 | protected static function getLocaleResourceFilepaths($locale) |
||
138 | } |
||
139 |