Passed
Push — master ( feac44...0272f5 )
by Aleksandr
33:21
created

Connection::getNativeConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the eav package.
4
 * @author    Aleksandr Drobotik <[email protected]>
5
 * @copyright 2023 Aleksandr Drobotik
6
 * @license   https://opensource.org/license/mit  The MIT License
7
 */
8
declare(strict_types=1);
9
10
namespace Drobotik\Eav\Database;
11
12
use Doctrine\DBAL\Connection as DBALConnection;
13
use Doctrine\DBAL\DriverManager;
14
use Drobotik\Eav\Exception\ConnectionException;
15
use PDO;
16
17
class Connection
18
{
19
    protected static DBALConnection|null $conn = null;
20 1
21
    public static function reset() : void
22 1
    {
23
        self::$conn = null;
24
    }
25 3
26
    public static function get(array $params = null) : DBALConnection
27 3
    {
28 2
        if(!is_null($params)) {
29 2
            self::$conn = DriverManager::getConnection($params);
30
            return self::$conn;
31 3
        }
32 1
        if(!is_null(self::$conn)) {
33
            return self::$conn;
34 2
        }
35
        ConnectionException::undefined();
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Doctrine\DBAL\Connection. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
36
    }
37
38
    public static function getNativeConnection() : PDO
39
    {
40
        return Connection::$conn->getNativeConnection();
0 ignored issues
show
Bug introduced by
The method getNativeConnection() does not exist on null. ( Ignorable by Annotation )

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

40
        return Connection::$conn->/** @scrutinizer ignore-call */ getNativeConnection();

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...
Bug Best Practice introduced by
The expression return Drobotik\Eav\Data...->getNativeConnection() could return the type resource which is incompatible with the type-hinted return PDO. Consider adding an additional type-check to rule them out.
Loading history...
41
    }
42
43
}