Test Failed
Push — main ( a8a1f8...d105a1 )
by Rafael
11:47
created

DB::exec()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Copyright (C) 2022-2023  Rafael San José Tovar   <[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
 * (at your option) 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\Database;
20
21
use PDO;
22
23
/**
24
 * Class DB
25
 *
26
 * Esta clase proporciona acceso directo a la base de datos.
27
 *
28
 * @author  Rafael San José Tovar <[email protected]>
29
 * @version 2022.0721
30
 *
31
 */
32
class DB
33
{
34
    /**
35
     * Motor utilizado, puede ser MySql, MariaDB, PostgreSql o cualquier otro PDO
36
     *
37
     * @var Engine
38
     */
39
    public static $engine;
40
41
    public function __construct()
42
    {
43
        if (!isset(self::$engine)) {
44
            $config = [
45
                'dbName' => constant('FS_DB_NAME'),
46
                'dbUser' => constant('FS_DB_USER'),
47
                'dbPass' => constant('FS_DB_PASS'),
48
                'dbHost' => constant('FS_DB_HOST'),
49
                'dbPort' => constant('FS_DB_PORT'),
50
                'dbOptions' => [
51
                    // PDO::ATTR_EMULATE_PREPARES => false,
52
                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
53
                ],
54
            ];
55
56
            $engine = constant('FS_DB_TYPE');
57
            switch (mb_strtolower($engine)) {
58
                case 'mysql':
59
                    self::$engine = new MySql($config);
0 ignored issues
show
Bug introduced by
The type Alxarafe\Database\MySql 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...
60
                    break;
61
                case 'mariadb':
62
                    self::$engine = new MariaDb($config);
0 ignored issues
show
Bug introduced by
The type Alxarafe\Database\MariaDb 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...
63
                    break;
64
                case 'postgresql':
65
                    self::$engine = new PostgreSql($config);
0 ignored issues
show
Bug introduced by
The type Alxarafe\Database\PostgreSql 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...
66
                    break;
67
                default:
68
                    die('Unknown engine: ' . $engine);
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...
69
            }
70
        }
71
    }
72
73
    public static function connect()
74
    {
75
        return self::$engine->connect();
76
    }
77
78
    public static function disconnect()
79
    {
80
        return self::$engine->disconnect();
0 ignored issues
show
Bug introduced by
The method disconnect() does not exist on Alxarafe\Database\Engine. Did you maybe mean connect()? ( Ignorable by Annotation )

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

80
        return self::$engine->/** @scrutinizer ignore-call */ disconnect();

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...
81
    }
82
83
    public static function connected()
84
    {
85
        return self::$engine->connected();
0 ignored issues
show
Bug introduced by
The method connected() does not exist on Alxarafe\Database\Engine. Did you maybe mean connect()? ( Ignorable by Annotation )

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

85
        return self::$engine->/** @scrutinizer ignore-call */ connected();

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...
86
    }
87
88
    /**
89
     * Ejecuta una sentencia SQL retornando TRUE si ha tenido éxito
90
     *
91
     * @author  Rafael San José Tovar <[email protected]>
92
     * @version 2022.0721
93
     *
94
     * @param string $query
95
     * @param array  $vars
96
     *
97
     * @return bool
98
     */
99
    public static function exec(string $query, array $vars = []): bool
100
    {
101
        return self::$engine->exec($query, $vars);
0 ignored issues
show
Unused Code introduced by
The call to Alxarafe\Database\Engine::exec() has too many arguments starting with $vars. ( Ignorable by Annotation )

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

101
        return self::$engine->/** @scrutinizer ignore-call */ exec($query, $vars);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
102
    }
103
104
    /**
105
     * Ejecuta una sentencia SELECT, retornando false si hay error, o un array
106
     * con el resultado de la consulta.
107
     *
108
     * @author  Rafael San José Tovar <[email protected]>
109
     * @version 2022.0721
110
     *
111
     * @param string $query
112
     * @param array  $vars
113
     *
114
     * @return array|false
115
     */
116
    public static function select(string $query, array $vars = [])
117
    {
118
        return self::$engine->select($query, $vars);
0 ignored issues
show
Unused Code introduced by
The call to Alxarafe\Database\Engine::select() has too many arguments starting with $vars. ( Ignorable by Annotation )

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

118
        return self::$engine->/** @scrutinizer ignore-call */ select($query, $vars);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
119
    }
120
121
    public static function getErrors()
122
    {
123
        return self::$engine->getErrors();
0 ignored issues
show
Bug introduced by
The method getErrors() does not exist on Alxarafe\Database\Engine. ( Ignorable by Annotation )

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

123
        return self::$engine->/** @scrutinizer ignore-call */ getErrors();

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...
124
    }
125
126
    public static function beginTransaction()
127
    {
128
        return self::$engine->beginTransaction();
129
    }
130
131
    public static function commit()
132
    {
133
        return self::$engine->commit();
134
    }
135
136
    public static function close()
137
    {
138
        return self::$engine->close();
0 ignored issues
show
Bug introduced by
The method close() does not exist on Alxarafe\Database\Engine. ( Ignorable by Annotation )

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

138
        return self::$engine->/** @scrutinizer ignore-call */ close();

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...
139
    }
140
141
    public static function rollback()
142
    {
143
        return self::$engine->rollback();
144
    }
145
146
    public static function version()
147
    {
148
        return self::$engine->server_info();
0 ignored issues
show
Bug introduced by
The method server_info() does not exist on Alxarafe\Database\Engine. ( Ignorable by Annotation )

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

148
        return self::$engine->/** @scrutinizer ignore-call */ server_info();

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...
149
    }
150
}
151