Passed
Push — main ( 676412...32f684 )
by Miaad
01:28
created

handler::__construct()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 37
rs 8.5706
cc 7
nc 9
nop 1
1
<?php
2
3
namespace BPT\database;
4
5
/**
6
 *
7
 */
8
class handler {
9
    /**
10
     * @const types database
11
     */
12
    const TYPES = ['Mysqli', 'Medoo', 'Json'];
13
    /**
14
     * @const Medoo database types
15
     */
16
    const Medoo_Types = ['mysql', 'mariadb', 'pgsql', 'sybase', 'oracle', 'mssql', 'sqlite'];
17
    /**
18
     * @var type database
0 ignored issues
show
Bug introduced by
The type BPT\database\type 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...
19
     */
20
    public static $type;
21
    /**
22
     * @var host for database
0 ignored issues
show
Bug introduced by
The type BPT\database\host 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...
23
     * Default value localhost
24
     */
25
    public static $host;
26
    /**
27
     * @var username database username
0 ignored issues
show
Bug introduced by
The type BPT\database\username 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...
28
     */
29
    public static $username;
30
    /**
31
     * @var dbname database name
0 ignored issues
show
Bug introduced by
The type BPT\database\dbname 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...
32
     */
33
    public static $dbname;
34
    /**
35
     * @var charset database
0 ignored issues
show
Bug introduced by
The type BPT\database\charset 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...
36
     */
37
    public static $charset;
38
    /**
39
     * @var password database password
0 ignored issues
show
Bug introduced by
The type BPT\database\password 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...
40
     */
41
    public static $password;
42
    /**
43
     * @var connection database
0 ignored issues
show
Bug introduced by
The type BPT\database\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...
44
     */
45
    public static $connect;
46
47
48
    /**
49
     *
50
     */
51
    public function __construct (array $settings) {
52
        if (in_array($settings['type'], self::TYPES)) {
53
            if ($settings['type'] === 'Mysqli') {
54
                if (self::CheckParam($settings)) {
55
                    $db = new \Mysqlidb([
0 ignored issues
show
Bug introduced by
The type Mysqlidb 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...
56
                        'host'     => self::$host,
57
                        'username' => self::$username,
58
                        'password' => self::$password,
59
                        'db'       => self::$dbname,
60
                        'charset'  => self::$charset
61
                    ]);
62
                    if ($db) {
0 ignored issues
show
introduced by
$db is of type Mysqlidb, thus it always evaluated to true.
Loading history...
63
                        self::$connect = $db;
64
                        print_r($db);
65
                    }
66
                    else {
67
                        print self::$username;
68
                        throw new \exception('a problem to connecting');
69
                    }
70
                }
71
                else {
72
                    throw new \exception('required parameters not found');
73
                }
74
            }
75
            if ($settings['type'] === 'Json') {
76
                if (isset($settings['dbname'])) {
77
                    self::$type = $settings['type'];
78
                    self::$dbname = $settings['dbname'];
79
                    (new database())->json_init();
80
                }
81
                else {
82
                    throw new \exception('parameter dbanme not found');
83
                }
84
            }
85
        }
86
        else {
87
            throw new \exception('parameter type not found');
88
        }
89
    }
90
91
    private static function CheckParam (array $array) {
92
        if (isset($array['username']) && isset($array['dbname']) && isset($array['password'])) {
93
            self::$host = $array['host'] ?? 'localhost';
0 ignored issues
show
Documentation Bug introduced by
It seems like $array['host'] ?? 'localhost' can also be of type string. However, the property $host is declared as type BPT\database\host. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
94
            self::$username = $array['username'];
95
            self::$dbname = $array['dbname'];
96
            self::$password = $array['password'];
97
            self::$charset = $array['charset'] ?? 'utf8';
0 ignored issues
show
Documentation Bug introduced by
It seems like $array['charset'] ?? 'utf8' can also be of type string. However, the property $charset is declared as type BPT\database\charset. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
98
            return true;
99
        }
100
        else {
101
            return false;
102
        }
103
    }
104
}