Failed Conditions
Push — use-fn-const-coverage-test ( d169ec )
by Michael
43:36 queued 10:27
created

SQLSrvConnection::requiresQueryForServerVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 3
cp 0.6667
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1.037
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
use Doctrine\DBAL\ParameterType;
25
26
/**
27
 * SQL Server implementation for the Connection interface.
28
 *
29
 * @since 2.3
30
 * @author Benjamin Eberlei <[email protected]>
31
 */
32
class SQLSrvConnection implements Connection, ServerInfoAwareConnection
33
{
34
    /**
35
     * @var resource
36
     */
37
    protected $conn;
38
39
    /**
40
     * @var \Doctrine\DBAL\Driver\SQLSrv\LastInsertId
41
     */
42
    protected $lastInsertId;
43
44
    /**
45
     * @param string $serverName
46
     * @param array  $connectionOptions
47
     *
48
     * @throws \Doctrine\DBAL\Driver\SQLSrv\SQLSrvException
49
     */
50 24
    public function __construct($serverName, $connectionOptions)
51
    {
52 24
        if ( ! sqlsrv_configure('WarningsReturnAsErrors', 0)) {
53
            throw SQLSrvException::fromSqlSrvErrors();
54
        }
55
56 24
        $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...
57 24
        if ( ! $this->conn) {
58 1
            throw SQLSrvException::fromSqlSrvErrors();
59
        }
60 24
        $this->lastInsertId = new LastInsertId();
61 24
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 23
    public function getServerVersion()
67
    {
68 23
        $serverInfo = sqlsrv_server_info($this->conn);
69
70 23
        return $serverInfo['SQLServerVersion'];
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 24
    public function requiresQueryForServerVersion()
77
    {
78 24
        return false;
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84 233
    public function prepare($sql)
85
    {
86 233
        return new SQLSrvStatement($this->conn, $sql, $this->lastInsertId);
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92 171
    public function query()
93
    {
94 171
        $args = func_get_args();
95 171
        $sql = $args[0];
96 171
        $stmt = $this->prepare($sql);
97 171
        $stmt->execute();
98
99 171
        return $stmt;
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     * @license New BSD, code from Zend Framework
105
     */
106 8
    public function quote($value, $type = ParameterType::STRING)
107
    {
108 8
        if (is_int($value)) {
109 2
            return $value;
110 7
        } elseif (is_float($value)) {
111
            return sprintf('%F', $value);
112
        }
113
114 7
        return "'" . str_replace("'", "''", $value) . "'";
115
    }
116
117
    /**
118
     * {@inheritDoc}
119
     */
120 154
    public function exec($statement)
121
    {
122 154
        $stmt = sqlsrv_query($this->conn, $statement);
123
124 154
        if (false === $stmt) {
125 100
            throw SQLSrvException::fromSqlSrvErrors();
126
        }
127
128 129
        return sqlsrv_rows_affected($stmt);
129
    }
130
131
    /**
132
     * {@inheritDoc}
133
     */
134 3
    public function lastInsertId($name = null)
135
    {
136 3
        if ($name !== null) {
137 1
            $stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?');
138 1
            $stmt->execute([$name]);
139
        } else {
140 2
            $stmt = $this->query('SELECT @@IDENTITY');
141
        }
142
143 3
        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...
144
    }
145
146
    /**
147
     * {@inheritDoc}
148
     */
149 15
    public function beginTransaction()
150
    {
151 15
        if ( ! sqlsrv_begin_transaction($this->conn)) {
152
            throw SQLSrvException::fromSqlSrvErrors();
153
        }
154 15
    }
155
156
    /**
157
     * {@inheritDoc}
158
     */
159 6
    public function commit()
160
    {
161 6
        if ( ! sqlsrv_commit($this->conn)) {
162
            throw SQLSrvException::fromSqlSrvErrors();
163
        }
164 6
    }
165
166
    /**
167
     * {@inheritDoc}
168
     */
169 10
    public function rollBack()
170
    {
171 10
        if ( ! sqlsrv_rollback($this->conn)) {
172
            throw SQLSrvException::fromSqlSrvErrors();
173
        }
174 10
    }
175
176
    /**
177
     * {@inheritDoc}
178
     */
179
    public function errorCode()
180
    {
181
        $errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
182
        if ($errors) {
183
            return $errors[0]['code'];
184
        }
185
186
        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...
187
    }
188
189
    /**
190
     * {@inheritDoc}
191
     */
192
    public function errorInfo()
193
    {
194
        return sqlsrv_errors(SQLSRV_ERR_ERRORS);
195
    }
196
}
197