aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGravatar Luc SANCHEZ <4697568+ColonelMoutarde@users.noreply.github.com> 2022-01-07 10:05:09 +0100
committerGravatar GitHub <noreply@github.com> 2022-01-07 10:05:09 +0100
commited19445f74c30854c60873cd1df1c38e15fc316b (patch)
tree926496a8e1f4d71c8a9b2fd0f87e835037792491 /lib
parentf8c5df28ab3f7e68460b74fed11f799c702435b7 (diff)
Optimise Minz_ModelPdo::class (#4119)
* - Fix typo, - remove unnecessary null in property, - remove unused property, - add phpDoc, - add ext PDO in composer.json, - use strict comparison, - indentation * Translate * Update lib/Minz/ModelPdo.php Co-authored-by: Frans de Jonge <fransdejonge@gmail.com> * The code is more explicite * Fix phpstan * Fix phpstan expect one * Fix phpstan * Return in back... * make fix-all * Fix exception and more types * Fix more types * Remove ext- in composer.json Co-authored-by: Luc SANCHEZ <l.sanchez-ext@ubitransport.com> Co-authored-by: Frans de Jonge <fransdejonge@gmail.com> Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
Diffstat (limited to 'lib')
-rw-r--r--lib/Minz/ModelPdo.php61
-rw-r--r--lib/Minz/Pdo.php14
-rw-r--r--lib/Minz/PdoMysql.php4
-rw-r--r--lib/Minz/PdoPgsql.php6
-rw-r--r--lib/Minz/PdoSqlite.php4
5 files changed, 65 insertions, 24 deletions
diff --git a/lib/Minz/ModelPdo.php b/lib/Minz/ModelPdo.php
index f183dae10..0f5b9efca 100644
--- a/lib/Minz/ModelPdo.php
+++ b/lib/Minz/ModelPdo.php
@@ -6,20 +6,41 @@
*/
/**
- * La classe Model_sql représente le modèle interragissant avec les bases de données
+ * The Model_sql class represents the model for interacting with databases.
*/
class Minz_ModelPdo {
/**
- * Partage la connexion à la base de données entre toutes les instances.
+ * Shares the connection to the database between all instances.
*/
public static $usesSharedPdo = true;
- private static $sharedPdo = null;
+
+ /**
+ * @var Minz_Pdo|null
+ */
+ private static $sharedPdo;
+
+ /**
+ * @var string|null
+ */
private static $sharedCurrentUser;
+ /**
+ * @var Minz_Pdo|null
+ */
protected $pdo;
+
+ /**
+ * @var string|null
+ */
protected $current_user;
+ /**
+ * @return void
+ * @throws Minz_ConfigurationNamespaceException
+ * @throws Minz_PDOConnectionException
+ * @throws PDOException
+ */
private function dbConnect() {
$db = Minz_Configuration::get('system')->db;
$driver_options = isset($db['pdo_options']) && is_array($db['pdo_options']) ? $db['pdo_options'] : [];
@@ -67,22 +88,25 @@ class Minz_ModelPdo {
}
/**
- * Créé la connexion à la base de données à l'aide des variables
- * HOST, BASE, USER et PASS définies dans le fichier de configuration
+ * Create the connection to the database using the variables
+ * HOST, BASE, USER and PASS variables defined in the configuration file
+ * @param string|null $currentUser
+ * @param Minz_Pdo|null $currentPdo
+ * @throws Minz_ConfigurationNamespaceException
+ * @throws Minz_PDOConnectionException
*/
public function __construct($currentUser = null, $currentPdo = null) {
if ($currentUser === null) {
$currentUser = Minz_Session::param('currentUser');
}
- if ($currentPdo != null) {
+ if ($currentPdo !== null) {
$this->pdo = $currentPdo;
return;
}
if ($currentUser == '') {
throw new Minz_PDOConnectionException('Current user must not be empty!', '', Minz_Exception::ERROR);
}
- if (self::$usesSharedPdo && self::$sharedPdo != null &&
- ($currentUser == '' || $currentUser === self::$sharedCurrentUser)) {
+ if (self::$usesSharedPdo && self::$sharedPdo !== null && $currentUser === self::$sharedCurrentUser) {
$this->pdo = self::$sharedPdo;
$this->current_user = self::$sharedCurrentUser;
return;
@@ -99,9 +123,11 @@ class Minz_ModelPdo {
} catch (PDOException $e) {
$ex = $e;
if (empty($e->errorInfo[0]) || $e->errorInfo[0] !== '08006') {
- //We are only only interested in: SQLSTATE connection exception / connection failure
+ //We are only interested in: SQLSTATE connection exception / connection failure
break;
}
+ } catch (Exception $e) {
+ $ex = $e;
}
sleep(2);
}
@@ -114,19 +140,34 @@ class Minz_ModelPdo {
);
}
+ /**
+ * @return void
+ */
public function beginTransaction() {
$this->pdo->beginTransaction();
}
- public function inTransaction() {
+
+ public function inTransaction(): bool {
return $this->pdo->inTransaction();
}
+
+ /**
+ * @return void
+ */
public function commit() {
$this->pdo->commit();
}
+
+ /**
+ * @return void
+ */
public function rollBack() {
$this->pdo->rollBack();
}
+ /**
+ * @return void
+ */
public static function clean() {
self::$sharedPdo = null;
self::$sharedCurrentUser = '';
diff --git a/lib/Minz/Pdo.php b/lib/Minz/Pdo.php
index 08436393e..9dcf0e5fa 100644
--- a/lib/Minz/Pdo.php
+++ b/lib/Minz/Pdo.php
@@ -6,7 +6,7 @@
*/
abstract class Minz_Pdo extends PDO {
- public function __construct($dsn, $username = null, $passwd = null, $options = null) {
+ public function __construct(string $dsn, $username = null, $passwd = null, $options = null) {
parent::__construct($dsn, $username, $passwd, $options);
$this->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
@@ -14,18 +14,18 @@ abstract class Minz_Pdo extends PDO {
abstract public function dbType();
private $prefix = '';
- public function prefix() {
+ public function prefix(): string {
return $this->prefix;
}
- public function setPrefix($prefix) {
+ public function setPrefix(string $prefix) {
$this->prefix = $prefix;
}
- private function autoPrefix($sql) {
+ private function autoPrefix(string $sql): string {
return str_replace('`_', '`' . $this->prefix, $sql);
}
- protected function preSql($statement) {
+ protected function preSql(string $statement): string {
if (preg_match('/^(?:UPDATE|INSERT|DELETE)/i', $statement)) {
invalidateHttpCache();
}
@@ -43,14 +43,14 @@ abstract class Minz_Pdo extends PDO {
// PHP8+: PDO::prepare(string $query, array $options = []): PDOStatement|false
#[\ReturnTypeWillChange]
- public function prepare($statement, $driver_options = array()) {
+ public function prepare(string $statement, array $driver_options = []) {
$statement = $this->preSql($statement);
return parent::prepare($statement, $driver_options);
}
// PHP8+: PDO::exec(string $statement): int|false
#[\ReturnTypeWillChange]
- public function exec($statement) {
+ public function exec(string $statement) {
$statement = $this->preSql($statement);
return parent::exec($statement);
}
diff --git a/lib/Minz/PdoMysql.php b/lib/Minz/PdoMysql.php
index e5d62bcc5..b66cccf28 100644
--- a/lib/Minz/PdoMysql.php
+++ b/lib/Minz/PdoMysql.php
@@ -6,12 +6,12 @@
*/
class Minz_PdoMysql extends Minz_Pdo {
- public function __construct($dsn, $username = null, $passwd = null, $options = null) {
+ public function __construct(string $dsn, $username = null, $passwd = null, $options = null) {
parent::__construct($dsn, $username, $passwd, $options);
$this->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
}
- public function dbType() {
+ public function dbType(): string {
return 'mysql';
}
diff --git a/lib/Minz/PdoPgsql.php b/lib/Minz/PdoPgsql.php
index 7d1a1912b..cae0fe476 100644
--- a/lib/Minz/PdoPgsql.php
+++ b/lib/Minz/PdoPgsql.php
@@ -6,16 +6,16 @@
*/
class Minz_PdoPgsql extends Minz_Pdo {
- public function __construct($dsn, $username = null, $passwd = null, $options = null) {
+ public function __construct(string $dsn, $username = null, $passwd = null, $options = null) {
parent::__construct($dsn, $username, $passwd, $options);
$this->exec("SET NAMES 'UTF8';");
}
- public function dbType() {
+ public function dbType(): string {
return 'pgsql';
}
- protected function preSql($statement) {
+ protected function preSql(string $statement): string {
$statement = parent::preSql($statement);
return str_replace(array('`', ' LIKE '), array('"', ' ILIKE '), $statement);
}
diff --git a/lib/Minz/PdoSqlite.php b/lib/Minz/PdoSqlite.php
index 978ee8db1..2c90413a1 100644
--- a/lib/Minz/PdoSqlite.php
+++ b/lib/Minz/PdoSqlite.php
@@ -6,12 +6,12 @@
*/
class Minz_PdoSqlite extends Minz_Pdo {
- public function __construct($dsn, $username = null, $passwd = null, $options = null) {
+ public function __construct(string $dsn, $username = null, $passwd = null, $options = null) {
parent::__construct($dsn, $username, $passwd, $options);
$this->exec('PRAGMA foreign_keys = ON;');
}
- public function dbType() {
+ public function dbType(): string {
return 'sqlite';
}