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 |
||
25 | class TranslateClient |
||
26 | { |
||
27 | /** |
||
28 | * @var TranslateClient Because nobody cares about singletons |
||
29 | */ |
||
30 | private static $staticInstance; |
||
31 | |||
32 | /** |
||
33 | * @var \GuzzleHttp\Client HTTP Client |
||
34 | */ |
||
35 | private $httpClient; |
||
36 | |||
37 | /** |
||
38 | * @var string Source language - from where the string should be translated |
||
39 | */ |
||
40 | private $sourceLanguage; |
||
41 | |||
42 | /** |
||
43 | * @var string Target language - to which language string should be translated |
||
44 | */ |
||
45 | private $targetLanguage; |
||
46 | |||
47 | /** |
||
48 | * @var string|bool Last detected source language |
||
49 | */ |
||
50 | private static $lastDetectedSource; |
||
51 | |||
52 | /** |
||
53 | * @var string Google Translate URL base |
||
54 | */ |
||
55 | private $urlBase = 'http://translate.google.com/translate_a/t'; |
||
56 | |||
57 | /** |
||
58 | * @var array URL Parameters |
||
59 | */ |
||
60 | private $urlParams = [ |
||
61 | 'client' => 't', |
||
62 | 'hl' => 'en', |
||
63 | 'sl' => null, // Source language |
||
64 | 'tl' => null, // Target language |
||
65 | 'text' => null, // String to translate |
||
66 | 'ie' => 'UTF-8', // Input encoding |
||
67 | 'oe' => 'UTF-8', // Output encoding |
||
68 | 'multires' => 1, |
||
69 | 'otf' => 0, |
||
70 | 'pc' => 1, |
||
71 | 'trs' => 1, |
||
72 | 'ssel' => 0, |
||
73 | 'tsel' => 0, |
||
74 | 'sc' => 1, |
||
75 | 'tk' => null, |
||
76 | ]; |
||
77 | |||
78 | /** |
||
79 | * @var array Regex key-value patterns to replace on response data |
||
80 | */ |
||
81 | private $resultRegexes = [ |
||
82 | '/,+/' => ',', |
||
83 | '/\[,/' => '[', |
||
84 | ]; |
||
85 | |||
86 | /** |
||
87 | * @var TokenProviderInterface |
||
88 | */ |
||
89 | private $tokenProvider; |
||
90 | |||
91 | /** |
||
92 | * @var string Default token generator class name |
||
93 | */ |
||
94 | private $defaultTokenProvider = GoogleTokenGenerator::class; |
||
95 | |||
96 | /** |
||
97 | * Class constructor. |
||
98 | * |
||
99 | * For more information about HTTP client configuration options, visit |
||
100 | * "Creating a client" section of GuzzleHttp docs. |
||
101 | * 5.x - http://guzzle.readthedocs.org/en/5.3/clients.html#creating-a-client |
||
102 | * |
||
103 | * @param string $source Source language (Optional) |
||
104 | * @param string $target Target language (Optional) |
||
105 | * @param array $options Associative array of http client configuration options (Optional) |
||
106 | * |
||
107 | * @throws Exception If token provider does not implement TokenProviderInterface |
||
108 | */ |
||
109 | public function __construct($source = null, $target = 'en', $options = [], TokenProviderInterface $tokener = null) |
||
127 | |||
128 | /** |
||
129 | * Override translate method for static call. |
||
130 | * |
||
131 | * @throws BadMethodCallException If calling nonexistent method |
||
132 | * @throws InvalidArgumentException If parameters are passed incorrectly |
||
133 | * @throws InvalidArgumentException If the provided argument is not of type 'string' |
||
134 | * @throws ErrorException If the HTTP request fails |
||
135 | * @throws UnexpectedValueException If received data cannot be decoded |
||
136 | */ |
||
137 | public static function __callStatic($name, $args) |
||
157 | |||
158 | /** |
||
159 | * Override translate method for instance call. |
||
160 | * |
||
161 | * @throws BadMethodCallException If calling nonexistent method |
||
162 | * @throws InvalidArgumentException If parameters are passed incorrectly |
||
163 | * @throws InvalidArgumentException If the provided argument is not of type 'string' |
||
164 | * @throws ErrorException If the HTTP request fails |
||
165 | * @throws UnexpectedValueException If received data cannot be decoded |
||
166 | */ |
||
167 | public function __call($name, $args) |
||
190 | |||
191 | /** |
||
192 | * Check if static instance exists and instantiate if not. |
||
193 | * |
||
194 | * @return void |
||
195 | */ |
||
196 | private static function checkStaticInstance() |
||
202 | |||
203 | /** |
||
204 | * Set source language we are transleting from. |
||
205 | * |
||
206 | * @param string $source Language code |
||
207 | * |
||
208 | * @return TranslateClient |
||
209 | */ |
||
210 | public function setSource($source = null) |
||
216 | |||
217 | /** |
||
218 | * Set translation language we are transleting to. |
||
219 | * |
||
220 | * @param string $target Language code |
||
221 | * |
||
222 | * @return TranslateClient |
||
223 | */ |
||
224 | public function setTarget($target) |
||
230 | |||
231 | /** |
||
232 | * Get response array. |
||
233 | * |
||
234 | * @param string|array $data String or array of strings to translate |
||
235 | * |
||
236 | * @throws InvalidArgumentException If the provided argument is not of type 'string' |
||
237 | * @throws ErrorException If the HTTP request fails |
||
238 | * @throws UnexpectedValueException If received data cannot be decoded |
||
239 | * |
||
240 | * @return array Response |
||
241 | */ |
||
242 | private function getResponse($data) |
||
277 | |||
278 | /** |
||
279 | * Translate text. |
||
280 | * |
||
281 | * This can be called from instance method translate() using __call() magic method. |
||
282 | * Use $instance->translate($string) instead. |
||
283 | * |
||
284 | * @param string|array $data Text or array of texts to translate |
||
285 | * |
||
286 | * @throws InvalidArgumentException If the provided argument is not of type 'string' |
||
287 | * @throws ErrorException If the HTTP request fails |
||
288 | * @throws UnexpectedValueException If received data cannot be decoded |
||
289 | * |
||
290 | * @return string|bool Translated text |
||
291 | */ |
||
292 | private function instanceTranslate($data) |
||
357 | |||
358 | /** |
||
359 | * Translate text statically. |
||
360 | * |
||
361 | * This can be called from static method translate() using __callStatic() magic method. |
||
362 | * Use TranslateClient::translate($source, $target, $string) instead. |
||
363 | * |
||
364 | * @param string $source Source language |
||
365 | * @param string $target Target language |
||
366 | * @param string $string Text to translate |
||
367 | * |
||
368 | * @throws InvalidArgumentException If the provided argument is not of type 'string' |
||
369 | * @throws ErrorException If the HTTP request fails |
||
370 | * @throws UnexpectedValueException If received data cannot be decoded |
||
371 | * |
||
372 | * @return string|bool Translated text |
||
373 | */ |
||
374 | private static function staticTranslate($source, $target, $string) |
||
388 | |||
389 | /** |
||
390 | * Get last detected language. |
||
391 | * |
||
392 | * @return string|bool Last detected language or boolean FALSE |
||
393 | */ |
||
394 | private static function staticGetLastDetectedSource() |
||
398 | |||
399 | /** |
||
400 | * Check if given locale is valid. |
||
401 | * |
||
402 | * @param string $lang Langauge code to verify |
||
403 | * |
||
404 | * @return bool |
||
405 | */ |
||
406 | private function isValidLocale($lang) |
||
410 | } |
||
411 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.