Failed Conditions
Push — master ( 656579...2742cd )
by Marco
11:55
created

SQLSrvConnection::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
ccs 0
cts 10
cp 0
cc 3
eloc 6
nc 3
nop 2
crap 12
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Driver\SQLSrv;
21
22
use Doctrine\DBAL\Driver\Connection;
23
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
24
25
/**
26
 * SQL Server implementation for the Connection interface.
27
 *
28
 * @since 2.3
29
 * @author Benjamin Eberlei <[email protected]>
30
 */
31
class SQLSrvConnection implements Connection, ServerInfoAwareConnection
32
{
33
    /**
34
     * @var resource
35
     */
36
    protected $conn;
37
38
    /**
39
     * @var \Doctrine\DBAL\Driver\SQLSrv\LastInsertId
40
     */
41
    protected $lastInsertId;
42
43
    /**
44
     * @param string $serverName
45
     * @param array  $connectionOptions
46
     *
47
     * @throws \Doctrine\DBAL\Driver\SQLSrv\SQLSrvException
48
     */
49
    public function __construct($serverName, $connectionOptions)
50
    {
51
        if ( ! sqlsrv_configure('WarningsReturnAsErrors', 0)) {
52
            throw SQLSrvException::fromSqlSrvErrors();
53
        }
54
55
        $this->conn = sqlsrv_connect($serverName, $connectionOptions);
0 ignored issues
show
Documentation Bug introduced by
It seems like sqlsrv_connect($serverName, $connectionOptions) can also be of type false. However, the property $conn is declared as type resource. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
56
        if ( ! $this->conn) {
57
            throw SQLSrvException::fromSqlSrvErrors();
58
        }
59
        $this->lastInsertId = new LastInsertId();
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getServerVersion()
66
    {
67
        $serverInfo = sqlsrv_server_info($this->conn);
68
69
        return $serverInfo['SQLServerVersion'];
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function requiresQueryForServerVersion()
76
    {
77
        return false;
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public function prepare($sql)
84
    {
85
        return new SQLSrvStatement($this->conn, $sql, $this->lastInsertId);
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91 View Code Duplication
    public function query()
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...
92
    {
93
        $args = func_get_args();
94
        $sql = $args[0];
95
        $stmt = $this->prepare($sql);
96
        $stmt->execute();
97
98
        return $stmt;
99
    }
100
101
    /**
102
     * {@inheritDoc}
103
     * @license New BSD, code from Zend Framework
104
     */
105 View Code Duplication
    public function quote($value, $type=\PDO::PARAM_STR)
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...
106
    {
107
        if (is_int($value)) {
108
            return $value;
109
        } elseif (is_float($value)) {
110
            return sprintf('%F', $value);
111
        }
112
113
        return "'" . str_replace("'", "''", $value) . "'";
114
    }
115
116
    /**
117
     * {@inheritDoc}
118
     */
119
    public function exec($statement)
120
    {
121
        $stmt = sqlsrv_query($this->conn, $statement);
122
123
        if (false === $stmt) {
124
            throw SQLSrvException::fromSqlSrvErrors();
125
        }
126
127
        return sqlsrv_rows_affected($stmt);
0 ignored issues
show
Bug Best Practice introduced by
The expression return sqlsrv_rows_affected($stmt) also could return the type false which is incompatible with the return type mandated by Doctrine\DBAL\Driver\Connection::exec() of integer.
Loading history...
128
    }
129
130
    /**
131
     * {@inheritDoc}
132
     */
133 View Code Duplication
    public function lastInsertId($name = null)
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...
134
    {
135
        if ($name !== null) {
136
            $stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?');
137
            $stmt->execute([$name]);
138
139
            return $stmt->fetchColumn();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $stmt->fetchColumn() also could return the type false which is incompatible with the return type mandated by Doctrine\DBAL\Driver\Connection::lastInsertId() of string.
Loading history...
140
        }
141
142
        return $this->lastInsertId->getId();
143
    }
144
145
    /**
146
     * {@inheritDoc}
147
     */
148
    public function beginTransaction()
149
    {
150
        if ( ! sqlsrv_begin_transaction($this->conn)) {
151
            throw SQLSrvException::fromSqlSrvErrors();
152
        }
153
    }
154
155
    /**
156
     * {@inheritDoc}
157
     */
158
    public function commit()
159
    {
160
        if ( ! sqlsrv_commit($this->conn)) {
161
            throw SQLSrvException::fromSqlSrvErrors();
162
        }
163
    }
164
165
    /**
166
     * {@inheritDoc}
167
     */
168
    public function rollBack()
169
    {
170
        if ( ! sqlsrv_rollback($this->conn)) {
171
            throw SQLSrvException::fromSqlSrvErrors();
172
        }
173
    }
174
175
    /**
176
     * {@inheritDoc}
177
     */
178 View Code Duplication
    public function errorCode()
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...
179
    {
180
        $errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
181
        if ($errors) {
182
            return $errors[0]['code'];
183
        }
184
185
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by Doctrine\DBAL\Driver\Connection::errorCode() of null|string.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
186
    }
187
188
    /**
189
     * {@inheritDoc}
190
     */
191
    public function errorInfo()
192
    {
193
        return sqlsrv_errors(SQLSRV_ERR_ERRORS);
194
    }
195
}
196