| Conditions | 6 |
| Paths | 3 |
| Total Lines | 51 |
| 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 |
||
| 133 | public function write($session_id, $session_data) |
||
| 134 | { |
||
| 135 | // Check ability to safely encrypt and write content |
||
| 136 | if (!$this->canWrite() |
||
| 137 | || (strlen($session_data) > static::config()->get('max_length')) |
||
| 138 | || !($crypto = $this->getCrypto($session_id)) |
||
| 139 | ) { |
||
| 140 | if ($this->canWrite() && strlen($session_data) > static::config()->get('max_length')) { |
||
| 141 | $params = session_get_cookie_params(); |
||
| 142 | // Clear stored cookie value and cookie when length exceeds the set limit |
||
| 143 | $this->currentCookieData = null; |
||
| 144 | Cookie::set( |
||
| 145 | $this->cookie, |
||
| 146 | '', |
||
| 147 | 0, |
||
| 148 | $params['path'], |
||
| 149 | $params['domain'], |
||
| 150 | $params['secure'], |
||
| 151 | $params['httponly'] |
||
| 152 | ); |
||
| 153 | } |
||
| 154 | |||
| 155 | return false; |
||
| 156 | } |
||
| 157 | |||
| 158 | // Prepare content for write |
||
| 159 | $params = session_get_cookie_params(); |
||
| 160 | // Total max lifetime, stored internally |
||
| 161 | $lifetime = $this->getLifetime(); |
||
| 162 | $expiry = $this->getNow() + $lifetime; |
||
| 163 | |||
| 164 | // Restore the known good cookie value |
||
| 165 | $this->currentCookieData = $this->crypto->encrypt( |
||
| 166 | sprintf('%010u', $expiry) . $session_data |
||
| 167 | ); |
||
| 168 | |||
| 169 | // Respect auto-expire on browser close for the session cookie (in case the cookie lifetime is zero) |
||
| 170 | $cookieLifetime = min((int)$params['lifetime'], $lifetime); |
||
| 171 | |||
| 172 | Cookie::set( |
||
| 173 | $this->cookie, |
||
| 174 | $this->currentCookieData, |
||
| 175 | $cookieLifetime / 86400, |
||
| 176 | $params['path'], |
||
| 177 | $params['domain'], |
||
| 178 | $params['secure'], |
||
| 179 | $params['httponly'] |
||
| 180 | ); |
||
| 181 | |||
| 182 | return true; |
||
| 183 | } |
||
| 184 | |||
| 205 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: