| Conditions | 9 |
| Paths | 6 |
| Total Lines | 65 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 36 | public function genPostsFile(): void |
||
| 37 | { |
||
| 38 | $settings = $this->app->get('settings'); |
||
| 39 | $result = ''; |
||
| 40 | |||
| 41 | // Hive API communication init |
||
| 42 | $apiConfig = [ |
||
| 43 | 'hiveNode' => $settings['api'], |
||
| 44 | 'debug' => false, |
||
| 45 | ]; |
||
| 46 | $api = new HiveCondenser($apiConfig); |
||
| 47 | |||
| 48 | // The file with the latest posts. |
||
| 49 | $file = $this->app->get('blogfile'); |
||
| 50 | |||
| 51 | // if the JSON file doesn't exist or if it's old, take it from API |
||
| 52 | if (!file_exists($file) || (time() - filemtime($file) > $settings['delay'])) { |
||
| 53 | // Prepare API call according to displayed posts type |
||
| 54 | $displayType = $settings['displayType']['type']; |
||
| 55 | if ($displayType === 'author') { |
||
| 56 | if (preg_match('/(hive-\d{6})/i', $settings['author']) == 1) { |
||
| 57 | $result = json_encode( |
||
| 58 | $api->getDiscussionsByCreated( |
||
| 59 | $settings['author'] |
||
| 60 | ), |
||
| 61 | JSON_PRETTY_PRINT |
||
| 62 | ); |
||
| 63 | } else { |
||
| 64 | $dateNow = (new \DateTime())->format('Y-m-d\TH:i:s'); |
||
| 65 | $result = json_encode( |
||
| 66 | $api->getDiscussionsByAuthorBeforeDate( |
||
| 67 | $settings['author'], |
||
| 68 | '', |
||
| 69 | $dateNow, |
||
| 70 | 100 |
||
| 71 | ), |
||
| 72 | JSON_PRETTY_PRINT |
||
| 73 | ); |
||
| 74 | } |
||
| 75 | } elseif (($displayType === 'tag')) { |
||
| 76 | $displayTag = $settings['displayType']['tag']; |
||
| 77 | $taggedPosts = []; |
||
| 78 | $allPosts = json_encode( |
||
| 79 | $api->getDiscussionsByAuthorBeforeDate( |
||
| 80 | $settings['author'], |
||
| 81 | '', |
||
| 82 | '', |
||
| 83 | 100 |
||
| 84 | ) |
||
| 85 | ); |
||
| 86 | $allPosts = json_decode($allPosts, true); |
||
| 87 | foreach ($allPosts as &$post) { |
||
| 88 | $postMeta = json_decode($post['json_metadata'], true); |
||
| 89 | $postTags = $postMeta['tags']; |
||
| 90 | if (in_array($displayTag, $postTags)) { |
||
| 91 | $taggedPosts[] = $post; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | $result = json_encode($taggedPosts, JSON_PRETTY_PRINT); |
||
| 96 | unset($taggedPosts); |
||
| 97 | } elseif ($displayType === 'reblog') { |
||
| 98 | $result = json_encode($api->getDiscussionsByBlog($settings['author']), JSON_PRETTY_PRINT); |
||
| 99 | } |
||
| 100 | file_put_contents($file, $result); |
||
| 101 | } |
||
| 175 |