Passed
Branch master (fe8497)
by
unknown
03:48
created

getKillmails::getKM()   F

Complexity

Conditions 18
Paths 769

Size

Total Lines 82
Code Lines 58

Duplication

Lines 12
Ratio 14.63 %

Importance

Changes 0
Metric Value
cc 18
eloc 58
nc 769
nop 0
dl 12
loc 82
rs 2.439
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

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
    }
72
73
74
    /**
75
     * @return array
76
     */
77
    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...
78
    {
79
        return array(
80
            "name" => "",
81
            "trigger" => array(),
82
            "information" => ""
83
        );
84
    }
85
86
    /**
87
     *
88
     */
89
    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...
90
    {
91
        $lastChecked = getPermCache("killmailCheck");
92
        if ($lastChecked <= time()) {
93
            //check if user is still using the old config
94
            if(is_null($this->groupConfig)){
95
                $this->logger->addError("Killmails: UPDATE YOUR CONFIG TO RECEIVE KILLMAILS");
96
                setPermCache("killmailCheck", time() + 900);
97
                return null;
98
            }
99
            $this->logger->addInfo("Killmails: Checking for new killmails.");
100
            $this->getKM();
101
            setPermCache("killmailCheck", time() + 900);
102
        }
103
104
    }
105
106
    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...
107
    {
108
        foreach($this->groupConfig as $kmGroup) {
109
            $lastMail = getPermCache("{$kmGroup["name"]}newestKillmailID");
110
            if (is_null($lastMail)){
111
                $lastMail = $kmGroup["startMail"];
112
            }
113 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...
114
                $url = "https://zkillboard.com/api/no-attackers/no-items/orderDirection/asc/afterKillID/{$lastMail}/corporationID/{$kmGroup["corpID"]}/";
115
            }
116 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...
117
                $url = "https://zkillboard.com/api/no-attackers/no-items/kills/orderDirection/asc/afterKillID/{$lastMail}/corporationID/{$kmGroup["corpID"]}/";
118
            }
119 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...
120
                $url = "https://zkillboard.com/api/no-attackers/no-items/orderDirection/asc/afterKillID/{$lastMail}/allianceID/{$kmGroup["allianceID"]}/";
121
            }
122 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...
123
                $url = "https://zkillboard.com/api/no-attackers/no-items/kills/orderDirection/asc/afterKillID/{$lastMail}/allianceID/{$kmGroup["allianceID"]}/";
124
            }
125
126
            if (!isset($url)) { // Make sure it's always set.
127
                $this->logger->addInfo("Killmails: ERROR - Ensure your config file is setup correctly for killmails.");
128
                return null;
129
            }
130
131
            $xml = json_decode(downloadData($url), true);
132
            $i = 0;
133
            $limit = $kmGroup["spamAmount"];
134
            if (isset($xml)) {
135
                foreach ($xml as $kill) {
136
                    if ($i < $limit) {
137
                        //if big kill isn't set, disable it
138
                        if (is_null($kmGroup["bigKill"])){
139
                            $kmGroup["bigKill"] = 99999999999999999999999999;
140
                        }
141
                        $killID = $kill['killID'];
142
                        //check if start id is greater than current id
143
                        if ($kmGroup["startMail"] > $killID) {
144
                            $killID = $kmGroup["startMail"];
145
                        }
146
                        $channelID = $kmGroup["channel"];
147
                        $solarSystemID = $kill['solarSystemID'];
148
                        $systemName = apiCharacterName($solarSystemID);
149
                        $killTime = $kill['killTime'];
150
                        $victimAllianceName = $kill['victim']['allianceName'];
151
                        $victimName = $kill['victim']['characterName'];
152
                        $victimCorpName = $kill['victim']['corporationName'];
153
                        $victimShipID = $kill['victim']['shipTypeID'];
154
                        $shipName = apiTypeName($victimShipID);
155
                        $rawValue = $kill['zkb']['totalValue'];
156
                        $totalValue = number_format($kill['zkb']['totalValue']);
157
                        // Check if it's a structure
158
                        if ($victimName != "") {
159
                            if ($rawValue >= $kmGroup["bigKill"]) {
160
                                $channelID = $kmGroup["bigKillChannel"];
161
                                $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}/";
162
                            } elseif ($rawValue <= $kmGroup["bigKill"]) {
163
                                $msg = "**{$killTime}**\n\n**{$shipName}** worth **{$totalValue} ISK** flown by **{$victimName}** of (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
164
                            }
165
                        } elseif ($victimName == "") {
166
                            $msg = "**{$killTime}**\n\n**{$shipName}** worth **{$totalValue} ISK** owned by (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
167
                        }
168
169
                        if (!isset($msg)) { // Make sure it's always set.
170
                            return null;
171
                        }
172
                        queueMessage($msg, $channelID, $this->guild);
173
                        setPermCache("{$kmGroup["name"]}newestKillmailID", $killID++);
174
175
                        $i++;
176
                    } else {
177
                        $updatedID = getPermCache("{$kmGroup["name"]}newestKillmailID");
178
                        $this->logger->addInfo("Killmails: Kill posting cap reached, newest kill id for {$kmGroup["name"]} is {$updatedID}");
179
                        break;
180
                    }
181
                }
182
            }
183
            $updatedID = getPermCache("{$kmGroup["name"]}newestKillmailID");
184
            $this->logger->addInfo("Killmails: All kills posted, newest kill id for {$kmGroup["name"]} is {$updatedID}");
185
            continue;
186
        }
187
    }
188
}
189