Completed
Pull Request — master (#3212)
by Sergei
49:12 queued 45:02
created

Driver::getName()   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

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
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\PDOSqlite;
21
22
use Doctrine\DBAL\DBALException;
23
use Doctrine\DBAL\Driver\AbstractSQLiteDriver;
24
use Doctrine\DBAL\Driver\PDOConnection;
25
use PDOException;
26
use function array_merge;
27
28
/**
29
 * The PDO Sqlite driver.
30
 *
31
 * @since 2.0
32
 */
33
class Driver extends AbstractSQLiteDriver
34
{
35
    /**
36
     * @var array
37
     */
38
    protected $_userDefinedFunctions = [
39
        'sqrt' => ['callback' => ['Doctrine\DBAL\Platforms\SqlitePlatform', 'udfSqrt'], 'numArgs' => 1],
40
        'mod'  => ['callback' => ['Doctrine\DBAL\Platforms\SqlitePlatform', 'udfMod'], 'numArgs' => 2],
41
        'locate'  => ['callback' => ['Doctrine\DBAL\Platforms\SqlitePlatform', 'udfLocate'], 'numArgs' => -1],
42
    ];
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 288
    public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
48
    {
49 288
        if (isset($driverOptions['userDefinedFunctions'])) {
50
            $this->_userDefinedFunctions = array_merge(
51
                $this->_userDefinedFunctions, $driverOptions['userDefinedFunctions']);
52
            unset($driverOptions['userDefinedFunctions']);
53
        }
54
55
        try {
56 288
            $pdo = new PDOConnection(
57 288
                $this->_constructPdoDsn($params),
58 288
                $username,
59 288
                $password,
60 288
                $driverOptions
61
            );
62
        } catch (PDOException $ex) {
63
            throw DBALException::driverException($this, $ex);
64
        }
65
66 288
        foreach ($this->_userDefinedFunctions as $fn => $data) {
67 288
            $pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']);
0 ignored issues
show
Bug introduced by
The method sqliteCreateFunction() does not exist on Doctrine\DBAL\Driver\PDOConnection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
            $pdo->/** @scrutinizer ignore-call */ 
68
                  sqliteCreateFunction($fn, $data['callback'], $data['numArgs']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
        }
69
70 288
        return $pdo;
71
    }
72
73
    /**
74
     * Constructs the Sqlite PDO DSN.
75
     *
76
     * @param array $params
77
     *
78
     * @return string The DSN.
79
     */
80 288
    protected function _constructPdoDsn(array $params)
81
    {
82 288
        $dsn = 'sqlite:';
83 288
        if (isset($params['path'])) {
84
            $dsn .= $params['path'];
85 288
        } elseif (isset($params['memory'])) {
86 288
            $dsn .= ':memory:';
87
        }
88
89 288
        return $dsn;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 18
    public function getName()
96
    {
97 18
        return 'pdo_sqlite';
98
    }
99
}
100