Failed Conditions
Pull Request — develop (#3523)
by
unknown
146:00 queued 80:53
created

Connection::quote()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 10
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Driver\PDOSqlsrv;
6
7
use Doctrine\DBAL\Driver\PDOConnection;
8
use Doctrine\DBAL\Driver\PDOSqlsrv\Statement as PDOSqlSrvStatement;
9
use Doctrine\DBAL\Driver\PDOStatement;
10
use Doctrine\DBAL\Driver\ResultStatement;
11
use Doctrine\DBAL\Driver\Statement;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Doctrine\DBAL\Driver\PDOSqlsrv\Statement. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
use function stripos;
13
use function strpos;
14
use function substr;
15
16
/**
17
 * Sqlsrv Connection implementation.
18
 */
19
class Connection extends PDOConnection
20
{
21
    /** @var LastInsertId|null */
22
    protected $lastInsertId;
23
24
    /**
25
     * Append to any INSERT query to retrieve the last insert id.
26
     */
27
    protected const LAST_INSERT_ID_SQL = ';SELECT SCOPE_IDENTITY() AS LastInsertId;';
28
29
    /**
30
     * {@inheritDoc}
31
     */
32
    public function lastInsertId($name = null)
33
    {
34
        if ($this->lastInsertId && $this->lastInsertId->getId()) {
35
            return $this->lastInsertId->getId();
36
        }
37
38
        if ($name === null) {
39
            return parent::lastInsertId();
40
        }
41
42
        $stmt = $this->prepare('SELECT CONVERT(VARCHAR(MAX), current_value) FROM sys.sequences WHERE name = ?');
43
        $stmt->execute([$name]);
44
45
        return (string) $stmt->fetchColumn();
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51
    public function quote(string $input) : string
52
    {
53
        $val = parent::quote($input);
54
55
        // Fix for a driver version terminating all values with null byte
56
        if (strpos($val, "\0") !== false) {
57
            $val = substr($val, 0, -1);
58
        }
59
60
        return $val;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function prepare(string $sql) : Statement
67
    {
68
        return parent::prepare($this->prepareSql($sql));
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function query(string $sql) : ResultStatement
75
    {
76
        return parent::query($this->prepareSql($sql));
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82
    protected function createStatement(\PDOStatement $stmt, string $sql) : PDOStatement
83
    {
84
        $this->lastInsertId = static::isInsertStatement($sql) ? new LastInsertId() : null;
85
86
        return new PDOSqlSrvStatement($stmt, $this->lastInsertId);
87
    }
88
89
    protected function prepareSql(string $sql) : string
90
    {
91
        if (static::isInsertStatement($sql)) {
92
            $sql .= self::LAST_INSERT_ID_SQL;
93
        }
94
95
        return $sql;
96
    }
97
98
    protected static function isInsertStatement(string $sql) : bool
99
    {
100
        return stripos($sql, 'INSERT INTO ') === 0;
101
    }
102
}
103