getKillmails::tick()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 5
Ratio 25 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 0
dl 5
loc 20
rs 9.2
c 0
b 0
f 0
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
    public $config;
34
    public $db;
35
    public $discord;
36
    public $channelConfig;
37
    public $lastCheck = 0;
38
    public $logger;
39
    public $groupConfig;
40
41
    /**
42
     * @param $config
43
     * @param $discord
44
     * @param $logger
45
     */
46 View Code Duplication
    public function init($config, $discord, $logger)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
47
    {
48
        $this->config = $config;
49
        $this->discord = $discord;
50
        $this->logger = $logger;
51
        $this->groupConfig = $config['plugins']['getKillmails']['groupConfig'];
52
        $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...
53
        //Refresh check at bot start
54
        setPermCache('killmailCheck', time() - 5);
55
    }
56
57
    /**
58
     *
59
     */
60
    public function tick()
61
    {
62
        // What was the servers last reported state
63
        $lastStatus = getPermCache('serverState');
64
        if ($lastStatus === 'online') {
65
            $lastChecked = getPermCache('killmailCheck');
66
            if ($lastChecked <= time()) {
67
                //check if user is still using the old config
68 View Code Duplication
                if (null === $this->groupConfig) {
0 ignored issues
show
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...
69
                    $this->logger->addError('Killmails: UPDATE YOUR CONFIG TO RECEIVE KILLMAILS');
70
                    setPermCache('killmailCheck', time() + 600);
71
                    return null;
72
                }
73
                $this->logger->addInfo('Killmails: Checking for new killmails.');
74
                $this->getKM();
75
                setPermCache('killmailCheck', time() + 60);
76
                $this->logger->addInfo('Killmails: Killmail check complete.');
77
            }
78
        }
79
    }
80
81
    private function getKM()
82
    {
83
        $i = 0;
84
        while ($i < 25) {
85
            $kill = zKillRedis();
86
            $i++;
87
88
            //Check if mail is null
89
            if (!$kill || !array_key_exists('killID', $kill)) {
90
                break;
91
            }
92
93
            //Check if mail is a big kill
94
            if (@$kill['zkb']['totalValue'] >= 10000000000 && @$this->config['plugins']['getKillmails']['bigKills']['shareBigKills'] === 'true') {
95
                $killID = getPermCache('bigKillNewestKillmailID');
96
                if ($this->config['plugins']['getKillmails']['bigKills']['bigKillStartID'] > $killID || null === $killID) {
97
                    $killID = $this->config['plugins']['getKillmails']['bigKills']['bigKillStartID'];
0 ignored issues
show
Unused Code introduced by
$killID is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
98
                }
99
                $channelID = $this->config['plugins']['getKillmails']['bigKills']['bigKillChannel'];
100
                $killID = $kill['package']['killmail']['killmail_id'];
101
                $systemID = $kill['package']['killmail']['solar_system_id'];
102
                $systemName = systemName($systemID);
103
                $killTime = $kill['package']['killmail']['killmail_time'];
104
                $victimAllianceID = @$kill['package']['killmail']['victim']['alliance_id'];
105
                if (isSet($victimAllianceID)){$victimAllianceName = allianceName($victimAllianceID);} else {$victimAllianceName = null;}
106
                $victimName = @$kill['package']['killmail']['victim']['character']['name'];
107
                $victimCorpID = @$kill['package']['killmail']['victim']['corporation_id'];
108
                if (isSet($victimCorpID)){$victimCorpName = corpName($victimCorpID);} else {$victimCorpName = null;}
109
                $shipName = $kill['package']['killmail']['victim']['shipType']['name'];
110
                $totalValue = number_format($kill['zkb']['totalValue']);
111
                // Check if it's a structure
112 View Code Duplication
                if ($victimName !== '') {
0 ignored issues
show
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...
113
                    $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}/";
114
                } else {
115
                    $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}/";
116
                }
117
                if (null === $msg) { // Make sure it's always set.
118
                    return null;
119
                }
120
                queueMessage($msg, $channelID, $this->guild);
121
                $newID = $killID++;
122
                setPermCache('bigKillNewestKillmailID', $newID);
123
                $this->logger->addInfo("Killmails: BIG KILLMAIL Mail {$killID} queued.");
124
                continue;
125
            }
