Users   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 19.05 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 7
Bugs 4 Features 2
Metric Value
c 7
b 4
f 2
dl 12
loc 63
rs 10
wmc 9
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C set() 12 27 7
A get() 0 4 1

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
namespace Sovereign\Lib;
4
5
6
/**
7
 * Class Users
8
 * @package Sovereign\Lib
9
 */
10
/**
11
 * Class Users
12
 * @package Sovereign\Lib
13
 */
14
class Users
15
{
16
    /**
17
     * @var Db
18
     */
19
    protected $db;
20
21
    /**
22
     * Users constructor.
23
     * @param Db $db
24
     */
25
    public function __construct(Db $db)
26
    {
27
        $this->db = $db;
28
    }
29
30
    /**
31
     * @param $userID
32
     * @param $name
33
     * @param $lastStatus
34
     * @param $game
35
     * @param $lastSeen
36
     * @param $lastSpoke
37
     * @param $lastWritten
38
     * @throws \Exception
39
     */
40
    public function set($userID, $name, $lastStatus, $game, $lastSeen, $lastSpoke, $lastWritten)
41
    {
42
        try {
43 View Code Duplication
            if (isset($lastStatus)) {
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...
44
                $this->db->execute("INSERT INTO users (discordID, nickName, lastStatus) VALUES (:discordID, :nickName, :lastStatus) ON DUPLICATE KEY UPDATE lastStatus = :lastStatus", [":discordID" => $userID, ":nickName" => $name, ":lastStatus" => $lastStatus]);
45
            }
46
47
            if (isset($game)) {
48
                $this->db->execute("INSERT INTO users (discordID, nickName, game) VALUES (:discordID, :nickName, :game) ON DUPLICATE KEY UPDATE game = :game", [":discordID" => $userID, ":nickName" => $name, ":game" => $game]);
49
                $this->db->execute("INSERT IGNORE INTO games (game) VALUES (:game)", array(":game" => $game));
50
            }
51
52 View Code Duplication
            if (isset($lastSeen)) {
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...
53
                $this->db->execute("INSERT INTO users (discordID, nickName, lastSeen) VALUES (:discordID, :nickName, :lastSeen) ON DUPLICATE KEY UPDATE lastSeen = :lastSeen", [":discordID" => $userID, ":nickName" => $name, ":lastSeen" => $lastSeen]);
54
            }
55
56 View Code Duplication
            if (isset($lastSpoke)) {
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...
57
                $this->db->execute("INSERT INTO users (discordID, nickName, lastSpoke) VALUES (:discordID, :nickName, :lastSpoke) ON DUPLICATE KEY UPDATE lastSpoke = :lastSpoke", [":discordID" => $userID, ":nickName" => $name, ":lastSpoke" => $lastSpoke]);
58
            }
59
60 View Code Duplication
            if (isset($lastWritten)) {
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...
61
                $this->db->execute("INSERT INTO users (discordID, nickName, lastWritten) VALUES (:discordID, :nickName, :lastWritten) ON DUPLICATE KEY UPDATE lastWritten = :lastWritten", [":discordID" => $userID, ":nickName" => $name, ":lastWritten" => $lastWritten]);
62
            }
63
        } catch (\Exception $e) {
64
            throw new \Exception("There was an error setting data for {$name}: {$e->getMessage()}");
65
        }
66
    }
67
68
    /**
69
     * @param $userID
70
     * @return array
71
     */
72
    public function get($userID)
73
    {
74
        return $this->db->queryRow("SELECT * FROM users WHERE userID = :userID", array(":userID" => $userID));
75
    }
76
}