Complex classes like LanguageMiddleware 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 LanguageMiddleware, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class LanguageMiddleware |
||
21 | { |
||
22 | use TranslatorAwareTrait; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $defaultLanguage; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $browserLanguage; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | private $excludedPath; |
||
38 | |||
39 | /** |
||
40 | * @var boolean |
||
41 | */ |
||
42 | private $usePath; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private $pathRegexp; |
||
48 | |||
49 | /** |
||
50 | * @var boolean |
||
51 | */ |
||
52 | private $useBrowser; |
||
53 | |||
54 | /** |
||
55 | * @var boolean |
||
56 | */ |
||
57 | private $useSession; |
||
58 | |||
59 | /** |
||
60 | * @var string[] |
||
61 | */ |
||
62 | private $sessionKey; |
||
63 | |||
64 | /** |
||
65 | * @var boolean |
||
66 | */ |
||
67 | private $useParams; |
||
68 | |||
69 | /** |
||
70 | * @var string[] |
||
71 | */ |
||
72 | private $paramKey; |
||
73 | |||
74 | /** |
||
75 | * @var boolean |
||
76 | */ |
||
77 | private $useHost; |
||
78 | |||
79 | /** |
||
80 | * @var array |
||
81 | */ |
||
82 | private $hostMap; |
||
83 | |||
84 | /** |
||
85 | * @var boolean |
||
86 | */ |
||
87 | private $setLocale; |
||
88 | |||
89 | /** |
||
90 | * @param array $data The middleware options. |
||
91 | */ |
||
92 | public function __construct(array $data) |
||
118 | |||
119 | /** |
||
120 | * Default middleware options. |
||
121 | * |
||
122 | * @return array |
||
123 | */ |
||
124 | public function defaults() |
||
148 | |||
149 | /** |
||
150 | * @param RequestInterface $request The PSR-7 HTTP request. |
||
151 | * @param ResponseInterface $response The PSR-7 HTTP response. |
||
152 | * @param callable $next The next middleware callable in the stack. |
||
153 | * @return ResponseInterface |
||
154 | */ |
||
155 | public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next) |
||
171 | |||
172 | /** |
||
173 | * @param RequestInterface $request The PSR-7 HTTP request. |
||
174 | * @return null|string |
||
175 | */ |
||
176 | private function getLanguage(RequestInterface $request) |
||
215 | |||
216 | /** |
||
217 | * @param RequestInterface $request The PSR-7 HTTP request. |
||
218 | * @return null|string |
||
219 | */ |
||
220 | private function getLanguageFromHost(RequestInterface $request) |
||
229 | |||
230 | /** |
||
231 | * @param RequestInterface $request The PSR-7 HTTP request. |
||
232 | * @return null|string |
||
233 | */ |
||
234 | private function getLanguageFromPath(RequestInterface $request) |
||
249 | |||
250 | /** |
||
251 | * @param RequestInterface $request The PSR-7 HTTP request. |
||
252 | * @return string |
||
253 | */ |
||
254 | private function getLanguageFromParams(RequestInterface $request) |
||
268 | |||
269 | /** |
||
270 | * @return string |
||
271 | */ |
||
272 | private function getLanguageFromSession() |
||
283 | |||
284 | /** |
||
285 | * @return mixed |
||
286 | */ |
||
287 | private function getLanguageFromBrowser() |
||
291 | |||
292 | /** |
||
293 | * @param string $lang The language code to set. |
||
294 | * @return void |
||
295 | */ |
||
296 | private function setLanguage($lang) |
||
310 | |||
311 | /** |
||
312 | * @param string $lang The language code to set. |
||
313 | * @return void |
||
314 | */ |
||
315 | private function setLocale($lang) |
||
343 | } |
||
344 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: