GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

MySQL::connect()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 28

Duplication

Lines 28
Ratio 100 %

Importance

Changes 0
Metric Value
dl 28
loc 28
rs 9.472
c 0
b 0
f 0
cc 4
nc 4
nop 2
1
<?php declare(strict_types=1);
2
/**
3
 * @license MIT
4
 * @author Samuel Adeshina <[email protected]>
5
 *
6
 * This file is part of the EmmetBlue project, please read the license document
7
 * available in the root level of the project
8
 */
9
namespace EmmetBlue\Core\Connection\Adapters;
10
11
use EmmetBlue\Core\Connection\ConnectableInterface;
12
use EmmetBlue\Core\Exception\SQLException;
13
14
/**
15
 * class ConnectionAdapter.
16
 * Instantiates a new instance of ConnectableInterface
17
 *
18
 * @author Samuel Adeshina <[email protected]>
19
 *
20
 * @since v0.0.1 08/06/2016 14:20
21
 */
22 View Code Duplication
class MySQL implements ConnectableInterface
0 ignored issues
show
Duplication introduced by
This class 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...
23
{
24
    /**
25
     * @var string $dsn
26
     */
27
    protected $dsn;
28
29
    /**
30
     * @var \PDO $connectionObject
31
     */
32
    protected $connectionObject;
33
34
    /**
35
     * @var array loginData
36
     */
37
    private $loginData = [];
38
39
    /**
40
     * Setter for the DSN string {@see $dsn}
41
     *
42
     * @param array $dsnArray
43
     * @return void
44
     */
45
    public function setDsn(array $dsnArray)
46
    {
47
        $this->dsn = $dsnArray;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dsnArray of type array is incompatible with the declared type string of property $dsn.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
48
    }
49
50
    /**
51
     * Establishes connection
52
     *
53
     * @param string $username
54
     * @param string $password optional
55
     * @throws \EmmetBlue\Core\Exception\SQLException
56
     * @return void
57
     */
58
    public function connect(string $username, string $password="")
59
    {
60
        $host = $this->dsn[0];
61
        $database = $this->dsn[1];
62
63
        $this->loginData['username'] = $username;
64
        $this->loginData['password'] = $password;
65
66
        try
67
        {
68
            $this->connectionObject = new \PDO("mysql:host=$host;dbname=$database;", $username, $password);
69
        }
70
        catch (\PDOException $e)
71
        {
72
            throw new SQLException("Unable to connect to database", 400, $e);  
0 ignored issues
show
Unused Code introduced by
The call to SQLException::__construct() has too many arguments starting with $e.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
73
        }
74
75
        $dsn = $this->dsn;
76
        unset($dsn[0], $dsn[1]);
77
        
78
        foreach ($dsn as $attribute)
0 ignored issues
show
Bug introduced by
The expression $dsn of type string is not traversable.
Loading history...
79
        {
80
            foreach ($attribute as $key=>$value)
81
            {
82
                $this->connectionObject->setAttribute($key, $value);
83
            }
84
        }
85
    }
86
87
    /**
88
     * Returns an instance a pdo intance of the connection object
89
     *
90
     * @return \PDO
91
     */
92
    public function getConnection() : \PDO
93
    {
94
        if ($this->connectionObject instanceof \PDO)
95
        {
96
            return $this->connectionObject;
97
        }
98
99
        $this->connect(
100
            $this->loginData['username'] ?? '',
101
            $this->loginData['password'] ?? ''
102
        );
103
104
        return $this->connectionObject;
105
    }
106
107
    /**
108
     * Closes connection
109
     */
110
    public function disableConnection()
111
    {
112
        $this->connectionObject = null;
113
    }
114
115
    /**
116
     * Sets attributes for the PDO object
117
     *
118
     * @param \PDO $attribute
119
     * @param \PDO $value
120
     */
121
    public function setAttribute(\PDO $attribute, \PDO $value)
122
    {
123
         $this->connectionObject->setAttribute($attribute, $value);
124
    }
125
}
126