From 9c3be2942e7a2bd287cd9ed56bea65713f57b62a Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Fri, 9 Jun 2023 18:23:28 +0900 Subject: [PATCH] phan and phpstan fixes, also add a convert flag reset to original dbResetConvertFlag resets to the settings given on init of class --- www/lib/CoreLibs/Admin/EditBase.php | 3 +- www/lib/CoreLibs/DB/Extended/ArrayIO.php | 3 +- www/lib/CoreLibs/DB/IO.php | 73 +++++++++++++++-------- www/lib/CoreLibs/Logging/Logging.php | 2 +- www/lib/CoreLibs/Output/Form/Generate.php | 3 +- www/lib/CoreLibs/Output/Image.php | 7 ++- 6 files changed, 60 insertions(+), 31 deletions(-) diff --git a/www/lib/CoreLibs/Admin/EditBase.php b/www/lib/CoreLibs/Admin/EditBase.php index 6a18572b..05538daf 100644 --- a/www/lib/CoreLibs/Admin/EditBase.php +++ b/www/lib/CoreLibs/Admin/EditBase.php @@ -41,7 +41,8 @@ class EditBase /** * construct form generator * - * @param array $db_config db config array, mandatory + * phpcs:ignore + * @param array{db_name:string,db_user:string,db_pass:string,db_host:string,db_port:int,db_schema:string,db_encoding:string,db_type:string,db_ssl:string,db_convert_type?:string[]} $db_config db config array, mandatory * @param \CoreLibs\Logging\Logging $log Logging class, null auto set * @param \CoreLibs\Language\L10n $l10n l10n language class, null auto set * @param \CoreLibs\ACL\Login $login login class for ACL settings diff --git a/www/lib/CoreLibs/DB/Extended/ArrayIO.php b/www/lib/CoreLibs/DB/Extended/ArrayIO.php index 2dce7bb7..36a9fd3a 100644 --- a/www/lib/CoreLibs/DB/Extended/ArrayIO.php +++ b/www/lib/CoreLibs/DB/Extended/ArrayIO.php @@ -54,7 +54,8 @@ class ArrayIO extends \CoreLibs\DB\IO * constructor for the array io class, set the * primary key name automatically (from array) * - * @param array $db_config db connection config + * phpcs:ignore + * @param array{db_name:string,db_user:string,db_pass:string,db_host:string,db_port:int,db_schema:string,db_encoding:string,db_type:string,db_ssl:string,db_convert_type?:string[]} $db_config db connection config * @param array $table_array table array config * @param string $table_name table name string * @param \CoreLibs\Logging\Logging $log Logging class diff --git a/www/lib/CoreLibs/DB/IO.php b/www/lib/CoreLibs/DB/IO.php index 47ab9ce3..9c6e4ae5 100644 --- a/www/lib/CoreLibs/DB/IO.php +++ b/www/lib/CoreLibs/DB/IO.php @@ -339,7 +339,7 @@ class IO // 2: convert json/jsonb to array (CONVERT_JSON) // 4: convert numeric/floatN to float (CONVERT_NUMERIC) // 8: convert bytea to string data (CONVERT_BYTEA) - /** @var int convert type settings as bit mask, 0 for off, anything >2 will aways set 1 too */ + /** @var int type settings as bit mask, 0 for off, anything >2 will aways set 1 too */ private int $convert_type = Convert::off->value; // FOR BELOW: (This should be private and only readable through some method) // cursor array for cached readings @@ -410,7 +410,8 @@ class IO * main DB concstructor with auto connection to DB * and failure set on failed connection * - * @param array $db_config DB configuration array + * phpcs:ignore + * @param array{db_name:string,db_user:string,db_pass:string,db_host:string,db_port:int,db_schema:string,db_encoding:string,db_type:string,db_ssl:string,db_convert_type?:string[]} $db_config DB configuration array * @param \CoreLibs\Logging\Logging $log Logging class */ public function __construct( @@ -508,7 +509,8 @@ class IO /** * Setup DB config and options * - * @param array> $db_config + * phpcs:ignore + * @param array{db_name:string,db_user:string,db_pass:string,db_host:string,db_port:int,db_schema:string,db_encoding:string,db_type:string,db_ssl:string,db_convert_type?:string[]} $db_config * @return bool */ private function __setConfigOptions(array $db_config): bool @@ -548,29 +550,40 @@ class IO continue; } $this->db_convert_type[] = $db_convert_type; - switch ($db_convert_type) { - case 'on': - $this->convert_type |= Convert::on->value; - break; - case 'json': - $this->convert_type |= Convert::on->value; - $this->convert_type |= Convert::json->value; - break; - case 'numeric': - $this->convert_type |= Convert::on->value; - $this->convert_type |= Convert::numeric->value; - break; - case 'bytea': - $this->convert_type |= Convert::on->value; - $this->convert_type |= Convert::bytea->value; - break; - } + $this->__setConvertType($db_convert_type); } // return status true: ok, false: options error return true; } + /** + * Set the convert bit flags + * + * @param string $db_convert_type One of 'on', 'json', 'numeric', 'bytea' + * @return void + */ + private function __setConvertType(string $db_convert_type): void + { + switch ($db_convert_type) { + case 'on': + $this->convert_type |= Convert::on->value; + break; + case 'json': + $this->convert_type |= Convert::on->value; + $this->convert_type |= Convert::json->value; + break; + case 'numeric': + $this->convert_type |= Convert::on->value; + $this->convert_type |= Convert::numeric->value; + break; + case 'bytea': + $this->convert_type |= Convert::on->value; + $this->convert_type |= Convert::bytea->value; + break; + } + } + /** * based on $this->db_type * here we need to load the db pgsql include one @@ -1046,7 +1059,7 @@ class IO // always bool/int if ( $this->dbGetFieldType($key) != 'interval' && - str_starts_with($this->dbGetFieldType($key), 'int') + str_starts_with($this->dbGetFieldType($key) ?: '', 'int') ) { $row[$key] = (int)$value; } @@ -1055,15 +1068,15 @@ class IO } if ( $this->convert_type & Convert::json->value && - str_starts_with($this->dbGetFieldType($key), 'json') + str_starts_with($this->dbGetFieldType($key) ?: '', 'json') ) { $row[$key] = Json::jsonConvertToArray($value); } if ( $this->convert_type & Convert::numeric->value && ( - str_starts_with($this->dbGetFieldType($key), 'numeric') || - str_starts_with($this->dbGetFieldType($key), 'float') + str_starts_with($this->dbGetFieldType($key) ?: '', 'numeric') || + str_starts_with($this->dbGetFieldType($key) ?: '', 'float') // $this->dbGetFieldType($key) == 'real' ) ) { @@ -3407,6 +3420,18 @@ class IO $this->convert_type &= ~$convert->value; } + /** + * Reset to origincal config file set + * + * @return void + */ + public function dbResetConvertFlag(): void + { + foreach ($this->db_convert_type as $db_convert_type) { + $this->__setConvertType($db_convert_type); + } + } + /** * Undocumented function * diff --git a/www/lib/CoreLibs/Logging/Logging.php b/www/lib/CoreLibs/Logging/Logging.php index 4dd2f99f..443a4bee 100644 --- a/www/lib/CoreLibs/Logging/Logging.php +++ b/www/lib/CoreLibs/Logging/Logging.php @@ -953,11 +953,11 @@ class Logging * * @param string $group_id id for error message, groups messages together * @param string|Stringable $message the actual error message + * @param mixed[] $context * @param string $prefix Attach some block before $string. * Will not be stripped even * when strip is true * if strip is false, recommended to add that to $string - * @param mixed[] $context * @return bool True if logged, false if not logged */ public function debug( diff --git a/www/lib/CoreLibs/Output/Form/Generate.php b/www/lib/CoreLibs/Output/Form/Generate.php index 52884871..ba23f1ea 100644 --- a/www/lib/CoreLibs/Output/Form/Generate.php +++ b/www/lib/CoreLibs/Output/Form/Generate.php @@ -308,7 +308,8 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO /** * construct form generator * - * @param array $db_config db config array, mandatory + * phpcs:ignore + * @param array{db_name:string,db_user:string,db_pass:string,db_host:string,db_port:int,db_schema:string,db_encoding:string,db_type:string,db_ssl:string,db_convert_type?:string[]} $db_config db config array, mandatory * @param \CoreLibs\Logging\Logging $log Logging class * @param \CoreLibs\Language\L10n $l10n l10n language class * @param array $login_acl Login ACL array, diff --git a/www/lib/CoreLibs/Output/Image.php b/www/lib/CoreLibs/Output/Image.php index 8f5a02ad..f530ec01 100644 --- a/www/lib/CoreLibs/Output/Image.php +++ b/www/lib/CoreLibs/Output/Image.php @@ -36,6 +36,7 @@ class Image ): string|false { // get image type flags $image_types = [ + 0 => 'UNKOWN-IMAGE', 1 => 'gif', 2 => 'jpg', 3 => 'png' @@ -69,7 +70,7 @@ class Image } // does this picture exist and is it a picture if (file_exists($filename) && is_file($filename)) { - [$width, $height, $type] = getimagesize($filename) ?: [0, 0, null]; + [$width, $height, $type] = getimagesize($filename) ?: [0, 0, 0]; $convert_prefix = ''; $create_file = false; $delete_filename = ''; @@ -98,7 +99,7 @@ class Image if (!is_file($filename)) { $filename .= '-0'; } - [$width, $height, $type] = getimagesize($filename) ?: [0, 0, null]; + [$width, $height, $type] = getimagesize($filename) ?: [0, 0, 0]; } // if no size given, set size to original if (!$size_x || $size_x < 1) { @@ -117,7 +118,7 @@ class Image $status = exec($convert_string, $output, $return); // get the size of the converted data, if converted if (is_file($thumbnail)) { - [$width, $height, $type] = getimagesize($thumbnail) ?: [0, 0, null]; + [$width, $height, $type] = getimagesize($thumbnail) ?: [0, 0, 0]; } } if ($height > $size_y) {