Conditions | 8 |
Paths | 39 |
Total Lines | 51 |
Code Lines | 31 |
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 |
||
63 | public function tick() |
||
64 | { |
||
65 | $continue = false; |
||
66 | $data = array(); |
||
67 | // If last check + 60 seconds is larger or equal to the current time(), we run |
||
68 | if ($this->lastCheck <= time()) { |
||
69 | // Fetch the last 25 twitter replies and/or searches |
||
70 | try { |
||
71 | $data = $this->twitter->load(Twitter::ME_AND_FRIENDS, 5); |
||
72 | foreach ($data as $message) { |
||
73 | $text = (array) $message->text; |
||
74 | $createdAt = (array) $message->created_at; |
||
75 | $postedBy = (array) $message->user->name; |
||
76 | $screenName = (array) $message->user->screen_name; |
||
77 | $id = (int) $message->id; |
||
78 | $this->lastID = getPermCache('twitterLatestID'); // get the last posted ID |
||
79 | |||
80 | if ($id <= $this->lastID) { |
||
81 | continue; |
||
82 | } |
||
83 | |||
84 | $this->maxID = max($id, $this->maxID); |
||
85 | |||
86 | $url = 'https://twitter.com/' . $screenName[0] . '/status/' . $id; |
||
87 | $message = array('message' => $text[0], 'postedAt' => $createdAt[0], 'postedBy' => $postedBy[0], 'screenName' => $screenName[0], 'url' => $url . $id[0]); |
||
88 | $msg = '**@' . $screenName[0] . '** (' . $message['postedBy'] . ') / ' . htmlspecialchars_decode($message['message']); |
||
89 | $messages[$id] = $msg; |
||
90 | |||
91 | $continue = true; |
||
92 | |||
93 | if (count($data)) { |
||
94 | setPermCache('twitterLatestID', $this->maxID); |
||
95 | } |
||
96 | } |
||
97 | } catch (Exception $e) { |
||
98 | //$this->logger->err("Twitter Error: " . $e->getMessage()); // Don't show there was an error, it's most likely just a rate limit |
||
99 | } |
||
100 | |||
101 | if ($continue === true) { |
||
102 | ksort($messages); |
||
103 | |||
104 | foreach ($messages as $id => $msg) { |
||
105 | // Send the tweets to the channel |
||
106 | $channelID = $this->channelID; |
||
107 | queueMessage($msg, $channelID, $this->guild); |
||
108 | $this->logger->addInfo('twitterOutput: Tweet queued.'); |
||
109 | } |
||
110 | } |
||
111 | $this->lastCheck = time() + 60; |
||
112 | } |
||
113 | } |
||
114 | |||
124 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.