| Conditions | 12 |
| Paths | 48 |
| Total Lines | 59 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 18 | public static function imageCache(string $url, bool $rewrite = false) |
||
| 19 | { |
||
| 20 | if (!self::$api) { |
||
| 21 | self::$api = new \Extender\request('https://unsplash.it'); |
||
| 22 | } |
||
| 23 | self::$url = $url; |
||
| 24 | $saved = self::$cache_dir . '/img/saved.json'; |
||
| 25 | self::$saved = $saved; |
||
| 26 | $res = read_file($saved, []); |
||
| 27 | if (is_string($res)) { |
||
| 28 | $res = json_decode($res, true); |
||
| 29 | } |
||
| 30 | |||
| 31 | self::$api->setUrl($url); |
||
| 32 | $url = self::$api->url; |
||
| 33 | $cache = self::$cache_dir . '/' . md5($url); |
||
| 34 | self::$cache = $cache; |
||
| 35 | if (file_exists($cache) && !headers_sent()) { |
||
| 36 | header_remove('x-powered-by'); |
||
| 37 | header_remove('pragma'); |
||
| 38 | self::send_cache_header($cache); |
||
| 39 | $lastModified = gmdate('D, d M Y H:i:s', filemtime($cache)) . ' GMT'; |
||
| 40 | header('Cache-Control: private'); |
||
| 41 | /* |
||
| 42 | * Send fallback headers |
||
| 43 | */ |
||
| 44 | //header('Content-type: image/jpeg'); |
||
| 45 | header('ETag: ' . md5_file($cache)); |
||
| 46 | header("Last-Modified: $lastModified"); |
||
| 47 | // 1 day expires |
||
| 48 | header('Expires: ' . gmdate('D, d M Y H:i:s', ((60 * 60 * 24 * 1) + strtotime($lastModified)))); |
||
| 49 | } |
||
| 50 | |||
| 51 | if (!isset($res[$url]) || (is_bool($rewrite) && $rewrite)) { |
||
| 52 | self::$api->set_method('get'); |
||
| 53 | self::$api->exec(); |
||
| 54 | for ($i = 0; $i < 2; ++$i) { |
||
| 55 | if (isset(self::$api->responseHeaders['Location'])) { |
||
| 56 | self::$api->get(self::$api->responseHeaders['Location']); |
||
| 57 | } else { |
||
| 58 | break; |
||
| 59 | } |
||
| 60 | } |
||
| 61 | $res[$url] = self::$api->url; |
||
| 62 | write_file($saved, $res, true); |
||
| 63 | write_file($cache, self::$api->response, true); |
||
| 64 | header('Content-Type: ' . self::$api->responseHeaders['Content-Type'], true); |
||
| 65 | echo self::$api->response; |
||
| 66 | } elseif (file_exists($cache)) { |
||
| 67 | header('Content-Type: ' . mime_content_type($cache), true); |
||
| 68 | $read = read_file($cache); |
||
| 69 | if (!empty($read)) { |
||
| 70 | echo $read; |
||
| 71 | } else { |
||
| 72 | return self::{__FUNCTION__}($url, true); |
||
| 73 | } |
||
| 74 | } else { |
||
| 75 | self::$api->set_url($res[$url])->set_method('get')->exec(); |
||
| 76 | self::writeCache(); |
||
| 77 | } |
||
| 214 |