Test Setup Failed
Branch dev (952e73)
by
unknown
05:37
created

src/plugins/onTick/getKillmailsRedis.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 getKillmailsRedis
30
 */
31
class getKillmailsRedis
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)
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'];
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) {
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'];
98
                }
99
                $channelID = $this->config['plugins']['getKillmails']['bigKills']['bigKillChannel'];
100
                $killID = $kill['killmail']['killID'];
101
                $systemName = $kill['killmail']['solarSystem']['name'];
102
                $killTime = $kill['killmail']['killTime'];
103
                $victimAllianceName = @$kill['killmail']['victim']['alliance']['name'];
104
                $victimName = @$kill['killmail']['victim']['character']['name'];
105
                $victimCorpName = @$kill['killmail']['victim']['corporation']['name'];
106
                $shipName = $kill['killmail']['victim']['shipType']['name'];
107
                $totalValue = number_format($kill['zkb']['totalValue']);
108
                // Check if it's a structure
109 View Code Duplication
                if ($victimName !== '') {
110
                    $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}/";
111
                } else {
112
                    $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}/";
113
                }
114
                if (null === $msg) { // Make sure it's always set.
115
                    return null;
116
                }
117
                queueMessage($msg, $channelID, $this->guild);
118
                $newID = $killID++;
119
                setPermCache('bigKillNewestKillmailID', $newID);
120
                $this->logger->addInfo("Killmails: BIG KILLMAIL Mail {$killID} queued.");
121
                continue;
122
            }
123
124
            foreach ($this->groupConfig as $kmGroup) {
125
                $killID = getPermCache("{$kmGroup['name']}newestKillmailID");
126
127
                //check if start id is greater than current id and if it is set
128
                if ($kmGroup['startMail'] > $killID || null === $killID) {
129
                    $killID = $kmGroup['startMail'];
130
                }
131
132
                //setup variables
133
                $corpLoss = false;
134
                $allianceLoss = false;
135
                $corpKill = false;
136
                $allianceKill = false;
137
                $attackerCorpArray = array();
138
                $attackerAllianceArray = array();
139
140
                //Populate arrays with corp and alliance ids of attackers
141
                foreach ($kill['killmail']['attackers'] as $attacker) {
142
                    $attackerCorpArray[] = (int) @$attacker['corporation']['id'];
143
                    $attackerAllianceArray[] = (int) @$attacker['alliance']['id'];
144
                }
145
146
                //check if it was corp or alliance kill or lossmail
147
                if ((int) @$kill['killmail']['victim']['corporation']['id'] === $kmGroup['corpID'] && (int) $kmGroup['corpID'] !== 0) {
148
                    $corpLoss = true;
149
                } elseif ((int) @$kill['killmail']['victim']['alliance']['id'] === $kmGroup['allianceID'] && (int) $kmGroup['allianceID'] !== 0) {
150
                    $allianceLoss = true;
151
                } elseif (in_array((int) $kmGroup['corpID'], $attackerCorpArray) && (int) $kmGroup['corpID'] !== 0) {
152
                    $corpKill = true;
0 ignored issues
show
$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...
153
                } elseif (in_array((int) $kmGroup['allianceID'], $attackerAllianceArray) && (int) $kmGroup['allianceID'] !== 0) {
154
                    $allianceKill = true;
0 ignored issues
show
$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...
155
                } else {
156
                	//if it wasn't any of these continue in case there is another group to process
157
                    continue;
158
                }
159
160
                //Check if it's a lossmail and continue to next group if lossmails are turned off
161
                if (($corpLoss === true || $allianceLoss === true) && $kmGroup['lossMails'] === 'false') {
162
                    continue;
163
                }
164
165
                //if big kill isn't set, disable it (unless someone explodes with a lot of plex..)
166
                if (null === $kmGroup['bigKill']) {
167
                    $kmGroup['bigKill'] = 99999999999999999999999999;
168
                }
169
                
170
                //get the kill values
171
                $killID = $kill['killmail']['killID'];
172
                $channelID = $kmGroup['channel'];
173
                $systemName = $kill['killmail']['solarSystem']['name'];
174
                $killTime = $kill['killmail']['killTime'];
175
                $victimAllianceName = @$kill['killmail']['victim']['alliance']['name'];
176
                $victimName = @$kill['killmail']['victim']['character']['name'];
177
                $victimCorpName = @$kill['killmail']['victim']['corporation']['name'];
178
                $shipName = @$kill['killmail']['victim']['shipType']['name'];
179
                $rawValue = $kill['zkb']['totalValue'];
180
                //Check if killmail is older than 2 days
181
                if ((time() - (60 * 60 * 48)) > DateTime::createFromFormat('Y.m.d H:i:s', $killTime)->getTimestamp()) {
182
                    continue;
183
                }
184
                //Check if killmail meets minimum value
185
                if (isset($kmGroup['minimumValue'])) {
186
                    if ($rawValue < $kmGroup['minimumValue']) {
187
                        $this->logger->addInfo("Killmails: Mail {$killID} ignored for not meeting the minimum value required.");
188
                        setPermCache("{$kmGroup['name']}newestKillmailID", $killID);
189
                        continue;
190
                    }
191
                }
192
                $totalValue = number_format($kill['zkb']['totalValue']);
193
                // Check if it's a structure
194 View Code Duplication
                if ($victimName !== '') {
0 ignored issues
show
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...
195
                    if ($kmGroup['bigKill'] != null && $rawValue >= $kmGroup['bigKill']) {
196
                        $channelID = $kmGroup['bigKillChannel'];
197
                        $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}/";
198
                    } elseif ($kmGroup['bigKill'] == null || $rawValue <= $kmGroup['bigKill']) {
199
                        $msg = "**{$killTime}**\n\n**{$shipName}** worth **{$totalValue} ISK** flown by **{$victimName}** of (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
200
                    }
201
                } elseif ($victimName === '') {
202
                    $msg = "**{$killTime}**\n\n**{$shipName}** worth **{$totalValue} ISK** owned by (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
203
                }
204
205
                if (null === $msg) { // Make sure it's always set.
0 ignored issues
show
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...
206
                    return null;
207
                }
208
                queueMessage($msg, $channelID, $this->guild);
209
                $this->logger->addInfo("Killmails: Mail {$killID} queued.");
210
                setPermCache("{$kmGroup['name']}newestKillmailID", $killID);
211
                //don't process the same kill twice
212
                break;
213
            }
214
        }
215
    }
216
}
217