Test Failed
Pull Request — master (#2765)
by Marco
04:16
created

SQLSrvConnection::exec()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 7
Ratio 31.82 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 7
loc 22
c 0
b 0
f 0
ccs 0
cts 15
cp 0
rs 8.9197
cc 4
eloc 11
nc 4
nop 1
crap 20
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);
56
        if ( ! $this->conn) {
57
            throw SQLSrvException::fromSqlSrvErrors();
58
        }
59
        $this->lastInsertId = new LastInsertId();
60
        $this->lastInsertId->setId('0');
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getServerVersion()
67
    {
68
        $serverInfo = sqlsrv_server_info($this->conn);
69
70
        return $serverInfo['SQLServerVersion'];
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function requiresQueryForServerVersion()
77
    {
78
        return false;
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84
    public function prepare($sql)
85
    {
86
        return new SQLSrvStatement($this->conn, $sql, $this->lastInsertId);
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92 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...
93
    {
94
        $args = func_get_args();
95
        $sql = $args[0];
96
        $stmt = $this->prepare($sql);
97
        $stmt->execute();
98
99
        return $stmt;
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     * @license New BSD, code from Zend Framework
105
     */
106 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...
107
    {
108
        if (is_int($value)) {
109
            return $value;
110
        } elseif (is_float($value)) {
111
            return sprintf('%F', $value);
112
        }
113
114
        return "'" . str_replace("'", "''", $value) . "'";
115
    }
116
117
    /**
118
     * {@inheritDoc}
119
     */
120
    public function exec($statement)
121
    {
122
        $stmt = sqlsrv_query($this->conn, $statement);
123
124
        if (false === $stmt) {
125
            throw SQLSrvException::fromSqlSrvErrors();
126
        }
127
128
        $affectedRowCount = sqlsrv_rows_affected($stmt);
129
130
        $stmt = sqlsrv_query($this->conn, 'SELECT @@IDENTITY');
131
132 View Code Duplication
        if (false !== $statement) {
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...
133
            sqlsrv_fetch($stmt);
134
135
            $lastInsertId = sqlsrv_get_field($stmt, 0) ?: '0';
136
137
            $this->lastInsertId->setId($lastInsertId);
138
        }
139
140
        return $affectedRowCount;
141
    }
142
143
    /**
144
     * {@inheritDoc}
145
     */
146
    public function lastInsertId($name = null)
147
    {
148
        if ($name !== null) {
149
            $stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?');
150
            $stmt->execute([$name]);
151
152
            return $stmt->fetchColumn();
0 ignored issues
show
Bug Compatibility introduced by
The expression $stmt->fetchColumn(); of type string|boolean adds the type boolean to the return on line 152 which is incompatible with the return type declared by the interface Doctrine\DBAL\Driver\Connection::lastInsertId of type string.
Loading history...
153
        }
154
155
        return $this->lastInsertId->getId();
156
    }
157
158
    /**
159
     * {@inheritDoc}
160
     */
161
    public function beginTransaction()
162
    {
163
        if ( ! sqlsrv_begin_transaction($this->conn)) {
164
            throw SQLSrvException::fromSqlSrvErrors();
165
        }
166
    }
167
168
    /**
169
     * {@inheritDoc}
170
     */
171
    public function commit()
172
    {
173
        if ( ! sqlsrv_commit($this->conn)) {
174
            throw SQLSrvException::fromSqlSrvErrors();
175
        }
176
    }
177
178
    /**
179
     * {@inheritDoc}
180
     */
181
    public function rollBack()
182
    {
183
        if ( ! sqlsrv_rollback($this->conn)) {
184
            throw SQLSrvException::fromSqlSrvErrors();
185
        }
186
    }
187
188
    /**
189
     * {@inheritDoc}
190
     */
191 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...
192
    {
193
        $errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
194
        if ($errors) {
195
            return $errors[0]['code'];
196
        }
197
198
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Doctrine\DBAL\Driver\Connection::errorCode of type string|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
199
    }
200
201
    /**
202
     * {@inheritDoc}
203
     */
204
    public function errorInfo()
205
    {
206
        return sqlsrv_errors(SQLSRV_ERR_ERRORS);
207
    }
208
}
209