Passed
Branch master (d9babe)
by
unknown
02:45
created

getKillmails   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 223
Duplicated Lines 5.38 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 12
loc 223
rs 8.8
c 0
b 0
f 0
wmc 36
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A information() 0 8 1
A init() 0 10 1
B tick() 0 23 5
F getKM() 12 87 20
C getBigKM() 0 50 9

How to fix   Duplicated Code   

Duplicated Code

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
2
/**
3
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2016 Robert Sardinia
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
26
use discord\discord;
27
28
/**
29
 * Class getKillmails
30
 */
31
class getKillmails
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
32
{
33
    /**
34
     * @var
35
     */
36
    var $config;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $config.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
37
    /**
38
     * @var
39
     */
40
    var $db;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $db.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
41
    /**
42
     * @var
43
     */
44
    var $discord;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $discord.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
45
    /**
46
     * @var
47
     */
48
    var $channelConfig;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $channelConfig.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
49
    /**
50
     * @var int
51
     */
52
    var $lastCheck = 0;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $lastCheck.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
53
    /**
54
     * @var
55
     */
56
    var $logger;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $logger.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
57
    public $groupConfig;
58
59
    /**
60
     * @param $config
61
     * @param $discord
62
     * @param $logger
63
     */
64
    function init($config, $discord, $logger)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
65
    {
66
        $this->config = $config;
67
        $this->discord = $discord;
68
        $this->logger = $logger;
69
        $this->groupConfig = $config["plugins"]["getKillmails"]["groupConfig"];
70
        $this->guild = $config["bot"]["guild"];
0 ignored issues
show
Bug introduced by
The property guild does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
71
        //Refresh check at bot start
72
        setPermCache("killmailCheck", time() - 5);
73
    }
74
75
76
    /**
77
     * @return array
78
     */
79
    function information()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
80
    {
81
        return array(
82
            "name" => "",
83
            "trigger" => array(),
84
            "information" => ""
85
        );
86
    }
87
88
    /**
89
     *
90
     */
91
    function tick()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
92
    {
93
        // What was the servers last reported state
94
        $lastStatus = getPermCache("serverState");
95
        if ($lastStatus == "online") {
96
            $lastChecked = getPermCache("killmailCheck");
97
            if ($lastChecked <= time()) {
98
                //check if user is still using the old config
99
                if (is_null($this->groupConfig)) {
100
                    $this->logger->addError("Killmails: UPDATE YOUR CONFIG TO RECEIVE KILLMAILS");
101
                    setPermCache("killmailCheck", time() + 600);
102
                    return null;
103
                }
104
                $this->logger->addInfo("Killmails: Checking for new killmails.");
105
                $this->getKM();
106
                setPermCache("killmailCheck", time() + 600);
107
                if ($this->config["plugins"]["getKillmails"]["bigKills"]["shareBigKills"] == "true") {
108
                    $this->logger->addInfo("Killmails: Checking for 10b+ kills.");
109
                    $this->getBigKM();
110
                }
111
            }
112
        }
113
    }
114
115
    function getKM()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
116
    {
117
        foreach ($this->groupConfig as $kmGroup) {
118
            $killID = getPermCache("{$kmGroup["name"]}newestKillmailID");
119
            //check if start id is greater than current id and if it is set
120
            if ($kmGroup["startMail"] > $killID || is_null($killID)) {
121
                $killID = $kmGroup["startMail"];
122
            }
123 View Code Duplication
            if ($kmGroup["allianceID"] == "0" & $kmGroup["lossMails"] == 'true') {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: ($kmGroup['allianceID'] ...['lossMails'] == 'true', Probably Intended Meaning: $kmGroup['allianceID'] =...'lossMails'] == 'true')

When comparing the result of a bit operation, we suggest to add explicit parenthesis and not to rely on PHP’s built-in operator precedence to ensure the code behaves as intended and to make it more readable.

Let’s take a look at these examples:

// Returns always int(0).
return 0 === $foo & 4;
return (0 === $foo) & 4;

// More likely intended return: true/false
return 0 === ($foo & 4);
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
                $url = "https://zkillboard.com/api/no-attackers/no-items/orderDirection/asc/afterKillID/{$killID}/corporationID/{$kmGroup["corpID"]}/";
125
            }
126 View Code Duplication
            if ($kmGroup["allianceID"] == "0" & $kmGroup["lossMails"] == 'false') {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: ($kmGroup['allianceID'] ...'lossMails'] == 'false', Probably Intended Meaning: $kmGroup['allianceID'] =...lossMails'] == 'false')

When comparing the result of a bit operation, we suggest to add explicit parenthesis and not to rely on PHP’s built-in operator precedence to ensure the code behaves as intended and to make it more readable.

Let’s take a look at these examples:

// Returns always int(0).
return 0 === $foo & 4;
return (0 === $foo) & 4;

// More likely intended return: true/false
return 0 === ($foo & 4);
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127
                $url = "https://zkillboard.com/api/no-attackers/no-items/kills/orderDirection/asc/afterKillID/{$killID}/corporationID/{$kmGroup["corpID"]}/";
128
            }
129 View Code Duplication
            if ($kmGroup["allianceID"] != "0" & $kmGroup["lossMails"] == 'true') {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: ($kmGroup['allianceID'] ...['lossMails'] == 'true', Probably Intended Meaning: $kmGroup['allianceID'] !...'lossMails'] == 'true')

When comparing the result of a bit operation, we suggest to add explicit parenthesis and not to rely on PHP’s built-in operator precedence to ensure the code behaves as intended and to make it more readable.

Let’s take a look at these examples:

// Returns always int(0).
return 0 === $foo & 4;
return (0 === $foo) & 4;

// More likely intended return: true/false
return 0 === ($foo & 4);
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
                $url = "https://zkillboard.com/api/no-attackers/no-items/orderDirection/asc/afterKillID/{$killID}/allianceID/{$kmGroup["allianceID"]}/";
131
            }
132 View Code Duplication
            if ($kmGroup["allianceID"] != "0" & $kmGroup["lossMails"] == 'false') {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: ($kmGroup['allianceID'] ...'lossMails'] == 'false', Probably Intended Meaning: $kmGroup['allianceID'] !...lossMails'] == 'false')

When comparing the result of a bit operation, we suggest to add explicit parenthesis and not to rely on PHP’s built-in operator precedence to ensure the code behaves as intended and to make it more readable.

Let’s take a look at these examples:

// Returns always int(0).
return 0 === $foo & 4;
return (0 === $foo) & 4;

// More likely intended return: true/false
return 0 === ($foo & 4);
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
                $url = "https://zkillboard.com/api/no-attackers/no-items/kills/orderDirection/asc/afterKillID/{$killID}/allianceID/{$kmGroup["allianceID"]}/";
134
            }
135
136
            if (!isset($url)) { // Make sure it's always set.
137
                $this->logger->addInfo("Killmails: ERROR - Ensure your config file is setup correctly for killmails.");
138
                return null;
139
            }
140
141
            $kills = json_decode(downloadData($url), true);
142
            $i = 0;
143
            if (isset($kills)) {
144
                foreach ($kills as $kill) {
145
                    if ($i < 10) {
146
                        //if big kill isn't set, disable it
147
                        if (is_null($kmGroup["bigKill"])) {
148
                            $kmGroup["bigKill"] = 99999999999999999999999999;
149
                        }
150
                        $killID = $kill['killID'];
151
                        $channelID = $kmGroup["channel"];
152
                        $solarSystemID = $kill['solarSystemID'];
153
                        $systemName = apiCharacterName($solarSystemID);
154
                        $killTime = $kill['killTime'];
155
                        $victimAllianceName = $kill['victim']['allianceName'];
156
                        $victimName = $kill['victim']['characterName'];
157
                        $victimCorpName = $kill['victim']['corporationName'];
158
                        $victimShipID = $kill['victim']['shipTypeID'];
159
                        $shipName = apiTypeName($victimShipID);
160
                        $rawValue = $kill['zkb']['totalValue'];
161
                        //Check if killmail meets minimum value
162
                        if (isset($kmGroup["minimumValue"])) {
163
                            if ($rawValue < $kmGroup["minimumValue"]) {
164
                                $this->logger->addInfo("Killmails: Mail {$killID} ignored for not meeting the minimum value required.");
165
                                setPermCache("{$kmGroup["name"]}newestKillmailID", $killID);
166
                                continue;
167
                            }
168
                        }
169
                        $totalValue = number_format($kill['zkb']['totalValue']);
170
                        // Check if it's a structure
171
                        if ($victimName != "") {
172
                            if ($rawValue >= $kmGroup["bigKill"]) {
173
                                $channelID = $kmGroup["bigKillChannel"];
174
                                $msg = "@here \n :warning:***Expensive Killmail***:warning: \n **{$killTime}**\n\n**{$shipName}** worth **{$totalValue} ISK** flown by **{$victimName}** of (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
175
                            } elseif ($rawValue <= $kmGroup["bigKill"]) {
176
                                $msg = "**{$killTime}**\n\n**{$shipName}** worth **{$totalValue} ISK** flown by **{$victimName}** of (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
177
                            }
178
                        } elseif ($victimName == "") {
179
                            $msg = "**{$killTime}**\n\n**{$shipName}** worth **{$totalValue} ISK** owned by (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
180
                        }
181
182
                        if (!isset($msg)) { // Make sure it's always set.
183
                            return null;
184
                        }
185
                        queueMessage($msg, $channelID, $this->guild);
186
                        $this->logger->addInfo("Killmails: Mail {$killID} queued.");
187
188
                        $i++;
189
                    } else {
190
                        $updatedID = getPermCache("{$kmGroup["name"]}newestKillmailID");
191
                        $this->logger->addInfo("Killmails: Kill posting cap reached, newest kill id for {$kmGroup["name"]} is {$updatedID}");
192
                        break;
193
                    }
194
                }
195
                setPermCache("{$kmGroup["name"]}newestKillmailID", $killID);
196
            }
197
            $updatedID = getPermCache("{$kmGroup["name"]}newestKillmailID");
198
            $this->logger->addInfo("Killmails: All kills posted, newest kill id for {$kmGroup["name"]} is {$updatedID}");
199
            continue;
200
        }
