help::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 2 Features 2
Metric Value
cc 1
eloc 14
c 5
b 2
f 2
nc 1
nop 12
dl 0
loc 16
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
namespace Sovereign\Plugins\onMessage;
3
4
use Discord\Discord;
5
use Discord\Parts\Channel\Message;
6
use Monolog\Logger;
7
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...
8
use Sovereign\Lib\cURL;
9
use Sovereign\Lib\Db;
10
use Sovereign\Lib\Permissions;
11
use Sovereign\Lib\ServerConfig;
12
use Sovereign\Lib\Settings;
13
use Sovereign\Lib\Users;
14
15
/**
16
 * Class cleverBotMessage
17
 * @package Sovereign
18
 */
19
class help extends \Threaded implements \Collectable
20
{
21
    /**
22
     * @var Message
23
     */
24
    private $message;
25
    /**
26
     * @var Discord
27
     */
28
    private $discord;
29
    /**
30
     * @var Logger
31
     */
32
    private $log;
33
    /**
34
     * @var array
35
     */
36
    private $channelConfig;
37
    /**
38
     * @var Config
39
     */
40
    private $config;
41
    /**
42
     * @var Db
43
     */
44
    private $db;
45
    /**
46
     * @var cURL
47
     */
48
    private $curl;
49
    /**
50
     * @var Settings
51
     */
52
    private $settings;
53
    /**
54
     * @var Permissions
55
     */
56
    private $permissions;
57
    /**
58
     * @var ServerConfig
59
     */
60
    private $serverConfig;
61
    /**
62
     * @var Users
63
     */
64
    private $users;
65
    /**
66
     * @var array
67
     */
68
    private $onMessagePlugins;
69
    private $onVoicePlugins;
70
71
    public function __construct($message, $discord, $channelConfig, $log, $config, $db, $curl, $settings, $permissions, $serverConfig, $users, $extras)
72
    {
73
        $this->message = $message;
74
        $this->discord = $discord;
75
        $this->channelConfig = $channelConfig;
76
        $this->log = $log;
77
        $this->config = $config;
78
        $this->db = $db;
79
        $this->curl = $curl;
80
        $this->settings = $settings;
81
        $this->permissions = $permissions;
82
        $this->serverConfig = $serverConfig;
83
        $this->users = $users;
84
        $this->onMessagePlugins = $extras["onMessagePlugins"];
85
        $this->onVoicePlugins = $extras["onVoicePlugins"];
86
    }
87
88
    /**
89
     *
90
     */
91
    public function run()
92
    {
93
        $explode = explode(" ", $this->message->content);
94
        $cmd = isset($explode[1]) ? $explode[1] : null;
95
        $plugins = (object)array_merge((array)$this->onMessagePlugins, (array)$this->onVoicePlugins);
96
        if (isset($cmd)) {
97
            foreach ($plugins as $command => $data) {
98
                if ($command == $cmd) {
99
                    if ($data["usage"])
100
                        $this->message->reply("**{$this->channelConfig->prefix}{$command}** _{$data["usage"]}_\r\n {$data["description"]}");
101
                    else
102
                        $this->message->reply("**{$this->channelConfig->prefix}{$command}** \r\n {$data["description"]}");
103
                }
104
            }
105
        } else {
106
            $msg = "**Commands:** \r\n";
107
            foreach ($plugins as $command => $data) {
108
                $msg .= "**{$this->channelConfig->prefix}{$command}** | ";
109
            }
110
111
            $this->message->reply($msg);
112
        }
113
    }
114
}