Passed
Push — master ( 2f56fd...c7e98d )
by Anton
05:11 queued 01:55
created

DB::getLog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @package Framework\DB
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2016, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace {
11
12
	abstract class DB {
13
14
		private static $link = null, $last = null, $log = [], $time = 0;
15
16
		/**
17
		 * Connect to a database
18
		 */
19
20
		public static function connect(string $server, string $user, string $password, string $name) {
21
22
			# Establish connection
23
24
			// Ignore error to avoid warning when trying to connect to unavailable host
25
26
			if (false === ($link = @mysqli_connect($server, $user, $password))) throw new Exception\DBConnect;
27
28
			# Set encoding
29
30
			if (!mysqli_query($link, "SET character_set_client = 'utf8'")) throw new Exception\DBCharset;
31
32
			if (!mysqli_query($link, "SET character_set_results = 'utf8'")) throw new Exception\DBCharset;
33
34
			if (!mysqli_query($link, "SET collation_connection = 'utf8_general_ci'")) throw new Exception\DBCharset;
35
36
			# ------------------------
37
38
			self::$link = $link; self::name($name);
39
		}
40
41
		/**
42
		 * Select a database
43
		 */
44
45
		public static function name(string $name) {
46
47
			if (!mysqli_select_db(self::$link, $name)) throw new Exception\DBSelect;
48
		}
49
50
		/**
51
		 * Send a query
52
		 *
53
		 * @return DB\Result|false : the result object or false on failure
54
		 */
55
56
		public static function send(string $query) {
57
58
			if (null === self::$link) return (self::$last = false);
59
60
			$time = microtime(true); $result = mysqli_query(self::$link, $query); $time = (microtime(true) - $time);
61
62
			self::$last = new DB\Result(self::$link, $result, $query, $time);
63
64
			self::$log[] = self::$last; self::$time += $time;
65
66
			# ------------------------
67
68
			return self::$last;
69
		}
70
71
		/**
72
		 * Send a select query
73
		 *
74
		 * @param $table        a table name
75
		 * @param $selection    a string or an array where each value is a field name
76
		 * @param $condition    a string or an array where each key is a field name and each value is a field value
77
		 * @param $order        a string or an array where each key is a field name and each value is a sorting direction (ASC or DESC)
78
		 * @param $limit        a maximum number of rows to be selected
79
		 *
80
		 * @return DB\Result|false : the result object or false on failure
81
		 */
82
83
		public static function select(string $table, $selection, $condition = null, $order = null, int $limit = 0) {
0 ignored issues
show
Unused Code introduced by
The parameter $table is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $selection is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $condition is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $order is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $limit is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
85
			return self::send(new DB\Query\Select(...func_get_args()));
0 ignored issues
show
Bug introduced by
The call to Select::__construct() misses a required argument $selection.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
func_get_args() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
86
		}
87
88
		/**
89
		 * Send an insert query
90
		 *
91
		 * @param $table        a table name
92
		 * @param $set          an array where each key is a field name and each value is a field value, or an array of such arrays
93
		 * @param $multiple     tells that the set must be interpreted as a multi-dimensional array (for multi-row inserts)
94
		 * @param $ignore       tells to ignore insert errors, such as a duplicate-key error
95
		 *
96
		 * @return DB\Result|false : the result object or false on failure
97
		 */
98
99
		public static function insert(string $table, array $set, bool $multiple = false, bool $ignore = false) {
0 ignored issues
show
Unused Code introduced by
The parameter $table is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $set is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $multiple is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $ignore is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
101
			return self::send(new DB\Query\Insert(...func_get_args()));
0 ignored issues
show
Bug introduced by
The call to Insert::__construct() misses a required argument $set.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
func_get_args() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
102
		}
103
104
		/**
105
		 * Send an update query
106
		 *
107
		 * @param $table        a table name
108
		 * @param $set          an array where each key is a field name and each value is a field value
109
		 * @param $condition    a string or an array where each key is a field name and each value is a field value
110
		 *
111
		 * @return DB\Result|false : the result object or false on failure
112
		 */
113
114
		public static function update(string $table, array $set, $condition = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $table is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $set is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $condition is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
115
116
			return self::send(new DB\Query\Update(...func_get_args()));
0 ignored issues
show
Bug introduced by
The call to Update::__construct() misses a required argument $set.

This check looks for function calls that miss required arguments.

Loading history...
Documentation introduced by
func_get_args() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
117
		}
118
119
		/**
120
		 * Send a delete query
121
		 *
122
		 * @param $table        a table name
123
		 * @param $condition    a string or an array where each key is a field name and each value is a field value
124
		 *
125
		 * @return DB\Result|false : the result object or false on failure
126
		 */
127
128
		public static function delete(string $table, $condition = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $table is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $condition is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
129
130
			return self::send(new DB\Query\Delete(...func_get_args()));
0 ignored issues
show
Documentation introduced by
func_get_args() is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
131
		}
132
133
		/**
134
		 * Get the last result object
135
		 *
136
		 * @return DB\Result|false|null : the result object or false if the last query failed or null if there have been no queries sent
137
		 */
138
139
		public static function getLast() {
140
141
			return self::$last;
142
		}
143
144
		/**
145
		 * Get the array of all the result objects
146
		 */
147
148
		public static function getLog() : array {
149
150
			return self::$log;
151
		}
152
153
		/**
154
		 * Get the total time of all the queries sent
155
		 */
156
157
		public static function getTime() : string {
158
159
			return number_format(self::$time, 10);
160
		}
161
	}
162
}
163