Passed
Push — main ( 7af4bb...131f20 )
by Rafael
58:13
created

DbTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 15
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A connectDb() 0 13 3
1
<?php
2
3
/* Copyright (C) 2024      Rafael San José      <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace Alxarafe\Base\Controller\Trait;
20
21
use Alxarafe\Base\Config;
22
use DoliModules\Install\Controller\InstallController;
23
24
trait DbTrait
25
{
26
    public static function connectDb(?stdClass $db=null): bool
0 ignored issues
show
Bug introduced by
The type Alxarafe\Base\Controller\Trait\stdClass was not found. Did you mean stdClass? If so, make sure to prefix the type with \.
Loading history...
27
    {
28
        if ($db === null) {
29
            return false;
30
        }
31
        $checkDatabase = Config::checkDatabaseConnection($db);
32
        if (!$checkDatabase) {
33
            dump([
34
                'db' => $db,
35
                'check' => $checkDatabase,
36
            ]);
37
            (new InstallController())->doIndex();
38
            die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return boolean. 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
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 32 is false. This is incompatible with the type-hinted return boolean. 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...
40
    }
41
42
}
43