126
127
            foreach ($this->groupConfig as $kmGroup) {
128
                $killID = getPermCache("{$kmGroup['name']}newestKillmailID");
129
130
                //check if start id is greater than current id and if it is set
131
                if ($kmGroup['startMail'] > $killID || null === $killID) {
132
                    $killID = $kmGroup['startMail'];
0 ignored issues
show
Unused Code introduced by
$killID is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
133
                }
134
135
                //setup variables
136
                $corpLoss = false;
137
                $allianceLoss = false;
138
                $corpKill = false;
0 ignored issues
show
Unused Code introduced by
$corpKill is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
139
                $allianceKill = false;
0 ignored issues
show
Unused Code introduced by
$allianceKill is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
140
                $attackerCorpArray = array();
141
                $attackerAllianceArray = array();
142
143
                //Populate arrays with corp and alliance ids of attackers
144
                foreach ($kill['killmail']['attackers'] as $attacker) {
145
                    $attackerCorpArray[] = (int) @$attacker['corporation']['id'];
146
                    $attackerAllianceArray[] = (int) @$attacker['alliance']['id'];
147
                }
148
149
                //check if it was corp or alliance kill or lossmail
150
                if ((int) @$kill['killmail']['victim']['corporation']['id'] === $kmGroup['corpID'] && (int) $kmGroup['corpID'] !== 0) {
151
                    $corpLoss = true;
152
                } elseif ((int) @$kill['killmail']['victim']['alliance']['id'] === $kmGroup['allianceID'] && (int) $kmGroup['allianceID'] !== 0) {
153
                    $allianceLoss = true;
154
                } elseif (in_array((int) $kmGroup['corpID'], $attackerCorpArray) && (int) $kmGroup['corpID'] !== 0) {
155
                    $corpKill = true;
0 ignored issues
show
Unused Code introduced by
$corpKill is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
156
                } elseif (in_array((int) $kmGroup['allianceID'], $attackerAllianceArray) && (int) $kmGroup['allianceID'] !== 0) {
157
                    $allianceKill = true;
0 ignored issues
show
Unused Code introduced by
$allianceKill is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
158
                } else {
159
                	//if it wasn't any of these continue in case there is another group to process
160
                    continue;
161
                }
162
163
                //Check if it's a lossmail and continue to next group if lossmails are turned off
164
                if (($corpLoss === true || $allianceLoss === true) && $kmGroup['lossMails'] === 'false') {
165
                    continue;
166
                }
167
168
                //if big kill isn't set, disable it (unless someone explodes with a lot of plex..)
169
                if (null === $kmGroup['bigKill']) {
170
                    $kmGroup['bigKill'] = 99999999999999999999999999;
171
                }
172
                
173
                //get the kill values
174
                $killID = $kill['killmail']['killID'];
175
                $channelID = $kmGroup['channel'];
176
                $systemName = $kill['killmail']['solarSystem']['name'];
177
                $killTime = $kill['killmail']['killTime'];
178
                $victimAllianceName = @$kill['killmail']['victim']['alliance']['name'];
179
                $victimName = @$kill['killmail']['victim']['character']['name'];
180
                $victimCorpName = @$kill['killmail']['victim']['corporation']['name'];
181
                $shipName = @$kill['killmail']['victim']['shipType']['name'];
182
                $rawValue = $kill['zkb']['totalValue'];
183
                //Check if killmail is older than 2 days
184
                if ((time() - (60 * 60 * 48)) > DateTime::createFromFormat('Y.m.d H:i:s', $killTime)->getTimestamp()) {
185
                    continue;
186
                }
187
                //Check if killmail meets minimum value
188
                if (isset($kmGroup['minimumValue'])) {
189
                    if ($rawValue < $kmGroup['minimumValue']) {
190
                        $this->logger->addInfo("Killmails: Mail {$killID} ignored for not meeting the minimum value required.");
191
                        setPermCache("{$kmGroup['name']}newestKillmailID", $killID);
192
                        continue;
193
                    }
194
                }
195
                $totalValue = number_format($kill['zkb']['totalValue']);
196
                // Check if it's a structure
197 View Code Duplication
                if ($victimName !== '') {
0 ignored issues
show
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...
198
                    if ($kmGroup['bigKill'] != null && $rawValue >= $kmGroup['bigKill']) {
199
                        $channelID = $kmGroup['bigKillChannel'];
200
                        $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}/";
201
                    } elseif ($kmGroup['bigKill'] == null || $rawValue <= $kmGroup['bigKill']) {
202
                        $msg = "**{$killTime}**\n\n**{$shipName}** worth **{$totalValue} ISK** flown by **{$victimName}** of (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
203
                    }
204
                } elseif ($victimName === '') {
205
                    $msg = "**{$killTime}**\n\n**{$shipName}** worth **{$totalValue} ISK** owned by (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
206
                }
207
208
                if (null === $msg) { // Make sure it's always set.
0 ignored issues
show
Bug introduced by
The variable $msg does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
209
                    return null;
210
                }
211
                queueMessage($msg, $channelID, $this->guild);
212
                $this->logger->addInfo("Killmails: Mail {$killID} queued.");
213
                setPermCache("{$kmGroup['name']}newestKillmailID", $killID);
214
                //don't process the same kill twice
215
                break;
216
            }
217
        }
218
    }
219
}
220