| Conditions | 7 |
| Paths | 68 |
| Total Lines | 63 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 19 | ||
| 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 |
||
| 91 | public function getBodyHtml() |
||
| 92 | { |
||
| 93 | $settings = $this->getSettings(); |
||
| 94 | |||
| 95 | $searchQuery = $settings['query']; |
||
| 96 | $count = $settings['count']; |
||
| 97 | |||
| 98 | $token = Plugin::getInstance()->getOauth()->getToken(); |
||
| 99 | |||
| 100 | if ($token) { |
||
| 101 | if (!empty($searchQuery)) { |
||
| 102 | try { |
||
| 103 | $q = $searchQuery; |
||
| 104 | |||
| 105 | if (Plugin::getInstance()->getSettings()->searchWidgetExtraQuery) { |
||
| 106 | $q .= ' '.Plugin::getInstance()->getSettings()->searchWidgetExtraQuery; |
||
| 107 | } |
||
| 108 | |||
| 109 | $response = Plugin::getInstance()->getApi()->get('search/tweets', [ |
||
| 110 | 'q' => $q, |
||
| 111 | 'count' => $count, |
||
| 112 | 'tweet_mode' => 'extended' |
||
| 113 | ]); |
||
| 114 | |||
| 115 | $tweets = []; |
||
| 116 | |||
| 117 | foreach ($response['statuses'] as $tweetData) { |
||
| 118 | $tweet = new Tweet(); |
||
| 119 | Plugin::getInstance()->getApi()->populateTweetFromData($tweet, $tweetData); |
||
| 120 | array_push($tweets, $tweet); |
||
| 121 | } |
||
| 122 | |||
| 123 | $variables['tweets'] = $tweets; |
||
| 124 | |||
| 125 | Craft::$app->getView()->registerAssetBundle(SearchWidgetAsset::class); |
||
| 126 | Craft::$app->getView()->registerJs("new Craft.Twitter_SearchWidget('".$this->id."');"); |
||
| 127 | |||
| 128 | return Craft::$app->getView()->renderTemplate('twitter/_components/widgets/Search/body', $variables); |
||
| 129 | } catch (ClientException $e) { |
||
| 130 | $errorMsg = $e->getMessage(); |
||
| 131 | $data = Json::decodeIfJson($e->getResponse()->getBody()->getContents()); |
||
| 132 | |||
| 133 | if (isset($data['errors'][0]['message'])) { |
||
| 134 | $errorMsg = $data['errors'][0]['message']; |
||
| 135 | } |
||
| 136 | |||
| 137 | Craft::error('Couldn’t retrieve tweets: '.$e->getTraceAsString(), __METHOD__); |
||
| 138 | |||
| 139 | return Craft::$app->getView()->renderTemplate('twitter/_components/widgets/Search/_error', [ |
||
| 140 | 'errorMsg' => $errorMsg |
||
| 141 | ]); |
||
| 142 | } |
||
| 143 | } else { |
||
| 144 | $variables['infoMsg'] = Craft::t('twitter', 'Please enter a search query in the widget’s settings.'); |
||
| 145 | |||
| 146 | return Craft::$app->getView()->renderTemplate('twitter/_components/widgets/Search/_error', $variables); |
||
| 147 | } |
||
| 148 | } else { |
||
| 149 | $variables['infoMsg'] = Craft::t('twitter', 'Twitter is not configured, please check the <a href="{url}">plugin’s settings</a>.', [ |
||
| 150 | 'url' => UrlHelper::url('twitter/settings') |
||
| 151 | ]); |
||
| 152 | |||
| 153 | return Craft::$app->getView()->renderTemplate('twitter/_components/widgets/Search/_error', $variables); |
||
| 154 | } |
||
| 167 |