Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 31 | class siphons { |
||
|
|
|||
| 32 | /** |
||
| 33 | * @var |
||
| 34 | */ |
||
| 35 | var $config; |
||
| 36 | /** |
||
| 37 | * @var |
||
| 38 | */ |
||
| 39 | var $discord; |
||
| 40 | /** |
||
| 41 | * @var |
||
| 42 | */ |
||
| 43 | var $logger; |
||
| 44 | /** |
||
| 45 | * @var |
||
| 46 | */ |
||
| 47 | var $toDiscordChannel; |
||
| 48 | public $guild; |
||
| 49 | protected $keyID; |
||
| 50 | protected $vCode; |
||
| 51 | protected $prefix; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param $config |
||
| 55 | * @param $discord |
||
| 56 | * @param $logger |
||
| 57 | */ |
||
| 58 | function init($config, $discord, $logger) |
||
| 59 | { |
||
| 60 | $this->config = $config; |
||
| 61 | $this->discord = $discord; |
||
| 62 | $this->logger = $logger; |
||
| 63 | $this->guild = $config["bot"]["guild"]; |
||
| 64 | $this->toDiscordChannel = $config["plugins"]["siphons"]["channelID"]; |
||
| 65 | $this->keyID = $config["plugins"]["siphons"]["keyID"]; |
||
| 66 | $this->vCode = $config["plugins"]["siphons"]["vCode"]; |
||
| 67 | $this->prefix = $config["plugins"]["siphons"]["prefix"]; |
||
| 68 | $lastCheck = getPermCache("siphonLastChecked{$this->keyID}"); |
||
| 69 | if ($lastCheck == NULL) { |
||
| 70 | // Schedule it for right now if first run |
||
| 71 | setPermCache("siphonLastChecked{$this->keyID}", time() - 5); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * |
||
| 77 | */ |
||
| 78 | function tick() |
||
| 79 | { |
||
| 80 | $lastChecked = getPermCache("siphonLastChecked{$this->keyID}"); |
||
| 81 | $keyID = $this->keyID; |
||
| 82 | $vCode = $this->vCode; |
||
| 83 | |||
| 84 | if ($lastChecked <= time()) { |
||
| 85 | $this->logger->addInfo("Checking API Key {$keyID} for siphons"); |
||
| 86 | $this->checkTowers($keyID, $vCode); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | function checkTowers($keyID, $vCode) |
||
| 91 | { |
||
| 92 | $discord = $this->discord; |
||
| 93 | |||
| 94 | $url = "https://api.eveonline.com/corp/AssetList.xml.aspx?keyID={$keyID}&vCode={$vCode}"; |
||
| 95 | $xml = makeApiRequest($url); |
||
| 96 | $siphonCount = 0; |
||
| 97 | foreach ($xml->result->rowset->row as $structures) { |
||
| 98 | //Check silos |
||
| 99 | View Code Duplication | if ($structures->attributes()->typeID == 14343) { |
|
| 100 | if (isset($structures->rowset->row)) { |
||
| 101 | foreach ($structures->rowset->row as $silo) { |
||
| 102 | //Avoid reporting empty silos |
||
| 103 | if ($silo->attributes()->quantity != 0) { |
||
| 104 | //Check for a multiple of 50 |
||
| 105 | if ($silo->attributes()->quantity % 50 != 0) { |
||
| 106 | $gooType = apiTypeName($silo->attributes()->typeID); |
||
| 107 | $systemName = apiCharacterName($structures->attributes()->locationID); |
||
| 108 | $msg = "{$this->prefix}"; |
||
| 109 | $msg .= "**POSSIBLE SIPHON**\n"; |
||
| 110 | $msg .= "**System: **{$systemName} has a possible siphon stealing {$gooType} from a silo.\n"; |
||
| 111 | // Send the msg to the channel; |
||
| 112 | $channelID = $this->toDiscordChannel; |
||
| 113 | $guild = $discord->guilds->get('id', $this->guild); |
||
| 114 | $channel = $guild->channels->get('id', $channelID); |
||
| 115 | $channel->sendMessage($msg, false); |
||
| 116 | $this->logger->addInfo($msg); |
||
| 117 | $siphonCount++; |
||
| 118 | sleep(2); // Lets sleep for a second, so we don't rage spam |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | View Code Duplication | if ($structures->attributes()->typeID == 17982) { |
|
| 125 | if (isset($structures->rowset->row)) { |
||
| 126 | foreach ($structures->rowset->row as $coupling) { |
||
| 127 | //Avoid reporting empty coupling arrays |
||
| 128 | if ($coupling->attributes()->quantity != 0) { |
||
| 129 | //Check for a multiple of 50 |
||
| 130 | if ($coupling->attributes()->quantity % 50 != 0) { |
||
| 131 | $gooType = apiTypeName($silo->attributes()->typeID); |
||
|
1 ignored issue
–
show
|
|||
| 132 | $systemName = apiCharacterName($structures->attributes()->locationID); |
||
| 133 | $msg = "{$this->prefix}"; |
||
| 134 | $msg .= "**POSSIBLE SIPHON**\n"; |
||
| 135 | $msg .= "**System: **{$systemName} has a possible siphon stealing {$gooType} from a coupling array.\n"; |
||
| 136 | // Send the msg to the channel; |
||
| 137 | $channelID = $this->toDiscordChannel; |
||
| 138 | $guild = $discord->guilds->get('id', $this->guild); |
||
| 139 | $channel = $guild->channels->get('id', $channelID); |
||
| 140 | $channel->sendMessage($msg, false); |
||
| 141 | $this->logger->addInfo($msg); |
||
| 142 | $siphonCount++; |
||
| 143 | sleep(2); // Lets sleep for a second, so we don't rage spam |
||
| 144 | } |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } |
||
| 150 | $cached = $xml->cachedUntil[0]; |
||
| 151 | $baseUnix = strtotime($cached); |
||
| 152 | $cacheClr = $baseUnix - 13500; |
||
| 153 | View Code Duplication | if ($cacheClr <= time()) { |
|
| 154 | $weirdTime = time() + 21700; |
||
| 155 | $cacheTimer = gmdate("Y-m-d H:i:s", $weirdTime); |
||
| 156 | setPermCache("siphonLastChecked{$keyID}", $weirdTime); |
||
| 157 | } else { |
||
| 158 | $cacheTimer = gmdate("Y-m-d H:i:s", $cacheClr); |
||
| 159 | setPermCache("siphonLastChecked{$keyID}", $cacheClr); |
||
| 160 | } |
||
| 161 | View Code Duplication | if ($siphonCount > 0) { |
|
| 162 | $msg = "Next Siphon Check At: {$cacheTimer} EVE Time"; |
||
| 163 | $channelID = $this->toDiscordChannel; |
||
| 164 | $guild = $discord->guilds->get('id', $this->guild); |
||
| 165 | $channel = $guild->channels->get('id', $channelID); |
||
| 166 | $channel->sendMessage($msg, false); |
||
| 167 | } |
||
| 168 | $this->logger->addInfo("Siphon Check Complete Next Check At {$cacheTimer}"); |
||
| 169 | return null; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * |
||
| 174 | */ |
||
| 175 | function onMessage() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @return array |
||
| 181 | */ |
||
| 182 | function information() |
||
| 190 | } |
||
| 191 |
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.