Failed Conditions
Pull Request — develop (#3367)
by Benjamin
73:27 queued 70:28
created

Driver   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 80.77%

Importance

Changes 0
Metric Value
wmc 8
eloc 28
dl 0
loc 67
ccs 21
cts 26
cp 0.8077
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A _constructPdoDsn() 0 10 3
A connect() 0 28 4
1
<?php
2
3
namespace Doctrine\DBAL\Driver\PDOSqlite;
4
5
use Doctrine\DBAL\DBALException;
6
use Doctrine\DBAL\Driver\AbstractSQLiteDriver;
7
use Doctrine\DBAL\Driver\PDOConnection;
8
use Doctrine\DBAL\Platforms\SqlitePlatform;
9
use PDOException;
10
use function array_merge;
11
12
/**
13
 * The PDO Sqlite driver.
14
 */
15
class Driver extends AbstractSQLiteDriver
16
{
17
    /** @var mixed[] */
18
    protected $_userDefinedFunctions = [
19
        'sqrt' => ['callback' => [SqlitePlatform::class, 'udfSqrt'], 'numArgs' => 1],
20
        'mod'  => ['callback' => [SqlitePlatform::class, 'udfMod'], 'numArgs' => 2],
21
        'locate'  => ['callback' => [SqlitePlatform::class, 'udfLocate'], 'numArgs' => -1],
22
    ];
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 399
    public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
28
    {
29 399
        if (isset($driverOptions['userDefinedFunctions'])) {
30
            $this->_userDefinedFunctions = array_merge(
31
                $this->_userDefinedFunctions,
32
                $driverOptions['userDefinedFunctions']
33
            );
34
            unset($driverOptions['userDefinedFunctions']);
35
        }
36
37
        try {
38 399
            $connection = new PDOConnection(
39 399
                $this->_constructPdoDsn($params),
40 399
                $username,
41 399
                $password,
42 399
                $driverOptions
43
            );
44 1
        } catch (PDOException $ex) {
45
            throw DBALException::driverException($this, $ex);
46
        }
47
48 398
        $pdo = $connection->getWrappedConnection();
49
50 398
        foreach ($this->_userDefinedFunctions as $fn => $data) {
51 398
            $pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']);
0 ignored issues
show
Bug introduced by
The method sqliteCreateFunction() does not exist on PDO. ( Ignorable by Annotation )

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

51
            $pdo->/** @scrutinizer ignore-call */ 
52
                  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...
52
        }
53
54 398
        return $connection;
55
    }
56
57
    /**
58
     * Constructs the Sqlite PDO DSN.
59
     *
60
     * @param mixed[] $params
61
     *
62
     * @return string The DSN.
63
     */
64 399
    protected function _constructPdoDsn(array $params)
65
    {
66 399
        $dsn = 'sqlite:';
67 399
        if (isset($params['path'])) {
68 4
            $dsn .= $params['path'];
69 396
        } elseif (isset($params['memory'])) {
70 396
            $dsn .= ':memory:';
71
        }
72
73 399
        return $dsn;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 28
    public function getName()
80
    {
81 28
        return 'pdo_sqlite';
82
    }
83
}
84