| Conditions | 11 |
| Paths | 15 |
| Total Lines | 44 |
| Code Lines | 22 |
| 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 |
||
| 70 | public function setLanguage() |
||
| 71 | { |
||
| 72 | if ($this->_controller->Auth->user()) { |
||
| 73 | //The user has already a valid language defined in the database. |
||
| 74 | if ($this->_session->read('Auth.User.language') && isset($this->_locales[$this->_session->read('Auth.User.language')])) { |
||
| 75 | //If the user has not the cookie, we set the cookie. |
||
| 76 | if (!$this->_cookie->check('language') || $this->_cookie->read('language') != $this->_session->read('Auth.User.language')) { |
||
| 77 | $this->_cookie->write('language', $this->_session->read('Auth.User.language')); |
||
| 78 | } |
||
| 79 | //Stock the locale of the user. |
||
| 80 | $this->_locale = $this->_session->read('Auth.User.language'); |
||
| 81 | } |
||
| 82 | } else { |
||
| 83 | //The user has a valid cookie. |
||
| 84 | if ($this->_cookie->check('language') && isset($this->_locales[$this->_cookie->read('language')])) { |
||
| 85 | $this->_locale = $this->_cookie->read('language'); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | //The user want to change his language. |
||
| 90 | if (!is_null($this->_controller->request->getParam('lang')) && isset($this->_locales[$this->_controller->request->getParam('lang')])) { |
||
| 91 | //If the user is connected, we need to save the new language in the database and refresh his session. |
||
| 92 | if ($this->_controller->Auth->user()) { |
||
| 93 | $this->_controller->loadModel('Users'); |
||
| 94 | |||
| 95 | $user = $this->_controller->Users |
||
| 96 | ->find() |
||
| 97 | ->where(['id' => $this->_session->read('Auth.User.id')]) |
||
| 98 | ->first(); |
||
| 99 | |||
| 100 | $user->language = $this->_controller->request->getParam('lang'); |
||
| 101 | $this->_controller->Users->save($user); |
||
| 102 | $this->_session->write('Auth.User.language', $this->_controller->request->getParam('lang')); |
||
| 103 | } |
||
| 104 | |||
| 105 | //Save the new language in the cookie. |
||
| 106 | $this->_cookie->write('language', $this->_controller->request->getParam('lang')); |
||
| 107 | |||
| 108 | $this->_locale = $this->_controller->request->getParam('lang'); |
||
| 109 | } |
||
| 110 | |||
| 111 | //Set the locale. |
||
| 112 | I18n::locale($this->_locale); |
||
| 113 | } |
||
| 114 | } |
||
| 115 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..