Conditions | 9 |
Paths | 6 |
Total Lines | 59 |
Code Lines | 39 |
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 |
||
44 | public function genPostsFile() |
||
45 | { |
||
46 | $settings = $this->app->get('settings'); |
||
47 | |||
48 | // Hive API communication init |
||
49 | $apiConfig = [ |
||
50 | "hiveNode" => $settings['api'], |
||
51 | "debug" => false |
||
52 | ]; |
||
53 | $api = new HiveCondenser($apiConfig); |
||
54 | |||
55 | // The file with the latest posts. |
||
56 | $file = $this->app->get('blogfile'); |
||
57 | |||
58 | // if the JSON file doesn't exist or if it's old, take it from API |
||
59 | if (!file_exists($file) || (time() - filemtime($file) > 120)) { |
||
60 | // Prepare API call according to displayed posts type |
||
61 | $displayType = $settings['displayType']['type']; |
||
62 | if ($displayType === 'author') { |
||
63 | $dateNow = (new \DateTime())->format('Y-m-d\TH:i:s'); |
||
64 | $result = json_encode( |
||
65 | $api->getDiscussionsByAuthorBeforeDate( |
||
66 | $settings['author'], |
||
67 | "", |
||
68 | $dateNow, |
||
69 | 100 |
||
70 | ), |
||
71 | JSON_PRETTY_PRINT |
||
72 | ); |
||
73 | } elseif (($displayType === 'tag')) { |
||
74 | $displayTag = $settings['displayType']['tag']; |
||
75 | $taggedPosts = array(); |
||
76 | $allPosts = json_encode( |
||
77 | $api->getDiscussionsByAuthorBeforeDate( |
||
78 | $settings['author'], |
||
79 | "", |
||
80 | "", |
||
81 | 100 |
||
82 | ) |
||
83 | ); |
||
84 | $allPosts = json_decode($allPosts, true); |
||
85 | //print_r($allPosts); |
||
86 | foreach ($allPosts as &$post) { |
||
87 | //$postTags = json_encode($post['json_metadata'], JSON_PRETTY_PRINT); |
||
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 | } elseif (strpos($settings['author'], "hive-") === 0) { |
||
100 | $result = json_encode($api->getDiscussionsByCreated($settings['author']), JSON_PRETTY_PRINT); |
||
101 | } |
||
102 | file_put_contents($file, $result); |
||
|
|||
103 | } |
||
106 |