Conditions | 14 |
Paths | 80 |
Total Lines | 51 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
35 | public function resolveLocale(Request $request, array $availableLocales) |
||
36 | { |
||
37 | if (!empty($this->hostMap) && isset($this->hostMap[$host = $request->getHost()])) { |
||
38 | return $this->hostMap[$host]; |
||
39 | } |
||
40 | |||
41 | // if a locale has been specifically set as a query parameter, use it |
||
42 | if ($request->query->has('hl')) { |
||
43 | $hostLanguage = $request->query->get('hl'); |
||
44 | |||
45 | if (preg_match('#^[a-z]{2}(?:_[a-z]{2})?$#i', $hostLanguage)) { |
||
46 | return $hostLanguage; |
||
47 | } |
||
48 | } |
||
49 | |||
50 | // check if a session exists, and if it contains a locale |
||
51 | if ($request->hasPreviousSession()) { |
||
52 | $session = $request->getSession(); |
||
53 | if ($session->has('_locale')) { |
||
54 | return $session->get('_locale'); |
||
55 | } |
||
56 | } |
||
57 | |||
58 | // if user sends a cookie, use it |
||
59 | if ($request->cookies->has($this->cookieName)) { |
||
60 | $hostLanguage = $request->cookies->get($this->cookieName); |
||
61 | |||
62 | if (preg_match('#^[a-z]{2}(?:_[a-z]{2})?$#i', $hostLanguage)) { |
||
63 | return $hostLanguage; |
||
64 | } |
||
65 | } |
||
66 | |||
67 | $languages = []; |
||
68 | foreach ($request->getLanguages() as $language) { |
||
69 | if (strlen($language) != 2) { |
||
70 | $newLang = explode('_', $language, 2); |
||
71 | $languages[] = reset($newLang); |
||
72 | } else { |
||
73 | $languages[] = $language; |
||
74 | } |
||
75 | } |
||
76 | $languages = array_unique($languages); |
||
77 | if (!empty($languages)) { |
||
78 | foreach ($languages as $lang) { |
||
79 | if (in_array($lang, $availableLocales, true)) { |
||
80 | return $lang; |
||
81 | } |
||
82 | } |
||
83 | } |
||
84 | |||
85 | return null; |
||
86 | } |
||
88 |