Conditions | 18 |
Paths | 26 |
Total Lines | 80 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
126 | public function TwitterFeed($username, $count = 5) |
||
127 | { |
||
128 | if (!$username) { |
||
129 | user_error("No username provided"); |
||
130 | } |
||
131 | |||
132 | Config::inst()->update(MyTwitterData::class, "username", $username); |
||
133 | |||
134 | //check settings are available |
||
135 | $requiredSettings = [ |
||
136 | "twitter_consumer_key", |
||
137 | "twitter_consumer_secret", |
||
138 | "titter_oauth_token", |
||
139 | "titter_oauth_token" |
||
140 | ]; |
||
141 | |||
142 | foreach ($requiredSettings as $setting) { |
||
143 | if (!Config::inst()->get(MyTwitter::class, $setting)) { |
||
144 | user_error(" you must set MyTwitter::$setting", E_USER_NOTICE); |
||
145 | return null; |
||
146 | } |
||
147 | } |
||
148 | |||
149 | require_once(Director::baseFolder().'/'.SS_SHARETHIS_DIR.'/third_party/twitter_oauth/TwitterOAuthConsumer.php'); |
||
150 | |||
151 | $connection = new TwitterOAuth( |
||
152 | Config::inst()->get(MyTwitter::class, "twitter_consumer_key"), |
||
153 | Config::inst()->get(MyTwitter::class, "twitter_consumer_secret"), |
||
154 | Config::inst()->get(MyTwitter::class, "titter_oauth_token"), |
||
155 | Config::inst()->get(MyTwitter::class, "titter_oauth_token_secret") |
||
156 | ); |
||
157 | |||
158 | $config = Config::inst()->get(MyTwitter::class, "twitter_config"); |
||
159 | $config['screen_name'] = $username; |
||
160 | $tweets = $connection->get('statuses/user_timeline', $config); |
||
161 | $tweetList = new ArrayList(); |
||
162 | |||
163 | if (count($tweets) > 0 && !isset($tweets->error)) { |
||
164 | $i = 0; |
||
165 | |||
166 | foreach ($tweets as $tweet) { |
||
167 | if (Config::inst()->get(MyTwitter::class, "favourites_only") && $tweet->favorite_count == 0) { |
||
168 | break; |
||
169 | } |
||
170 | |||
171 | if (Config::inst()->get(MyTwitter::class, "non_replies_only") && $tweet->in_reply_to_status_id) { |
||
172 | break; |
||
173 | } |
||
174 | |||
175 | if (Config::inst()->get(MyTwitter::class, "debug")) { |
||
176 | print_r($tweet); |
||
177 | } |
||
178 | |||
179 | if (++$i > $count) { |
||
180 | break; |
||
181 | } |
||
182 | |||
183 | $date = new DBDatetime(); |
||
184 | $date->setValue(strtotime($tweet->created_at)); |
||
185 | $text = htmlentities($tweet->text, ENT_NOQUOTES, $encoding = "UTF-8", $doubleEncode = false); |
||
186 | |||
187 | if (!empty($tweet->entities) && !empty($tweet->entities->urls)) { |
||
188 | foreach ($tweet->entities->urls as $url) { |
||
189 | if (!empty($url->url) && !empty($url->display_url)) { |
||
190 | $text = str_replace($url->url, '<a href="'.$url->url.'" class="external">'.$url->display_url.'</a>', $text); |
||
191 | } |
||
192 | } |
||
193 | } |
||
194 | |||
195 | $tweetList->push( |
||
196 | new ArrayData([ |
||
197 | 'ID' => $tweet->id_str, |
||
198 | 'Title' => $text, |
||
199 | 'Date' => $date |
||
200 | ]) |
||
201 | ); |
||
202 | } |
||
203 | } |
||
204 | |||
205 | return $tweetList; |
||
206 | } |
||
208 |