| Conditions | 9 |
| Paths | 36 |
| Total Lines | 62 |
| Code Lines | 34 |
| 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 |
||
| 89 | public static function url2cache(string $url) |
||
| 90 | { |
||
| 91 | if (!self::$api) { |
||
| 92 | self::$api = new \Extender\request('https://unsplash.it'); |
||
| 93 | } |
||
| 94 | /** |
||
| 95 | * @var string Anonymize url into md5 |
||
| 96 | * |
||
| 97 | * @todo Fix unecessary characters from url |
||
| 98 | */ |
||
| 99 | $md5 = md5($url); |
||
| 100 | /** |
||
| 101 | * @var string Save cache name generator. |
||
| 102 | * |
||
| 103 | * @return string domain or default |
||
| 104 | */ |
||
| 105 | $savedname = function () { |
||
| 106 | global $url; |
||
| 107 | $savedname = \MVC\helper::url2host($url); |
||
| 108 | if (!$savedname) { |
||
| 109 | $savedname = 'default'; |
||
| 110 | } |
||
| 111 | |||
| 112 | return $savedname; |
||
| 113 | }; |
||
| 114 | /** |
||
| 115 | * @var string Save location schema image |
||
| 116 | */ |
||
| 117 | $saved = __DIR__ . "/data/$savedname.json"; |
||
| 118 | self::$saved = $saved; |
||
| 119 | $res = read_file($saved, []); |
||
| 120 | if (is_string($res)) { |
||
| 121 | $res = json_decode($res, true); |
||
| 122 | } |
||
| 123 | if (isset($res[$md5])) { |
||
| 124 | self::$url = $res[$md5]; |
||
| 125 | } elseif (\MVC\helper::is_url($url)) { |
||
| 126 | $res[$md5] = self::$url = $url; |
||
| 127 | $saved = __DIR__ . "/data/$savedname.json"; |
||
| 128 | } |
||
| 129 | |||
| 130 | self::$api->setUrl(self::$url); |
||
| 131 | $url = self::$api->url; |
||
| 132 | $cache = ROOT . '/tmp/img/' . md5($url); |
||
| 133 | self::$cache = $cache; |
||
| 134 | |||
| 135 | if (!file_exists($cache)) { |
||
| 136 | self::$api->set_method('get'); |
||
| 137 | self::$api->exec(); |
||
| 138 | for ($i = 0; $i < 10; ++$i) { |
||
| 139 | if (isset(self::$api->responseHeaders['Location'])) { |
||
| 140 | self::$api->get(self::$api->responseHeaders['Location']); |
||
| 141 | } else { |
||
| 142 | break; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | write_file($saved, $res, true); |
||
| 147 | write_file($cache, self::$api->response, true); |
||
| 148 | } |
||
| 149 | |||
| 150 | return '/img/cache?hash=' . md5($url); |
||
| 151 | } |
||
| 214 |