| Conditions | 8 |
| Paths | 39 |
| Total Lines | 55 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 1 | 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 |
||
| 87 | function tick() |
||
| 88 | { |
||
| 89 | $discord = $this->discord; |
||
| 90 | |||
| 91 | $continue = false; |
||
| 92 | $data = array(); |
||
| 93 | // If last check + 60 seconds is larger or equal to the current time(), we run |
||
| 94 | if ($this->lastCheck <= time()) { |
||
| 95 | // Fetch the last 25 twitter replies and/or searches |
||
| 96 | try { |
||
| 97 | $data = $this->twitter->load(Twitter::ME_AND_FRIENDS, 5); |
||
| 98 | foreach ($data as $message) { |
||
| 99 | $text = (array) $message->text; |
||
| 100 | $createdAt = (array) $message->created_at; |
||
| 101 | $postedBy = (array) $message->user->name; |
||
| 102 | $screenName = (array) $message->user->screen_name; |
||
| 103 | $id = (int) $message->id; |
||
| 104 | $this->lastID = getPermCache("twitterLatestID"); // get the last posted ID |
||
| 105 | |||
| 106 | if ($id <= $this->lastID) { |
||
| 107 | continue; |
||
| 108 | } |
||
| 109 | |||
| 110 | $this->maxID = max($id, $this->maxID); |
||
| 111 | |||
| 112 | $url = "https://twitter.com/" . $screenName[0] . "/status/" . $id; |
||
| 113 | $message = array("message" => $text[0], "postedAt" => $createdAt[0], "postedBy" => $postedBy[0], "screenName" => $screenName[0], "url" => $url . $id[0]); |
||
| 114 | $msg = "**@" . $screenName[0] . "** (" . $message["postedBy"] . ") / " . htmlspecialchars_decode($message["message"]); |
||
| 115 | $messages[$id] = $msg; |
||
| 116 | |||
| 117 | $continue = true; |
||
| 118 | |||
| 119 | if (sizeof($data)) { |
||
| 120 | setPermCache("twitterLatestID", $this->maxID); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } catch (Exception $e) { |
||
| 124 | //$this->logger->err("Twitter Error: " . $e->getMessage()); // Don't show there was an error, it's most likely just a rate limit |
||
|
1 ignored issue
–
show
|
|||
| 125 | } |
||
| 126 | |||
| 127 | if ($continue == true) { |
||
| 128 | ksort($messages); |
||
| 129 | |||
| 130 | foreach ($messages as $id => $msg) { |
||
| 131 | // Send the tweets to the channel |
||
| 132 | $channelID = $this->channelID; |
||
| 133 | $guild = $discord->guilds->get('id', $this->guild); |
||
| 134 | $channel = $guild->channels->get('id', $channelID); |
||
| 135 | $channel->sendMessage($msg, false); |
||
| 136 | sleep(1); // Lets sleep for a second, so we don't rage spam |
||
| 137 | } |
||
| 138 | } |
||
| 139 | $this->lastCheck = time() + 60; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 171 |
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.