201
    }
202
203
    function getBigKM()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
204
    {
205
        $killID = getPermCache("bigKillNewestKillmailID");
206
        if ($this->config["plugins"]["getKillmails"]["bigKills"]["bigKillStartID"] > $killID || is_null($killID)) {
207
            $killID = $this->config["plugins"]["getKillmails"]["bigKills"]["bigKillStartID"];
208
        }
209
210
        $url = "https://zkillboard.com/api/kills/iskValue/10000000000/afterKillID/{$killID}/";
211
212
        $kills = json_decode(downloadData($url), true);
213
        $i = 0;
214
        if (isset($kills)) {
215
            foreach ($kills as $kill) {
216
                if ($i < 5) {
217
                    $newestID = getPermCache("bigKillNewestKillmailID");
218
                    $killID = $kill['killID'];
219
                    $channelID = $this->config["plugins"]["getKillmails"]["bigKills"]["bigKillChannel"];
220
                    $solarSystemID = $kill['solarSystemID'];
221
                    $systemName = apiCharacterName($solarSystemID);
222
                    $killTime = $kill['killTime'];
223
                    $victimAllianceName = $kill['victim']['allianceName'];
224
                    $victimName = $kill['victim']['characterName'];
225
                    $victimCorpName = $kill['victim']['corporationName'];
226
                    $victimShipID = $kill['victim']['shipTypeID'];
227
                    $shipName = apiTypeName($victimShipID);
228
                    $totalValue = number_format($kill['zkb']['totalValue']);
229
                    if ($killID > $newestID) {
230
                        // Check if it's a structure
231
                        if ($victimName != "") {
232
                            $msg = "**10b+ Kill Reported**\n\n**{$killTime}**\n\n**{$shipName}** worth **{$totalValue} ISK** flown by **{$victimName}** of (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
233
                        } else {
234
                            $msg = "**10b+ Kill Reported**\n\n**{$killTime}**\n\n**{$shipName}** worth **{$totalValue} ISK** owned by (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
235
                        }
236
                        if (!isset($msg)) { // Make sure it's always set.
237
                            return null;
238
                        }
239
                        queueMessage($msg, $channelID, $this->guild);
240
                        $this->logger->addInfo("Killmails: Mail {$killID} queued.");
241
                        setPermCache("bigKillNewestKillmailID", $newID);
0 ignored issues
show
Bug introduced by
The variable $newID does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
242
                    }
243
                    $i++;
244
                } else {
245
                    $this->logger->addInfo("Killmails: bigKill posting cap reached, newest kill id is {$updatedID}");
0 ignored issues
show
Bug introduced by
The variable $updatedID seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
246
                    break;
247
                }
248
            }
249
        }
250
        $updatedID = getPermCache("bigKillNewestKillmailID");
251
        $this->logger->addInfo("Killmails: All bigKills posted, newest kill id is {$updatedID}");
252
    }
253
}
254