From c72914bba2363e436574204b3d6093a6f3cfce89 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Fri, 28 Apr 2023 14:01:11 +0200 Subject: 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 --- lib/Minz/Configuration.php | 4 ++-- lib/Minz/ModelPdo.php | 60 ++++++++++++++++++++++++++++++++++++++++++++++ lib/Minz/Pdo.php | 2 +- lib/Minz/PdoMysql.php | 2 +- lib/Minz/PdoPgsql.php | 2 +- lib/Minz/PdoSqlite.php | 2 +- 6 files changed, 66 insertions(+), 6 deletions(-) (limited to 'lib') 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} $db + * @property array{'type':string,'host':string,'user':string,'password':string,'base':string,'prefix':string, + * 'connection_uri_params':string,'pdo_options':array} $db * @property-read string $disable_update * @property-read string $environment * @property array $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 $values + * @phpstan-return ($mode is PDO::FETCH_ASSOC ? array>|null : array|null) + * @return array>|array|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 $values + * @return array>|null + */ + public function fetchAssoc(string $sql, array $values = []): ?array { + return $this->fetchAny($sql, $values, PDO::FETCH_ASSOC); + } + + /** + * @param array $values + * @return array|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|null $options */ + /** @param array|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|null $options */ + /** @param array|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|null $options */ + /** @param array|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|null $options */ + /** @param array|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;'); -- cgit v1.2.3