Passed
Branch dev (2c4856)
by
unknown
02:27
created

charInfo::onMessage()   D

Complexity

Conditions 14
Paths 21

Size

Total Lines 87
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 54
nc 21
nop 2
dl 0
loc 87
rs 4.9516
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
 * @property  message
30
 */
31
class charInfo
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 $discord;
35
    public $logger;
36
    private $excludeChannel;
37
    private $message;
38
39
    /**
40
     * @param $config
41
     * @param $discord
42
     * @param $logger
43
     */
44 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...
45
    {
46
        $this->config = $config;
47
        $this->discord = $discord;
48
        $this->logger = $logger;
49
        $this->excludeChannel = $this->config['bot']['restrictedChannels'];
50
    }
51
52
    /**
53
     * @param $msgData
54
     * @param $message
55
     * @return null
56
     */
57
    public function onMessage($msgData, $message)
58
    {
59
        $channelID = (int) $msgData['message']['channelID'];
60
61
        if (in_array($channelID, $this->excludeChannel, true))
62
        {
63
            return null;
64
        }
65
66
        $this->message = $message;
67
68
        $message = $msgData['message']['message'];
69
        $user = $msgData['message']['from'];
70
71
        $data = command($message, $this->information()['trigger'], $this->config['bot']['trigger']);
72
        if (isset($data['trigger'])) {
73
74
            // Most EVE players on Discord use their ingame name, so lets support @highlights
75
            $messageString = strstr($data['messageString'], '@') ? str_replace('<@', '', str_replace('>', '', $data['messageString'])) : $data['messageString'];
76
            if (is_numeric($messageString)) {
77
                // The person used @highlighting, so now we got a discord id, lets map that to a name
78
                $messageString = dbQueryField('SELECT name FROM usersSeen WHERE id = :id', 'name', array(':id' => $messageString));
79
            }
80
81
            $cleanString = urlencode($messageString);
82
            $characterID = urlencode(characterID($cleanString));
83
84
            if (empty($characterID)) {
85
                return $this->message->reply('**Error:** no data available');
86
            }
87
88
            //Get details
89
            $characterDetails = characterDetails($characterID);
90
            if (null === $characterDetails) {
91
                return $this->message->reply('**Error:** ESI is down. Try again later.');
92
            }
93
            $corporationID = $characterDetails['corporation_id'];
94
            $corporationName = corpName($corporationID);
95
            if (null === $corporationName) {
96
                return $this->message->reply('**Error:** ESI is down. Try again later.');
97
            }
98
            $corporationDetails = corpDetails($corporationID);
99
            $allianceID = $corporationDetails['alliance_id'];
100
            $allianceName = allianceName($allianceID);
101
            $characterName = $characterDetails['name'];
102
            $dateOfBirth = $characterDetails['birthday'];
103
104
            if ($characterName === null || $characterName === '') {
105
                return $this->message->reply('**Error:** No character found.');
106
            }
107
108
            //ZKill lookup
109
            $url = "https://zkillboard.com/api/orderDirection/desc/limit/1/no-items/characterID/{$characterID}/xml/";
110
            $xml = makeApiRequest($url);
111
            if (empty($xml)) {
112
                return $this->message->reply('**Error:** ZKill is down. Try again later.');
113
            }
114
            foreach ($xml->result->rowset->row as $kill) {
115
                $lastSeenSystemID = $kill->attributes()->solarSystemID;
116
                $lastSeenSystem = systemName($lastSeenSystemID);
117
                $lastSeenDate = $kill->attributes()->killTime;
118
            }
119
            foreach ($xml->result->rowset->row->rowset->row as $attacker) {
120
                if ($attacker->attributes()->characterID == $characterID) {
121
                    $lastSeenShipID = $attacker->attributes()->shipTypeID;
122
                    $lastSeenShip = apiTypeName($lastSeenShipID);
123
                }
124
            }
125
            $url = "https://zkillboard.com/character/{$characterID}/";
126
127
            $msg = "```Name: {$characterName}
128
DOB: {$dateOfBirth}
129
			
130
Corporation Name: {$corporationName}
131
Alliance Name: {$allianceName}
132
133
Last Seen In System: {$lastSeenSystem}
0 ignored issues
show
Bug introduced by
The variable $lastSeenSystem 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...
134
Last Seen Flying a: {$lastSeenShip}
0 ignored issues
show
Bug introduced by
The variable $lastSeenShip 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...
135
Last Seen On: {$lastSeenDate}```
0 ignored issues
show
Bug introduced by
The variable $lastSeenDate 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...
136
137
For more info, visit: $url";
138
139
            $this->logger->addInfo("charInfo: Sending character info to {$user}");
140
            $this->message->reply($msg);
141
        }
142
        return null;
143
    }
144
145
    /**
146
     * @return array
147
     */
148 View Code Duplication
    public function information()
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...
149
    {
150
        return array(
151
            'name' => 'char',
152
            'trigger' => array($this->config['bot']['trigger'] . 'char'),
153
            'information' => 'Returns basic EVE Online data about a character. To use simply type !char character_name'
154
        );
155
    }
156
157
}
158