pc::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 15
Ratio 100 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 1
eloc 13
c 3
b 1
f 0
nc 1
nop 12
dl 15
loc 15
rs 9.4285

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Sovereign\Plugins\onMessage;
4
5
use Discord\Discord;
6
use Discord\Parts\Channel\Message;
7
use Monolog\Logger;
8
use SimpleXMLElement;
9
use Sovereign\Lib\Config;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Sovereign\Plugins\onMessage\Config.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use Sovereign\Lib\cURL;
11
use Sovereign\Lib\Db;
12
use Sovereign\Lib\Permissions;
13
use Sovereign\Lib\ServerConfig;
14
use Sovereign\Lib\Settings;
15
use Sovereign\Lib\Users;
16
17
class pc extends \Threaded implements \Collectable
18
{
19
    /**
20
     * @var Message
21
     */
22
    private $message;
23
    /**
24
     * @var Discord
25
     */
26
    private $discord;
27
    /**
28
     * @var Logger
29
     */
30
    private $log;
31
    /**
32
     * @var array
33
     */
34
    private $channelConfig;
35
    /**
36
     * @var Config
37
     */
38
    private $config;
39
    /**
40
     * @var Db
41
     */
42
    private $db;
43
    /**
44
     * @var cURL
45
     */
46
    private $curl;
47
    /**
48
     * @var Settings
49
     */
50
    private $settings;
51
    /**
52
     * @var Permissions
53
     */
54
    private $permissions;
55
    /**
56
     * @var ServerConfig
57
     */
58
    private $serverConfig;
59
    /**
60
     * @var Users
61
     */
62
    private $users;
63
    /**
64
     * @var array
65
     */
66
    private $extras;
67
68 View Code Duplication
    public function __construct($message, $discord, $channelConfig, $log, $config, $db, $curl, $settings, $permissions, $serverConfig, $users, $extras)
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...
69
    {
70
        $this->message = $message;
71
        $this->discord = $discord;
72
        $this->channelConfig = $channelConfig;
73
        $this->log = $log;
74
        $this->config = $config;
75
        $this->db = $db;
76
        $this->curl = $curl;
77
        $this->settings = $settings;
78
        $this->permissions = $permissions;
79
        $this->serverConfig = $serverConfig;
80
        $this->users = $users;
81
        $this->extras = $extras;
82
    }
83
84
    public function run()
85
    {
86
        $explode = explode(" ", $this->message->content);
87
        $prefix = $this->channelConfig->prefix;
88
        $system = isset($explode[0]) ? $explode[0] == "{$prefix}pc" ? "global" : str_replace($prefix, "", $explode[0]) : "global";
89
        unset($explode[0]);
90
        $item = implode(" ", $explode);
91
92
        // Stuff that doesn't need a db lookup
93
        $quickLookUps = [
94
            "plex" => array("typeName" => "30 Day Pilot's License Extension (PLEX)", "typeID" => 29668),
95
            "injector" => array("typeName" => "Skill Injector", "typeID" => 40520),
96
            "extractor" => array("typeName" => "Skill Extractor", "typeID" => 40519)
97
        ];
98
99
        if ($system && $item) {
100
            if (isset($quickLookUps[$item])) {
101
                $single = $quickLookUps[$item];
102
                $multiple = null;
103
            } else {
104
                $single = $this->db->queryRow("SELECT typeID, typeName FROM invTypes WHERE typeName = :item", array(":item" => $item));
105
                $multiple = $this->db->query("SELECT typeID, typeName FROM invTypes WHERE typeName LIKE :item LIMIT 5", array(":item" => $item));
106
            }
107
108
            if (count($multiple) == 1) {
109
                $single = $multiple[0];
110
            }
111
112
            if (empty($single) && !empty($multiple)) {
113
                $items = array();
114
                foreach ($multiple as $item) {
115
                    $items[] = $item["typeName"];
116
                }
117
                $items = implode(", ", $items);
118
                return $this->message->reply("**Multiple results found:** {$items}");
119
            }
120
121
            // If there is a single result, we'll get data now!
122
            if ($single) {
123
                $typeID = $single["typeID"];
124
                $typeName = $single["typeName"];
125
126
                if ($system == "global") {
127
                    $system = "global";
128
                    $data = new SimpleXMLElement($this->curl->get("https://api.eve-central.com/api/marketstat?typeid={$typeID}"));
129
                } else {
130
                    $solarSystemID = $this->db->queryField("SELECT solarSystemID FROM mapSolarSystems WHERE solarSystemName = :system", "solarSystemID", array(":system" => $system));
131
                    $data = new SimpleXMLElement($this->curl->get("https://api.eve-central.com/api/marketstat?usesystem={$solarSystemID}&typeid={$typeID}"));
132
                }
133
                $lowBuy = number_format((float)$data->marketstat->type->buy->min, 2);
134
                $avgBuy = number_format((float)$data->marketstat->type->buy->avg, 2);
135
                $highBuy = number_format((float)$data->marketstat->type->buy->max, 2);
136
                $lowSell = number_format((float)$data->marketstat->type->sell->min, 2);
137
                $avgSell = number_format((float)$data->marketstat->type->sell->avg, 2);
138
                $highSell = number_format((float)$data->marketstat->type->sell->max, 2);
139
                $solarSystemName = $system == "pc" ? "Global" : ucfirst($system);
140
                $messageData = "```
141
typeName: {$typeName}
142
solarSystemName: {$solarSystemName}
143
Buy:
144
  Low: {$lowBuy}
145
  Avg: {$avgBuy}
146
  High: {$highBuy}
147
Sell:
148
  Low: {$lowSell}
149
  Avg: {$avgSell}
150
  High: {$highSell}```";
151
                $this->message->reply($messageData);
152
            } else {
153
                $this->message->reply("**Error:** ***{$item}*** not found");
154
            }
155
        } else {
156
            $this->message->reply("**Error:** No itemName set..");
157
        }
158
159
        // Mark this as garbage
160
        $this->isGarbage();
161
    }
162
}