Passed
Branch dev (6ab41f)
by
unknown
05:41 queued 02:41
created

getKillmailsRedis   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 184
Duplicated Lines 23.37 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 43
loc 184
rs 8.2857
c 0
b 0
f 0
wmc 39
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 10 10 1
A tick() 5 20 4
F getKM() 28 133 34

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 getKillmailsRedis
30
 */
31
class getKillmailsRedis
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 (null === $kill['killID']) {
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 View Code Duplication
                if ($this->config['plugins']['getKillmails']['bigKills']['bigKillStartID'] > $killID || null === $killID) {
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...
97
                    $killID = $this->config['plugins']['getKillmails']['bigKills']['bigKillStartID'];
98
                }
99
                if ($killID > $kill['killmail']['killID']) {
100
                    continue;
101
                }
102
                $channelID = $this->config['plugins']['getKillmails']['bigKills']['bigKillChannel'];
103
                $killID = $kill['killmail']['killID'];
104
                $systemName = $kill['killmail']['solarSystem']['name'];
105
                $killTime = $kill['killmail']['killTime'];
106
                $victimAllianceName = @$kill['killmail']['victim']['alliance']['name'];
107
                $victimName = $kill['killmail']['victim']['character']['name'];
108
                $victimCorpName = @$kill['killmail']['victim']['corporation']['name'];
109
                $shipName = $kill['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 View Code Duplication
                if ($kmGroup['startMail'] > $killID || null === $killID) {
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...
132
                    $killID = $kmGroup['startMail'];
133
                }
134
135
                //Check if id's are in the kill
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
                foreach ($kill['killmail']['attackers'] as $attacker) {
144
                    $attackerCorpArray[] = (int)@$attacker['corporation']['id'];
145
                    $attackerAllianceArray[] = (int)@$attacker['alliance']['id'];
146
                }
147
148
                if ((int)@$kill['killmail']['victim']['corporation']['id'] === $kmGroup['corpID'] && (int)$kmGroup['corpID'] !== 0) {
149
                    $corpLoss = true;
150
                } elseif ((int)@$kill['killmail']['victim']['alliance']['id'] === $kmGroup['allianceID'] && (int)$kmGroup['allianceID'] !== 0) {
151
                    $allianceLoss = true;
152
                } elseif (in_array((int)$kmGroup['corpID'], $attackerCorpArray) && (int)$kmGroup['corpID'] !== 0) {
153
                    $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...
154
                } elseif (in_array((int)$kmGroup['allianceID'], $attackerAllianceArray) && (int)$kmGroup['allianceID'] !== 0) {
155
                    $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...
156
                } else {
157
                    break;
158
                }
159
160
                //Check if it's a lossmail and lossmails are turned off
161
                if (($corpLoss === true || $allianceLoss === true) && $kmGroup['lossMails'] === 'false') {
162
                    break;
163
                }
164
165
                //Check if it's an old kill
166
                if ($killID > $kill['killmail']['killID']) {
167
                    break;
168
                }
169
170
                //if big kill isn't set, disable it
171
                if (null === $kmGroup['bigKill']) {
172
                    $kmGroup['bigKill'] = 99999999999999999999999999;
173
                }
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 meets minimum value
184 View Code Duplication
                if (isset($kmGroup['minimumValue'])) {
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...
185
                    if ($rawValue < $kmGroup['minimumValue']) {
186
                        $this->logger->addInfo("Killmails: Mail {$killID} ignored for not meeting the minimum value required.");
187
                        setPermCache("{$kmGroup['name']}newestKillmailID", $killID);
188
                        continue;
189
                    }
190
                }
191
                $totalValue = number_format($kill['zkb']['totalValue']);
192
                // Check if it's a structure
193 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...
194
                    if ($rawValue >= $kmGroup['bigKill']) {
195
                        $channelID = $kmGroup['bigKillChannel'];
196
                        $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}/";
197
                    } elseif ($rawValue <= $kmGroup['bigKill']) {
198
                        $msg = "**{$killTime}**\n\n**{$shipName}** worth **{$totalValue} ISK** flown by **{$victimName}** of (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
199
                    }
200
                } elseif ($victimName === '') {
201
                    $msg = "**{$killTime}**\n\n**{$shipName}** worth **{$totalValue} ISK** owned by (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
202
                }
203
204
                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...
205
                    return null;
206
                }
207
                queueMessage($msg, $channelID, $this->guild);
208
                $this->logger->addInfo("Killmails: Mail {$killID} queued.");
209
                setPermCache("{$kmGroup['name']}newestKillmailID", $killID);
210
                break;
211
            }
212
        }
213
    }
214
}
215