Conditions | 13 |
Paths | 48 |
Total Lines | 64 |
Code Lines | 49 |
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 |
||
26 | public static function imageCache(string $url, bool $rewrite = false) |
||
27 | { |
||
28 | resolve_file(self::$cache_dir . '/.htaccess', 'deny from all'); |
||
29 | if (!self::$api) { |
||
30 | self::$api = new \Extender\request('https://unsplash.it'); |
||
31 | } |
||
32 | self::$url = $url; |
||
33 | self::$api->setCookieFile(self::$cache_dir . '/cookies.txt'); |
||
34 | self::$api->setCookieJar(self::$cache_dir . '/cookies.txt'); |
||
35 | self::$api->setReferrer($url); |
||
36 | self::$api->setUserAgent($_SERVER['HTTP_USER_AGENT']); |
||
37 | $saved = resolve_file(self::$cache_dir . '/saved.json', '{}'); |
||
38 | self::$saved = $saved; |
||
39 | $res = read_file($saved, []); |
||
40 | if (is_string($res) && is_json($res)) { |
||
41 | $res = json_decode($res, true); |
||
42 | } |
||
43 | |||
44 | self::$api->setUrl($url); |
||
45 | $url = self::$api->url; |
||
46 | $cache = self::$cache_dir . '/' . md5($url); |
||
47 | self::$cache = $cache; |
||
48 | if (file_exists($cache) && !headers_sent()) { |
||
49 | header_remove('x-powered-by'); |
||
50 | header_remove('pragma'); |
||
51 | self::send_cache_header($cache); |
||
52 | $lastModified = gmdate('D, d M Y H:i:s', filemtime($cache)) . ' GMT'; |
||
53 | header('Cache-Control: private'); |
||
54 | /* |
||
55 | * Send fallback headers |
||
56 | */ |
||
57 | //header('Content-type: image/jpeg'); |
||
58 | header('ETag: ' . md5_file($cache)); |
||
59 | header("Last-Modified: $lastModified"); |
||
60 | // 1 day expires |
||
61 | header('Expires: ' . gmdate('D, d M Y H:i:s', ((60 * 60 * 24 * 1) + strtotime($lastModified)))); |
||
62 | } |
||
63 | |||
64 | if (!isset($res[$url]) || (is_bool($rewrite) && $rewrite)) { |
||
65 | self::$api->set_method('get'); |
||
66 | self::$api->exec(); |
||
67 | for ($i = 0; $i < 2; ++$i) { |
||
68 | if (isset(self::$api->responseHeaders['Location'])) { |
||
69 | self::$api->get(self::$api->responseHeaders['Location']); |
||
70 | } else { |
||
71 | break; |
||
72 | } |
||
73 | } |
||
74 | $res[$url] = self::$api->url; |
||
75 | write_file($saved, $res, true); |
||
76 | write_file($cache, self::$api->response, true); |
||
77 | header('Content-Type: ' . self::$api->responseHeaders['Content-Type'], true); |
||
78 | echo self::$api->response; |
||
79 | } elseif (file_exists($cache)) { |
||
80 | header('Content-Type: ' . mime_content_type($cache), true); |
||
81 | $read = read_file($cache); |
||
82 | if (!empty($read)) { |
||
83 | echo $read; |
||
84 | } else { |
||
85 | return self::{__FUNCTION__}($url, true); |
||
86 | } |
||
87 | } else { |
||
88 | self::$api->set_url($res[$url])->set_method('get')->exec(); |
||
89 | self::writeCache(); |
||
90 | } |
||
227 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.