aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2023-04-28 14:01:11 +0200
committerGravatar GitHub <noreply@github.com> 2023-04-28 14:01:11 +0200
commitc72914bba2363e436574204b3d6093a6f3cfce89 (patch)
tree377008a7393e4d80e4c8659f27dd42c0ccbab382 /lib
parent26e2a703125ffe1d0d2746b0e5ea3491b627832c (diff)
PHPStan Level 7 for more DAO PDO (#5328)
* PHPStan Level 7 for more DAO PDO With new function to address common type and check problems * A bit more * PHPStan Level 7 for FreshRSS_Entry
Diffstat (limited to 'lib')
-rw-r--r--lib/Minz/Configuration.php4
-rw-r--r--lib/Minz/ModelPdo.php60
-rw-r--r--lib/Minz/Pdo.php2
-rw-r--r--lib/Minz/PdoMysql.php2
-rw-r--r--lib/Minz/PdoPgsql.php2
-rw-r--r--lib/Minz/PdoSqlite.php2
6 files changed, 66 insertions, 6 deletions
diff --git a/lib/Minz/Configuration.php b/lib/Minz/Configuration.php
index d641d8994..4ed0233f4 100644
--- a/lib/Minz/Configuration.php
+++ b/lib/Minz/Configuration.php
@@ -3,8 +3,8 @@
/**
* Manage configuration for the application.
* @property-read string $base_url
- * @property array{'type'?:string,'host'?:string,'user'?:string,'password'?:string,'base'?:string,'prefix'?:string,
- * 'connection_uri_params'?:string,'pdo_options'?:array<string|int,string|int|bool>} $db
+ * @property array{'type':string,'host':string,'user':string,'password':string,'base':string,'prefix':string,
+ * 'connection_uri_params':string,'pdo_options':array<int,int|string|bool>} $db
* @property-read string $disable_update
* @property-read string $environment
* @property array<string,bool> $extensions_enabled
diff --git a/lib/Minz/ModelPdo.php b/lib/Minz/ModelPdo.php
index dc3d0a42c..49556450e 100644
--- a/lib/Minz/ModelPdo.php
+++ b/lib/Minz/ModelPdo.php
@@ -179,4 +179,64 @@ class Minz_ModelPdo {
self::$sharedPdo = null;
self::$sharedCurrentUser = '';
}
+
+ /**
+ * @param array<string,int|string|null> $values
+ * @phpstan-return ($mode is PDO::FETCH_ASSOC ? array<array<string,int|string|null>>|null : array<int|string|null>|null)
+ * @return array<array<string,int|string|null>>|array<int|string|null>|null
+ */
+ private function fetchAny(string $sql, array $values, int $mode, int $column = 0): ?array {
+ $stm = $this->pdo->prepare($sql);
+ $ok = $stm !== false;
+ if ($ok && !empty($values)) {
+ foreach ($values as $name => $value) {
+ if (is_int($value)) $type = PDO::PARAM_INT;
+ elseif (is_string($value)) $type = PDO::PARAM_STR;
+ elseif (is_null($value)) $type = PDO::PARAM_NULL;
+ else {
+ $ok = false;
+ break;
+ }
+ if (!$stm->bindValue($name, $value, $type)) {
+ $ok = false;
+ break;
+ }
+ }
+ }
+ if ($ok && $stm !== false && $stm->execute()) {
+ switch ($mode) {
+ case PDO::FETCH_COLUMN:
+ $res = $stm->fetchAll(PDO::FETCH_COLUMN, $column);
+ break;
+ case PDO::FETCH_ASSOC:
+ default:
+ $res = $stm->fetchAll(PDO::FETCH_ASSOC);
+ break;
+ }
+ if ($res !== false) {
+ return $res;
+ }
+ }
+
+ $callingFunction = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2]['function'] ?? '??';
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
+ Minz_Log::error('SQL error ' . $callingFunction . ' ' . json_encode($info));
+ return null;
+ }
+
+ /**
+ * @param array<string,int|string|null> $values
+ * @return array<array<string,int|string|null>>|null
+ */
+ public function fetchAssoc(string $sql, array $values = []): ?array {
+ return $this->fetchAny($sql, $values, PDO::FETCH_ASSOC);
+ }
+
+ /**
+ * @param array<string,int|string|null> $values
+ * @return array<int|string|null>|null
+ */
+ public function fetchColumn(string $sql, int $column, array $values = []): ?array {
+ return $this->fetchAny($sql, $values, PDO::FETCH_COLUMN, $column);
+ }
}
diff --git a/lib/Minz/Pdo.php b/lib/Minz/Pdo.php
index 2efff61d4..41a3e9c84 100644
--- a/lib/Minz/Pdo.php
+++ b/lib/Minz/Pdo.php
@@ -6,7 +6,7 @@
*/
abstract class Minz_Pdo extends PDO {
- /** @param array<int,int|string>|null $options */
+ /** @param array<int,int|string|bool>|null $options */
public function __construct(string $dsn, ?string $username = null, ?string $passwd = null, ?array $options = null) {
parent::__construct($dsn, $username, $passwd, $options);
$this->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
diff --git a/lib/Minz/PdoMysql.php b/lib/Minz/PdoMysql.php
index ee411d949..df17a2f52 100644
--- a/lib/Minz/PdoMysql.php
+++ b/lib/Minz/PdoMysql.php
@@ -6,7 +6,7 @@
*/
class Minz_PdoMysql extends Minz_Pdo {
- /** @param array<int,int|string>|null $options */
+ /** @param array<int,int|string|bool>|null $options */
public function __construct(string $dsn, ?string $username = null, ?string $passwd = null, ?array $options = null) {
parent::__construct($dsn, $username, $passwd, $options);
$this->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
diff --git a/lib/Minz/PdoPgsql.php b/lib/Minz/PdoPgsql.php
index b239d3544..e0efb8ad1 100644
--- a/lib/Minz/PdoPgsql.php
+++ b/lib/Minz/PdoPgsql.php
@@ -6,7 +6,7 @@
*/
class Minz_PdoPgsql extends Minz_Pdo {
- /** @param array<int,int|string>|null $options */
+ /** @param array<int,int|string|bool>|null $options */
public function __construct(string $dsn, ?string $username = null, ?string $passwd = null, ?array $options = null) {
parent::__construct($dsn, $username, $passwd, $options);
$this->exec("SET NAMES 'UTF8';");
diff --git a/lib/Minz/PdoSqlite.php b/lib/Minz/PdoSqlite.php
index 479879ffe..a66923ff2 100644
--- a/lib/Minz/PdoSqlite.php
+++ b/lib/Minz/PdoSqlite.php
@@ -6,7 +6,7 @@
*/
class Minz_PdoSqlite extends Minz_Pdo {
- /** @param array<int,int|string>|null $options */
+ /** @param array<int,int|string|bool>|null $options */
public function __construct(string $dsn, ?string $username = null, ?string $passwd = null, ?array $options = null) {
parent::__construct($dsn, $username, $passwd, $options);
$this->exec('PRAGMA foreign_keys = ON;');