Skip to content

Commit 20fb9ae

Browse files
committed
fixes and updates
1 parent 4149fbc commit 20fb9ae

File tree

4 files changed

+47
-211
lines changed

4 files changed

+47
-211
lines changed

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
],
1616
"config": {
1717
"platform": {
18-
"php": "7.4"
18+
"php": "8.2"
1919
}
2020
},
2121
"require": {
2222
"ext-mysqli": "*"
2323
},
2424
"require-dev": {
25-
"symfony/http-kernel": "^4.3"
25+
"symfony/http-kernel": "^5.4"
2626
},
2727

2828
"autoload": {

lib/MysqliManager/Connection.php

+22-88
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: polyanin
5-
* Date: 13.09.2018
6-
* Time: 17:07
7-
*/
8-
9-
namespace Aplab\Pst\Lib\MysqliManager;
10-
1+
<?php namespace Aplab\Pst\Lib\MysqliManager;
112

123
use Aplab\Pst\Lib\Tools;
134
use mysqli;
@@ -68,48 +59,37 @@ public function __construct(MysqliManager $manager, $name = MysqliManager::DEFAU
6859
$this->select_db($config->dbname);
6960
}
7061

71-
/**
72-
* @return mysqli_driver
73-
*/
74-
public function getDriver()
62+
public function getDriver(): mysqli_driver
7563
{
7664
$hash = spl_object_hash($this);
7765
return $this->{$hash}['driver'];
7866
}
7967

80-
/**
81-
* execute query
82-
*
83-
* @param string $sql
84-
* @param int $result_mode
85-
* @return Result
86-
*/
87-
public function query($sql, $result_mode = MYSQLI_STORE_RESULT)
68+
public function query(string $query, $result_mode = MYSQLI_STORE_RESULT): Result
8869
{
8970
if (MysqliManager::$debug) {
90-
$trace = debug_backtrace(null, 2);
71+
$trace = debug_backtrace(0, 2);
9172
$trace = array_pop($trace);
92-
$service_info = 'sql: ' . preg_replace('/\\s{2,}/', ' ', $sql);
73+
$service_info = 'sql: ' . preg_replace('/\\s{2,}/', ' ', $query);
9374
if ($trace) {
9475
$service_info = ' line: ' . $trace['line'] . ' ' . $service_info;
9576
$service_info = 'file: ' . $trace['file'] . $service_info;
9677
}
9778
var_dump($service_info);
9879
}
9980
try {
100-
$this->real_query($sql);
101-
/** @noinspection PhpMethodParametersCountMismatchInspection */
81+
$this->real_query($query);
10282
return new Result($this);
10383
} catch (mysqli_sql_exception $e) {
104-
/** @noinspection PhpUndefinedFieldInspection */
105-
$e->sql = preg_replace('/\\s{2,}/', ' ', $sql);
84+
/** @noinspection PhpDynamicFieldDeclarationInspection */
85+
$e->sql = preg_replace('/\\s{2,}/', ' ', $query);
10686
$trace = $e->getTrace();
10787
$function = __FUNCTION__;
10888
$class = get_class($this);
10989
$break = false;
11090
foreach ($trace as $data_item) {
11191
if ($break) {
112-
/** @noinspection PhpUndefinedFieldInspection */
92+
/** @noinspection PhpDynamicFieldDeclarationInspection */
11393
$e->called_from = $data_item;
11494
break;
11595
}
@@ -123,27 +103,16 @@ public function query($sql, $result_mode = MYSQLI_STORE_RESULT)
123103
}
124104
}
125105

126-
/**
127-
* mysql_real_escape_string wrapper shortcut
128-
*
129-
* @param string $string
130-
* @return string|false
131-
*/
132-
public function e($string)
106+
public function e(string $string): false|string
133107
{
134108
return $this->real_escape_string($string);
135109
}
136110

137111
/**
138112
* mysql_real_escape_string wrapper advanced and shortcut
139-
* It handles multi-dimensional arrays recursively.
140-
*
141-
* @param string|array $string
142-
* @param bool $quote
143-
* @param bool $double
144-
* @return false|string
113+
* It handles multidimensional arrays recursively.
145114
*/
146-
public function q($string, $quote = true, $double = true)
115+
public function q(array|string $string, bool $quote = true, bool $double = true): array|string
147116
{
148117
if (is_array($string)) {
149118
$db = $this;
@@ -164,11 +133,8 @@ public function q($string, $quote = true, $double = true)
164133
/**
165134
* Put values into backquotes
166135
* Processes multidimensional arrays recursively.
167-
*
168-
* @param string|array $value
169-
* @return array|string
170136
*/
171-
public function bq($value)
137+
public function bq(array|string $value): array|string
172138
{
173139
if (is_array($value)) {
174140
$db = $this;
@@ -182,11 +148,8 @@ public function bq($value)
182148

183149
/**
184150
* Returns a list of tables in the current database
185-
*
186-
* @param boolean $reload
187-
* @return array
188151
*/
189-
public function getListTables($reload = false)
152+
public function getListTables(bool $reload = false): array
190153
{
191154
$hash = spl_object_hash($this);
192155
if ($reload || (!isset($this->data['list_tables']))) {
@@ -197,11 +160,8 @@ public function getListTables($reload = false)
197160

198161
/**
199162
* Returns a list of table fields in the current database
200-
*
201-
* @param void
202-
* @return array
203163
*/
204-
public function listFields($table)
164+
public function listFields(string $table): array
205165
{
206166
$hash = spl_object_hash($this);
207167
if (!isset($this->data['list_fields'][$table])) {
@@ -214,23 +174,16 @@ public function listFields($table)
214174

215175
/**
216176
* Returns true if table exists into current database, false otherwise
217-
*
218-
* @param string $table
219-
* @param boolean $reload
220-
* @return boolean
221177
*/
222-
public function tableExists($table, $reload = false)
178+
public function tableExists(string $table, bool $reload = false): bool
223179
{
224180
return in_array($table, $this->getListTables($reload));
225181
}
226182

227183
/**
228184
* Returns true if table is empty, false otherwise
229-
*
230-
* @param string $table
231-
* @return boolean
232185
*/
233-
public function isEmpty($table)
186+
public function isEmpty(string $table): bool
234187
{
235188
/** @noinspection SqlNoDataSourceInspection */
236189
/** @noinspection SqlResolve */
@@ -239,14 +192,11 @@ public function isEmpty($table)
239192
}
240193

241194
/**
242-
* @param $table
243-
* @param bool|false $post_check
244195
* @throws Exception
245196
*/
246-
public function drop($table, $post_check = false)
197+
public function drop(string $table, bool $post_check = false)
247198
{
248199
/** @noinspection SqlNoDataSourceInspection */
249-
/** @noinspection SqlResolve */
250200
$this->query('DROP TABLE `' . $this->e($table) . '`');
251201
if ($post_check && $this->tableExists($table, true)) {
252202
$msg = 'Unable to drop table';
@@ -255,14 +205,11 @@ public function drop($table, $post_check = false)
255205
}
256206

257207
/**
258-
* @param $table
259-
* @param bool|false $post_check
260208
* @throws Exception
261209
*/
262-
public function dropIfExists($table, $post_check = false)
210+
public function dropIfExists(string $table, bool $post_check = false)
263211
{
264212
/** @noinspection SqlNoDataSourceInspection */
265-
/** @noinspection SqlResolve */
266213
$this->query('DROP TABLE IF EXISTS `' . $this->e($table) . '`');
267214
if ($post_check && $this->tableExists($table, true)) {
268215
$msg = 'Unable to drop table';
@@ -272,16 +219,11 @@ public function dropIfExists($table, $post_check = false)
272219

273220
/**
274221
* Delete the table if it is empty
275-
*
276-
* @param $table
277-
* @param bool|false $post_check
278-
* @throws Exception
279222
*/
280-
public function dropIfEmpty($table, $post_check = false)
223+
public function dropIfEmpty(string $table, bool $post_check = false)
281224
{
282225
if ($this->isEmpty($table)) {
283226
/** @noinspection SqlNoDataSourceInspection */
284-
/** @noinspection SqlResolve */
285227
$this->query('DROP TABLE IF EXISTS `' . $this->e($table) . '`');
286228
if ($post_check && $this->tableExists($table, true)) {
287229
$msg = 'Unable to drop table';
@@ -292,15 +234,11 @@ public function dropIfEmpty($table, $post_check = false)
292234

293235
/**
294236
* Split a string that represents multiple sql queries, separated by a separator ";"
295-
*
296-
* @param string $query
297-
* @return array
298237
*/
299-
public function splitMultiQuery($query)
238+
public function splitMultiQuery(string $query): array
300239
{
301240
$ret = array();
302241
$token = Tools::my_strtok($query, ";");
303-
304242
while ($token) {
305243
$prev = $token;
306244
$ret[] = $prev;
@@ -310,17 +248,13 @@ public function splitMultiQuery($query)
310248
return $ret;
311249
}
312250
}
313-
314251
return $ret;
315252
}
316253

317254
/**
318255
* Returns current database name
319-
*
320-
* @param void
321-
* @return string
322256
*/
323-
public function selectSchema()
257+
public function selectSchema(): string
324258
{
325259
return $this->query('select schema()')->fetch_one();
326260
}

0 commit comments

Comments
 (0)