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