Conditions | 14 |
Paths | 30 |
Total Lines | 54 |
Code Lines | 26 |
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 |
||
117 | public static function isExpired(Google_Http_Request $resp) |
||
118 | { |
||
119 | // HTTP/1.1 clients and caches MUST treat other invalid date formats, |
||
120 | // especially including the value “0”, as in the past. |
||
121 | $parsedExpires = false; |
||
122 | $responseHeaders = $resp->getResponseHeaders(); |
||
123 | |||
124 | if (isset($responseHeaders['expires'])) { |
||
125 | $rawExpires = $responseHeaders['expires']; |
||
126 | // Check for a malformed expires header first. |
||
127 | if (empty($rawExpires) || (is_numeric($rawExpires) && $rawExpires <= 0)) { |
||
128 | return true; |
||
129 | } |
||
130 | |||
131 | // See if we can parse the expires header. |
||
132 | $parsedExpires = strtotime($rawExpires); |
||
133 | if (false == $parsedExpires || $parsedExpires <= 0) { |
||
134 | return true; |
||
135 | } |
||
136 | } |
||
137 | |||
138 | // Calculate the freshness of an http response. |
||
139 | $freshnessLifetime = false; |
||
140 | $cacheControl = $resp->getParsedCacheControl(); |
||
141 | if (isset($cacheControl['max-age'])) { |
||
142 | $freshnessLifetime = $cacheControl['max-age']; |
||
143 | } |
||
144 | |||
145 | $rawDate = $resp->getResponseHeader('date'); |
||
146 | $parsedDate = strtotime($rawDate); |
||
147 | |||
148 | if (empty($rawDate) || false == $parsedDate) { |
||
149 | // We can't default this to now, as that means future cache reads |
||
150 | // will always pass with the logic below, so we will require a |
||
151 | // date be injected if not supplied. |
||
152 | throw new Google_Exception("All cacheable requests must have creation dates."); |
||
153 | } |
||
154 | |||
155 | if (false == $freshnessLifetime && isset($responseHeaders['expires'])) { |
||
156 | $freshnessLifetime = $parsedExpires - $parsedDate; |
||
157 | } |
||
158 | |||
159 | if (false == $freshnessLifetime) { |
||
160 | return true; |
||
161 | } |
||
162 | |||
163 | // Calculate the age of an http response. |
||
164 | $age = max(0, time() - $parsedDate); |
||
165 | if (isset($responseHeaders['age'])) { |
||
166 | $age = max($age, strtotime($responseHeaders['age'])); |
||
167 | } |
||
168 | |||
169 | return $freshnessLifetime <= $age; |
||
170 | } |
||
171 | |||
186 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.