Passed
Push — master ( 0272f5...3a0c5f )
by Aleksandr
28:04 queued 25:53
created

Connection   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 24
ccs 9
cts 11
cp 0.8182
rs 10
c 3
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 10 3
A reset() 0 3 1
A getNativeConnection() 0 3 1
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
21 1
    public static function reset() : void
22
    {
23 1
        self::$conn = null;
24
    }
25
26 3
    public static function get(array $params = null) : DBALConnection
27
    {
28 3
        if(!is_null($params)) {
29 2
            self::$conn = DriverManager::getConnection($params);
30 2
            return self::$conn;
31
        }
32 3
        if(!is_null(self::$conn)) {
33 1
            return self::$conn;
34
        }
35 2
        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
}