Complex classes like Translator 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 Translator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Translator extends SymfonyTranslator |
||
22 | { |
||
23 | /** |
||
24 | * The locales manager. |
||
25 | * |
||
26 | * @var LocalesManager |
||
27 | */ |
||
28 | private $manager; |
||
29 | |||
30 | /** |
||
31 | * The message selector. |
||
32 | * |
||
33 | * @var MessageSelector |
||
34 | */ |
||
35 | private $selector; |
||
|
|||
36 | |||
37 | /** |
||
38 | * The loaded domains. |
||
39 | * |
||
40 | * @var string[] |
||
41 | */ |
||
42 | private $domains = [ 'messages' ]; |
||
43 | |||
44 | /** |
||
45 | * @param array $data Constructor data. |
||
46 | */ |
||
47 | public function __construct(array $data) |
||
73 | |||
74 | /** |
||
75 | * Adds a resource. |
||
76 | * |
||
77 | * @see SymfonyTranslator::addResource() Keep track of the translation domains. |
||
78 | * @param string $format The name of the loader (@see addLoader()). |
||
79 | * @param mixed $resource The resource name. |
||
80 | * @param string $locale The locale. |
||
81 | * @param string|null $domain The domain. |
||
82 | * @return void |
||
83 | */ |
||
84 | public function addResource($format, $resource, $locale, $domain = null) |
||
92 | |||
93 | /** |
||
94 | * Retrieve the loaded domains. |
||
95 | * |
||
96 | * @return string[] |
||
97 | */ |
||
98 | public function availableDomains() |
||
102 | |||
103 | /** |
||
104 | * Retrieve a translation object from a (mixed) message. |
||
105 | * |
||
106 | * @uses SymfonyTranslator::trans() |
||
107 | * @param mixed $val The string or translation-object to retrieve. |
||
108 | * @param array $parameters An array of parameters for the message. |
||
109 | * @param string|null $domain The domain for the message or NULL to use the default. |
||
110 | * @return Translation|null The translation object or NULL if the value is not translatable. |
||
111 | */ |
||
112 | public function translation($val, array $parameters = [], $domain = null) |
||
127 | |||
128 | /** |
||
129 | * Translates the given (mixed) message. |
||
130 | * |
||
131 | * @uses SymfonyTranslator::trans() |
||
132 | * @uses Translator::translation() |
||
133 | * @param mixed $val The string or translation-object to retrieve. |
||
134 | * @param array $parameters An array of parameters for the message. |
||
135 | * @param string|null $domain The domain for the message or NULL to use the default. |
||
136 | * @param string|null $locale The locale or NULL to use the default. |
||
137 | * @return string The translated string |
||
138 | */ |
||
139 | public function translate($val, array $parameters = [], $domain = null, $locale = null) |
||
160 | |||
161 | /** |
||
162 | * Retrieve a translation object from a (mixed) message by choosing a translation according to a number. |
||
163 | * |
||
164 | * @uses SymfonyTranslator::transChoice() |
||
165 | * @param mixed $val The string or translation-object to retrieve. |
||
166 | * @param integer $number The number to use to find the indice of the message. |
||
167 | * @param array $parameters An array of parameters for the message. |
||
168 | * @param string|null $domain The domain for the message or NULL to use the default. |
||
169 | * @return Translation|null The translation object or NULL if the value is not translatable. |
||
170 | */ |
||
171 | public function translationChoice($val, $number, array $parameters = [], $domain = null) |
||
195 | |||
196 | /** |
||
197 | * Translates the given (mixed) choice message by choosing a translation according to a number. |
||
198 | * |
||
199 | * @uses SymfonyTranslator::transChoice() |
||
200 | * @uses Translator::translationChoice() |
||
201 | * @param mixed $val The string or translation-object to retrieve. |
||
202 | * @param integer $number The number to use to find the indice of the message. |
||
203 | * @param array $parameters An array of parameters for the message. |
||
204 | * @param string|null $domain The domain for the message or NULL to use the default. |
||
205 | * @param string|null $locale The locale or NULL to use the default. |
||
206 | * @return string The translated string |
||
207 | */ |
||
208 | public function translateChoice($val, $number, array $parameters = [], $domain = null, $locale = null) |
||
236 | |||
237 | /** |
||
238 | * Retrieve the available locales information. |
||
239 | * |
||
240 | * @return array |
||
241 | */ |
||
242 | public function locales() |
||
246 | |||
247 | /** |
||
248 | * Retrieve the available locales (language codes). |
||
249 | * |
||
250 | * @return string[] |
||
251 | */ |
||
252 | public function availableLocales() |
||
256 | |||
257 | /** |
||
258 | * Sets the current locale. |
||
259 | * |
||
260 | * @see SymfonyTranslator::setLocale() Ensure that the method also changes the locales manager's language. |
||
261 | * @param string $locale The locale. |
||
262 | * @return void |
||
263 | */ |
||
264 | public function setLocale($locale) |
||
270 | |||
271 | /** |
||
272 | * Set the locales manager. |
||
273 | * |
||
274 | * @param LocalesManager $manager The locales manager. |
||
275 | * @return void |
||
276 | */ |
||
277 | private function setManager(LocalesManager $manager) |
||
281 | |||
282 | /** |
||
283 | * Retrieve the locales manager. |
||
284 | * |
||
285 | * @return LocalesManager |
||
286 | */ |
||
287 | protected function manager() |
||
291 | |||
292 | /** |
||
293 | * Set the message selector. |
||
294 | * |
||
295 | * The {@see SymfonyTranslator} keeps the message selector private (as of 3.3.2), |
||
296 | * thus we must explicitly require it in this class to guarantee access. |
||
297 | * |
||
298 | * @param MessageSelector $selector The selector. |
||
299 | * @return void |
||
300 | */ |
||
301 | public function setSelector(MessageSelector $selector) |
||
305 | |||
306 | /** |
||
307 | * Retrieve the message selector. |
||
308 | * |
||
309 | * @return MessageSelector |
||
310 | */ |
||
311 | protected function selector() |
||
315 | |||
316 | /** |
||
317 | * Determine if the value is translatable. |
||
318 | * |
||
319 | * @param mixed $val The value to be checked. |
||
320 | * @return boolean |
||
321 | */ |
||
322 | protected function isValidTranslation($val) |
||
353 | } |
||
354 |