auth::__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 Discord\Parts\Guild\Role;
8
use Discord\Parts\User\Member;
9
use Monolog\Logger;
10
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...
11
use Sovereign\Lib\cURL;
12
use Sovereign\Lib\Db;
13
use Sovereign\Lib\Permissions;
14
use Sovereign\Lib\ServerConfig;
15
use Sovereign\Lib\Settings;
16
use Sovereign\Lib\Users;
17
18
class auth extends \Threaded implements \Collectable
19
{
20
    /**
21
     * @var Message
22
     */
23
    private $message;
24
    /**
25
     * @var Discord
26
     */
27
    private $discord;
28
    /**
29
     * @var Logger
30
     */
31
    private $log;
32
    /**
33
     * @var array
34
     */
35
    private $channelConfig;
36
    /**
37
     * @var Config
38
     */
39
    private $config;
40
    /**
41
     * @var Db
42
     */
43
    private $db;
44
    /**
45
     * @var cURL
46
     */
47
    private $curl;
48
    /**
49
     * @var Settings
50
     */
51
    private $settings;
52
    /**
53
     * @var Permissions
54
     */
55
    private $permissions;
56
    /**
57
     * @var ServerConfig
58
     */
59
    private $serverConfig;
60
    /**
61
     * @var Users
62
     */
63
    private $users;
64
    /**
65
     * @var array
66
     */
67
    private $extras;
68
69 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...
70
    {
71
        $this->message = $message;
72
        $this->discord = $discord;
73
        $this->channelConfig = $channelConfig;
74
        $this->log = $log;
75
        $this->config = $config;
76
        $this->db = $db;
77
        $this->curl = $curl;
78
        $this->settings = $settings;
79
        $this->permissions = $permissions;
80
        $this->serverConfig = $serverConfig;
81
        $this->users = $users;
82
        $this->extras = $extras;
83
    }
84
85
    public function run()
86
    {
87
        $explode = explode(" ", $this->message->content);
88
        $authString = isset($explode[1]) ? $explode[1] : "";
89
90
        if ($this->message->getChannelAttribute()->is_private) {
91
            return $this->message->reply("**Error** You are trying to send your auth token in private. This won't work because i need the guild information, which i can only get if you post it in a public channel on the server you want to get authed on.");
92
        }
93
94
        $authData = $this->db->queryRow("SELECT * FROM authRegs WHERE authString = :authString AND active = 1", array(":authString" => $authString));
95
96
        if ($authData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $authData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
97
            $groups = json_decode($authData["groups"], true);
98
            $characterID = $authData["characterID"];
99
            /** @var Role $roles */
100
            $roles = $this->message->getFullChannelAttribute()->getGuildAttribute()->getRolesAttribute();
101
            /** @var Member $member */
102
            $member = $this->message->getFullChannelAttribute()->getGuildAttribute()->getMembersAttribute()->get("id", $this->message->author->id);
103
            $username = $this->message->author->username;
104
            $discordID = $this->message->author->id;
105
106
            // @todo Force ingame name
107
            $characterName = json_decode($this->curl->get("https://evedata.xyz/api/character/shortinformation/{$characterID}/"))->characterName;
0 ignored issues
show
Unused Code introduced by
$characterName 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...
108
            // Doesn't work yet, but it should be something like $member->nick($characterName);
109
            //$member->user->setAttribute("username", $characterName);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
110
111
            /** @var Role $role */
112
            foreach ($roles as $role) {
113
                $roleName = $role->name;
114
                if (in_array($roleName, $groups)) {
115
                    $member->addRole($role);
116
                }
117
            }
118
119
            // Save the member object, so all the roles are set
120
            $member->save();
121
122
            $this->db->execute("UPDATE authRegs SET discordID = :discordID, active = 0 WHERE authString = :authString", array(":discordID" => $discordID, ":authString" => $authString));
123
            $this->log->addInfo("{$username} authenticated in {$this->message->getChannelAttribute()->name} on {$this->message->getChannelAttribute()->getGuildAttribute()->name}");
124
            $this->message->reply("You have now been added to the following groups: " . implode(", ", $groups));
125
        } else {
126
            $this->message->reply("**Error** You are trying to authenticate with an already used (or not existing) auth token..");
127
        }
128
129
        // Mark this as garbage
130
        $this->isGarbage();
131
    }
132
}