| Conditions | 11 |
| Paths | 76 |
| Total Lines | 44 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 57 | private function customWikiLinks(): void |
||
| 58 | { |
||
| 59 | /** @var Redis $redis */ |
||
| 60 | $redis = $this->di->getShared(ManagedCacheProvider::SERVICE_NAME); |
||
| 61 | $links = $redis->get(self::WIKI_LINKS_CACHE_KEY); |
||
| 62 | |||
| 63 | if ($links === null) { |
||
| 64 | $ttl = 86400; |
||
| 65 | $client = new GuzzleHttp\Client(); |
||
| 66 | $url = 'https://raw.githubusercontent.com/mikopbx/Core/master/src/Common/WikiLinks/' . $this->language . '.json'; |
||
| 67 | try { |
||
| 68 | $res = $client->request('GET', $url, ['timeout' => 5, 'connect_timeout' => 5, 'read_timeout' => 5]); |
||
| 69 | } catch (GuzzleHttp\Exception\GuzzleException $e) { |
||
| 70 | $res = null; |
||
| 71 | $ttl = 3600; |
||
| 72 | if ($e->getCode() !== 404) { |
||
| 73 | Util::sysLogMsg('WikiLinksController', 'Error access to raw.githubusercontent.com'); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | $links = null; |
||
| 77 | if ($res && $res->getStatusCode() === 200) { |
||
| 78 | try { |
||
| 79 | $links = json_decode($res->getBody(), true, 512, JSON_THROW_ON_ERROR); |
||
| 80 | } catch (Exception $e) { |
||
| 81 | $ttl = 3600; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | if (!is_array($links)) { |
||
| 85 | $links = []; |
||
| 86 | } |
||
| 87 | $redis->set(self::WIKI_LINKS_CACHE_KEY, $links, $ttl); |
||
| 88 | } |
||
| 89 | if (empty($links)) { |
||
| 90 | $filename = str_replace('LANG', $this->language, self::WIKI_LINKS); |
||
| 91 | if (file_exists($filename)) { |
||
| 92 | try { |
||
| 93 | $links = json_decode(file_get_contents($filename), true, 512, JSON_THROW_ON_ERROR); |
||
| 94 | } catch (\Exception $e) { |
||
| 95 | Util::sysLogMsg('WikiLinksController', $e->getMessage()); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | } |
||
| 99 | $this->view->success = true; |
||
| 100 | $this->view->message = $links; |
||
| 101 | } |
||
| 122 | } |