Complex classes like TranslateClient 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 TranslateClient, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class TranslateClient |
||
30 | { |
||
31 | /** |
||
32 | * @var TranslateClient Because nobody cares about singletons |
||
33 | */ |
||
34 | private static $staticInstance; |
||
35 | |||
36 | /** |
||
37 | * @var \GuzzleHttp\Client HTTP Client |
||
38 | */ |
||
39 | private $httpClient; |
||
40 | |||
41 | /** |
||
42 | * @var string Source language - from where the string should be translated |
||
43 | */ |
||
44 | private $sourceLanguage; |
||
45 | |||
46 | /** |
||
47 | * @var string Target language - to which language string should be translated |
||
48 | */ |
||
49 | private $targetLanguage; |
||
50 | |||
51 | /** |
||
52 | * @var string|bool Last detected source language |
||
53 | */ |
||
54 | private static $lastDetectedSource; |
||
55 | |||
56 | /** |
||
57 | * @var string Google Translate URL base |
||
58 | */ |
||
59 | private $urlBase = 'https://translate.google.com/translate_a/single'; |
||
60 | |||
61 | /** |
||
62 | * @var array Dynamic guzzleHTTP client options |
||
63 | */ |
||
64 | private $httpOptions = []; |
||
65 | |||
66 | /** |
||
67 | * @var array URL Parameters |
||
68 | */ |
||
69 | private $urlParams = [ |
||
70 | 'client' => 't', |
||
71 | 'hl' => 'en', |
||
72 | 'dt' => 't', |
||
73 | 'sl' => null, // Source language |
||
74 | 'tl' => null, // Target language |
||
75 | 'q' => null, // String to translate |
||
76 | 'ie' => 'UTF-8', // Input encoding |
||
77 | 'oe' => 'UTF-8', // Output encoding |
||
78 | 'multires' => 1, |
||
79 | 'otf' => 0, |
||
80 | 'pc' => 1, |
||
81 | 'trs' => 1, |
||
82 | 'ssel' => 0, |
||
83 | 'tsel' => 0, |
||
84 | 'kc' => 1, |
||
85 | 'tk' => null, |
||
86 | ]; |
||
87 | |||
88 | /** |
||
89 | * @var array Regex key-value patterns to replace on response data |
||
90 | */ |
||
91 | private $resultRegexes = [ |
||
92 | '/,+/' => ',', |
||
93 | '/\[,/' => '[', |
||
94 | ]; |
||
95 | |||
96 | /** |
||
97 | * @var TokenProviderInterface |
||
98 | */ |
||
99 | private $tokenProvider; |
||
100 | |||
101 | /** |
||
102 | * @var string Default token generator class name |
||
103 | */ |
||
104 | private $defaultTokenProvider = GoogleTokenGenerator::class; |
||
105 | |||
106 | /** |
||
107 | * Class constructor. |
||
108 | * |
||
109 | * For more information about HTTP client configuration options, visit |
||
110 | * "Creating a client" section of GuzzleHttp docs. |
||
111 | * 5.x - http://guzzle.readthedocs.org/en/5.3/clients.html#creating-a-client |
||
112 | * |
||
113 | * @param string $source Source language (Optional) |
||
114 | * @param string $target Target language (Optional) |
||
115 | * @param array $options Associative array of http client configuration options (Optional) |
||
116 | * |
||
117 | * @throws Exception If token provider does not implement TokenProviderInterface |
||
118 | */ |
||
119 | public function __construct($source = null, $target = 'en', $options = [], TokenProviderInterface $tokener = null) |
||
137 | |||
138 | /** |
||
139 | * Override translate method for static call. |
||
140 | * |
||
141 | * @throws BadMethodCallException If calling nonexistent method |
||
142 | * @throws InvalidArgumentException If parameters are passed incorrectly |
||
143 | * @throws InvalidArgumentException If the provided argument is not of type 'string' |
||
144 | * @throws ErrorException If the HTTP request fails |
||
145 | * @throws UnexpectedValueException If received data cannot be decoded |
||
146 | */ |
||
147 | public static function __callStatic($name, $args) |
||
167 | |||
168 | /** |
||
169 | * Override translate method for instance call. |
||
170 | * |
||
171 | * @throws BadMethodCallException If calling nonexistent method |
||
172 | * @throws InvalidArgumentException If parameters are passed incorrectly |
||
173 | * @throws InvalidArgumentException If the provided argument is not of type 'string' |
||
174 | * @throws ErrorException If the HTTP request fails |
||
175 | * @throws UnexpectedValueException If received data cannot be decoded |
||
176 | */ |
||
177 | public function __call($name, $args) |
||
200 | |||
201 | /** |
||
202 | * Check if static instance exists and instantiate if not. |
||
203 | * |
||
204 | * @return void |
||
205 | */ |
||
206 | private static function checkStaticInstance() |
||
212 | |||
213 | /** |
||
214 | * Set the api we are used to translete. |
||
215 | * |
||
216 | * @param string $source Google translate api, default is https://translate.google.com/translate_a/single |
||
|
|||
217 | * |
||
218 | * @return TranslateClient |
||
219 | */ |
||
220 | public function setApi($api = null) |
||
228 | |||
229 | /** |
||
230 | * Set source language we are transleting from. |
||
231 | * |
||
232 | * @param string $source Language code |
||
233 | * |
||
234 | * @return TranslateClient |
||
235 | */ |
||
236 | public function setSource($source = null) |
||
242 | |||
243 | /** |
||
244 | * Set translation language we are transleting to. |
||
245 | * |
||
246 | * @param string $target Language code |
||
247 | * |
||
248 | * @return TranslateClient |
||
249 | */ |
||
250 | public function setTarget($target) |
||
256 | |||
257 | /** |
||
258 | * Set Google Translate URL base |
||
259 | * |
||
260 | * @param string $urlBase Google Translate URL base |
||
261 | * |
||
262 | * @return TranslateClient |
||
263 | */ |
||
264 | public function setUrlBase($urlBase) |
||
270 | |||
271 | /** |
||
272 | * Set guzzleHttp client options. |
||
273 | * |
||
274 | * @param array $options guzzleHttp client options. |
||
275 | * |
||
276 | * @return TranslateClient |
||
277 | */ |
||
278 | public function setHttpOption(array $options) |
||
284 | |||
285 | /** |
||
286 | * Get response array. |
||
287 | * |
||
288 | * @param string|array $data String or array of strings to translate |
||
289 | * |
||
290 | * @throws InvalidArgumentException If the provided argument is not of type 'string' |
||
291 | * @throws ErrorException If the HTTP request fails |
||
292 | * @throws UnexpectedValueException If received data cannot be decoded |
||
293 | * |
||
294 | * @return array Response |
||
295 | */ |
||
296 | private function getResponse($data) |
||
339 | |||
340 | /** |
||
341 | * Translate text. |
||
342 | * |
||
343 | * This can be called from instance method translate() using __call() magic method. |
||
344 | * Use $instance->translate($string) instead. |
||
345 | * |
||
346 | * @param string|array $data Text or array of texts to translate |
||
347 | * |
||
348 | * @throws InvalidArgumentException If the provided argument is not of type 'string' |
||
349 | * @throws ErrorException If the HTTP request fails |
||
350 | * @throws UnexpectedValueException If received data cannot be decoded |
||
351 | * |
||
352 | * @return string|bool Translated text |
||
353 | */ |
||
354 | private function instanceTranslate($data) |
||
433 | |||
434 | /** |
||
435 | * Translate text statically. |
||
436 | * |
||
437 | * This can be called from static method translate() using __callStatic() magic method. |
||
438 | * Use TranslateClient::translate($source, $target, $string) instead. |
||
439 | * |
||
440 | * @param string $source Source language |
||
441 | * @param string $target Target language |
||
442 | * @param string $string Text to translate |
||
443 | * |
||
444 | * @throws InvalidArgumentException If the provided argument is not of type 'string' |
||
445 | * @throws ErrorException If the HTTP request fails |
||
446 | * @throws UnexpectedValueException If received data cannot be decoded |
||
447 | * |
||
448 | * @return string|bool Translated text |
||
449 | */ |
||
450 | private static function staticTranslate($source, $target, $string) |
||
464 | |||
465 | /** |
||
466 | * Get last detected language. |
||
467 | * |
||
468 | * @return string|bool Last detected language or boolean FALSE |
||
469 | */ |
||
470 | private static function staticGetLastDetectedSource() |
||
474 | |||
475 | /** |
||
476 | * Check if given locale is valid. |
||
477 | * |
||
478 | * @param string $lang Langauge code to verify |
||
479 | * |
||
480 | * @return bool |
||
481 | */ |
||
482 | private function isValidLocale($lang) |
||
486 | } |
||
487 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.