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