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.

SQlServer::disableConnection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 SQlServer 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
     * MSSQl Server DSN Structure: sqlsrv:Server={srv};Database={db}"
43
     * `$dsnArray[0] = {srv}`
44
     * `$dsnArray[1] = {db}`
45
     * Attributes = [2 - infinity]
46
     *
47
     * @param array $dsnArray
48
     * @return void
49
     */
50
    public function setDsn(array $dsnArray)
51
    {
52
        $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...
53
    }
54
55
    /**
56
     * Establishes connection
57
     *
58
     * @param string $username
59
     * @param string $password optional
60
     * @throws \EmmetBlue\Core\Exception\SQLException
61
     * @return void
62
     */
63
    public function connect(string $username, string $password="")
64
    {
65
        $server = $this->dsn[0];
66
        $database = $this->dsn[1];
67
68
        $this->loginData['username'] = $username;
69
        $this->loginData['password'] = $password;
70
71
        try
72
        {
73
            $this->connectionObject = new \PDO("sqlsrv:Server=$server;Database=$database;ConnectionPooling=0", $username, $password);
74
        }
75
        catch (\PDOException $e)
76
        {
77
            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...
78
        }
79
80
        $dsn = $this->dsn;
81
        unset($dsn[0], $dsn[1]);
82
        
83
        foreach ($dsn as $attribute)
0 ignored issues
show
Bug introduced by
The expression $dsn of type string is not traversable.
Loading history...
84
        {
85
            foreach ($attribute as $key=>$value)
86
            {
87
                $this->connectionObject->setAttribute($key, $value);
88
            }
89
        }
90
    }
91
92
    /**
93
     * Returns an instance a pdo intance of the connection object
94
     *
95
     * @return \PDO
96
     */
97
    public function getConnection() : \PDO
98
    {
99
        if ($this->connectionObject instanceof \PDO)
100
        {
101
            return $this->connectionObject;
102
        }
103
104
        $this->connect(
105
            $this->loginData['username'] ?? '',
106
            $this->loginData['password'] ?? ''
107
        );
108
109
        return $this->connectionObject;
110
    }
111
112
    /**
113
     * Closes connection
114
     */
115
    public function disableConnection()
116
    {
117
        $this->connectionObject = null;
118
    }
119
120
    /**
121
     * Sets attributes for the PDO object
122
     *
123
     * @param \PDO $attribute
124
     * @param \PDO $value
125
     */
126
    public function setAttribute(\PDO $attribute, \PDO $value)
127
    {
128
         $this->connectionObject->setAttribute($attribute, $value);
129
    }
130
}
131