Passed
Push — master ( 022b32...35c6c5 )
by Aleksandr
01:52
created

Connection::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
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;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Connection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Doctrine\DBAL\DriverManager;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\DriverManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Drobotik\Eav\Exception\ConnectionException;
15
use PDO;
16
17
class Connection
18
{
19
    protected static ?PDO $conn = null;
20
21 1
    public static function reset() : void
22
    {
23 1
        self::$conn = null;
24
    }
25
26
    /**
27
     * @throws ConnectionException
28
     */
29 3
    public static function get(?PDO $pdo = null) : PDO
30
    {
31 3
        if(self::$conn instanceof PDO) {
32 1
            return self::$conn;
33
        }
34 3
        if($pdo instanceof PDO) {
35 2
            self::$conn = $pdo;
36 2
            return self::$conn;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::conn returns the type null which is incompatible with the type-hinted return PDO.
Loading history...
37
        }
38 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 PDO. 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...
39
    }
40
41
    public static function getNativeConnection() : PDO
42
    {
43
        return self::$conn;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::conn could return the type null which is incompatible with the type-hinted return PDO. Consider adding an additional type-check to rule them out.
Loading history...
44
    }
45
46
}