From 9edfc2acb67a6c76e3303a5b4bff058dfdda914c Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Mon, 18 Nov 2024 17:08:28 +0900 Subject: [PATCH] phpstan 2.0 update checks --- 4dev/checking/phpunit.sh | 2 +- phpstan.neon | 1 + www/admin/class_test.array.php | 3 - www/admin/class_test.db.dbReturn.php | 15 ++-- www/admin/class_test.db.php | 8 +- www/includes/edit_base.php | 2 +- www/lib/CoreLibs/ACL/Login.php | 13 +-- www/lib/CoreLibs/Admin/Backend.php | 5 +- www/lib/CoreLibs/Check/Colors.php | 11 ++- www/lib/CoreLibs/Convert/Color/CieXyz.php | 2 +- www/lib/CoreLibs/Create/Uids.php | 2 +- www/lib/CoreLibs/DB/Extended/ArrayIO.php | 4 +- www/lib/CoreLibs/DB/IO.php | 4 +- .../CoreLibs/Language/Core/GetTextReader.php | 8 +- www/lib/CoreLibs/Output/Form/Generate.php | 80 +++++++------------ www/lib/CoreLibs/Output/Image.php | 18 +++-- www/lib/CoreLibs/UrlRequests/Curl.php | 6 +- 17 files changed, 79 insertions(+), 105 deletions(-) diff --git a/4dev/checking/phpunit.sh b/4dev/checking/phpunit.sh index e331bd6a..0fbf93d8 100755 --- a/4dev/checking/phpunit.sh +++ b/4dev/checking/phpunit.sh @@ -36,7 +36,7 @@ if [ -n "${2}" ] && [ -z "${php_bin}" ]; then fi; # Note 4dev/tests/bootstrap.php has to be set as bootstrap file in phpunit.xml -phpunit_call="${php_bin}${base}tools/phpunit ${opt_testdox} -c ${base}phpunit.xml ${base}4dev/tests/"; +phpunit_call="${php_bin}${base}vendor/bin/phpunit ${opt_testdox} -c ${base}phpunit.xml ${base}4dev/tests/"; ${phpunit_call}; diff --git a/phpstan.neon b/phpstan.neon index a84ae62c..052adcbf 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,6 +2,7 @@ includes: - phpstan-conditional.php #- ./vendor/yamadashy/phpstan-friendly-formatter/extension.neon + - phar://phpstan.phar/conf/bleedingEdge.neon parameters: tmpDir: %currentWorkingDirectory%/tmp/phpstan-corelibs #errorFormat: friendly diff --git a/www/admin/class_test.array.php b/www/admin/class_test.array.php index cadf8117..85dbfc0a 100644 --- a/www/admin/class_test.array.php +++ b/www/admin/class_test.array.php @@ -115,9 +115,6 @@ print "ARRAYFLATFORKEY: " . DgS::printAr(ArrayHandler::arrayFlatForKey($test_arr */ function rec(string $pre, string $cur, array $node = []) { - if (!is_array($node)) { - $node = []; - } print "
#### PRE: " . $pre . ", CUR: " . $cur . ", N-c: " . count($node) . " [" . join('|', array_keys($node)) . "]
"; if (!$pre) { diff --git a/www/admin/class_test.db.dbReturn.php b/www/admin/class_test.db.dbReturn.php index 6d9d141f..17166558 100644 --- a/www/admin/class_test.db.dbReturn.php +++ b/www/admin/class_test.db.dbReturn.php @@ -70,8 +70,7 @@ for ($i = 1; $i <= 6; $i++) { print $i . ") " . $cache_flag . ": " . "res: " . (is_bool($res) ? "Bool: " . Support::prBl($res) : - (is_array($res) ? - "Array: " . Support::prBl(is_array($res)) : '{-}') + "Array: Yes" ) . ", " . "cursor_ext:
" . Support::printAr(
 			SetVarType::setArray($db->dbGetCursorExt($q_db_ret))
@@ -89,8 +88,7 @@ for ($i = 1; $i <= 6; $i++) {
 	print $i . ") " . $cache_flag . ": "
 		. "res: " . (is_bool($res) ?
 			"Bool: " . Support::prBl($res) :
-			(is_array($res) ?
-				"Array: " . Support::prBl(is_array($res)) : '{-}')
+			"Array: Yes"
 		) . ", "
 		. "cursor_ext: 
" . Support::printAr(
 			SetVarType::setArray($db->dbGetCursorExt($q_db_ret))
@@ -108,8 +106,7 @@ for ($i = 1; $i <= 6; $i++) {
 	print $i . ") " . $cache_flag . ": "
 		. "res: " . (is_bool($res) ?
 			"Bool: " . Support::prBl($res) :
-			(is_array($res) ?
-				"Array: " . Support::prBl(is_array($res)) : '{-}')
+			"Array: Yes"
 		) . ", "
 		. "cursor_ext: 
" . Support::printAr(
 			SetVarType::setArray($db->dbGetCursorExt($q_db_ret))
@@ -127,8 +124,7 @@ for ($i = 1; $i <= 6; $i++) {
 	print $i . ") " . $cache_flag . ": "
 		. "res: " . (is_bool($res) ?
 			"Bool: " . Support::prBl($res) :
-			(is_array($res) ?
-				"Array: " . Support::prBl(is_array($res)) : '{-}')
+			"Array: Yes"
 		) . ", "
 		. "cursor_ext: 
" . Support::printAr(
 			SetVarType::setArray($db->dbGetCursorExt($q_db_ret))
@@ -146,8 +142,7 @@ for ($i = 1; $i <= 6; $i++) {
 	print $i . ") " . $cache_flag . ": "
 		. "res: " . (is_bool($res) ?
 			"Bool: " . Support::prBl($res) :
-			(is_array($res) ?
-				"Array: " . Support::prBl(is_array($res)) : '{-}')
+			"Array: Yes"
 		) . ", "
 		. "cursor_ext: 
" . Support::printAr(
 			SetVarType::setArray($db->dbGetCursorExt($q_db_ret))
diff --git a/www/admin/class_test.db.php b/www/admin/class_test.db.php
index 31afe4a7..158c0fff 100644
--- a/www/admin/class_test.db.php
+++ b/www/admin/class_test.db.php
@@ -316,7 +316,8 @@ print "EOM STRING EXEC RETURN TEST: " . print_r(
 	$db->dbReturnRowParams(
 		$query_select,
 		[$__last_insert_id]
-	)
+	),
+	true
 ) . "
"; // B $status = $db->dbExecParams( @@ -345,7 +346,8 @@ print "EOM STRING EXEC RETURN TEST: " . print_r( $db->dbReturnRowParams( $query_select, [$__last_insert_id] - ) + ), + true ) . "
"; // params > 10 for debug // error catcher @@ -674,7 +676,7 @@ echo "
"; print "COMPOSITE ELEMENT READ
"; $res = $db->dbReturnRow("SELECT item, count, (item).name, (item).price, (item).supplier_id FROM on_hand"); -print "ROW:
" . print_r($res) . "
"; +print "ROW:
" . print_r($res, true) . "
"; var_dump($res); print "Field Name/Types:
" . print_r($db->dbGetFieldNameTypes(), true) . "
"; echo "
"; diff --git a/www/includes/edit_base.php b/www/includes/edit_base.php index c5968e96..fd2256d8 100644 --- a/www/includes/edit_base.php +++ b/www/includes/edit_base.php @@ -24,7 +24,7 @@ declare(strict_types=1); ob_start(); -require 'config.php'; +require 'config.php'; /** @phpstan-ignore-line Is path, is symlinked */ // should be utf8 header("Content-type: text/html; charset=" . DEFAULT_ENCODING); diff --git a/www/lib/CoreLibs/ACL/Login.php b/www/lib/CoreLibs/ACL/Login.php index 2a38c219..2ed56c09 100644 --- a/www/lib/CoreLibs/ACL/Login.php +++ b/www/lib/CoreLibs/ACL/Login.php @@ -960,10 +960,7 @@ class Login . "AND ear.edit_access_right_id = epa.edit_access_right_id " . "AND epa.enabled = 1 AND epa.edit_group_id = " . $res["edit_group_id"] . " " . "ORDER BY ep.order_number"; - while ($res = $this->db->dbReturn($q)) { - if (!is_array($res)) { - break; - } + while (is_array($res = $this->db->dbReturn($q))) { // page id array for sub data readout $edit_page_ids[$res['edit_page_id']] = $res['cuid']; // create the array for pages @@ -1303,11 +1300,9 @@ class Login { $is_valid_password = true; // check for valid in regex arrays in list - if (is_array($this->password_valid_chars)) { - foreach ($this->password_valid_chars as $password_valid_chars) { - if (!preg_match("/$password_valid_chars/", $password)) { - $is_valid_password = false; - } + foreach ($this->password_valid_chars as $password_valid_chars) { + if (!preg_match("/$password_valid_chars/", $password)) { + $is_valid_password = false; } } // check for min length diff --git a/www/lib/CoreLibs/Admin/Backend.php b/www/lib/CoreLibs/Admin/Backend.php index 9f705c7b..5fb2918b 100644 --- a/www/lib/CoreLibs/Admin/Backend.php +++ b/www/lib/CoreLibs/Admin/Backend.php @@ -425,10 +425,7 @@ class Backend ?string $set_content_path = null, int $flag = 0, ): array { - if ( - $set_content_path === null || - !is_string($set_content_path) - ) { + if ($set_content_path === null) { /** @deprecated adbTopMenu missing set_content_path parameter */ trigger_error( 'Calling adbTopMenu without set_content_path parameter is deprecated', diff --git a/www/lib/CoreLibs/Check/Colors.php b/www/lib/CoreLibs/Check/Colors.php index d896664a..8630dd12 100644 --- a/www/lib/CoreLibs/Check/Colors.php +++ b/www/lib/CoreLibs/Check/Colors.php @@ -119,6 +119,13 @@ class Colors /** * check if html/css color string is valid + * + * TODO: update check for correct validate values + * - space instead of "," + * - / opcatiy checks + * - loose numeric values + * - lab/lch,oklab/oklch validation too + * * @param string $color A color string of any format * @param int $flags defaults to ALL, else use | to combined from * HEX_RGB, HEX_RGBA, RGB, RGBA, HSL, HSLA @@ -168,9 +175,9 @@ class Colors if (preg_match("/$regex/", $color)) { // if valid regex, we now need to check if the content is actually valid // only for rgb/hsl type - /** @var int|false */ + /** @var int<0, max>|false */ $rgb_flag = strpos($color, 'rgb'); - /** @var int|false */ + /** @var int<0, max>|false */ $hsl_flag = strpos($color, 'hsl'); // if both not match, return true if ( diff --git a/www/lib/CoreLibs/Convert/Color/CieXyz.php b/www/lib/CoreLibs/Convert/Color/CieXyz.php index 2784f3e8..40c90bb0 100644 --- a/www/lib/CoreLibs/Convert/Color/CieXyz.php +++ b/www/lib/CoreLibs/Convert/Color/CieXyz.php @@ -246,7 +246,7 @@ class CieXyz self::convertArray(array_map( fn ($k, $v) => $v * $d50[$k], array_keys($xyz), - array_values($xyz), + $xyz, )), options: ["whitepoint" => 'D50'] ); diff --git a/www/lib/CoreLibs/Create/Uids.php b/www/lib/CoreLibs/Create/Uids.php index 47691338..42339797 100644 --- a/www/lib/CoreLibs/Create/Uids.php +++ b/www/lib/CoreLibs/Create/Uids.php @@ -38,7 +38,7 @@ class Uids $uniqid_length++; } /** @var int<1,max> make sure that internal this is correct */ - $random_bytes_length = ($uniqid_length - ($uniqid_length % 2)) / 2; + $random_bytes_length = (int)(($uniqid_length - ($uniqid_length % 2)) / 2); $uniqid = bin2hex(random_bytes($random_bytes_length)); // if not forced shorten return next lower length if (!$force_length) { diff --git a/www/lib/CoreLibs/DB/Extended/ArrayIO.php b/www/lib/CoreLibs/DB/Extended/ArrayIO.php index d40b71b4..ac98091c 100644 --- a/www/lib/CoreLibs/DB/Extended/ArrayIO.php +++ b/www/lib/CoreLibs/DB/Extended/ArrayIO.php @@ -374,7 +374,7 @@ class ArrayIO extends \CoreLibs\DB\IO public function dbDelete(array $table_array = [], bool $acl_limit = false): array { // is array and has values, override set and set new - if (is_array($table_array) && count($table_array)) { + if (count($table_array)) { $this->table_array = $table_array; } if (!$this->dbCheckPkSet()) { @@ -440,7 +440,7 @@ class ArrayIO extends \CoreLibs\DB\IO public function dbRead(bool $edit = false, array $table_array = []): array { // if array give, overrules internal array - if (is_array($table_array) && count($table_array)) { + if (count($table_array)) { $this->table_array = $table_array; } if (!$this->dbCheckPkSet()) { diff --git a/www/lib/CoreLibs/DB/IO.php b/www/lib/CoreLibs/DB/IO.php index d3ecc374..cae7cf50 100644 --- a/www/lib/CoreLibs/DB/IO.php +++ b/www/lib/CoreLibs/DB/IO.php @@ -914,7 +914,7 @@ class IO if ($cursor !== false) { [$db_prefix, $db_error_string] = $this->db_functions->__dbPrintError($cursor); } - if ($cursor === false && method_exists($this->db_functions, '__dbPrintError')) { + if ($cursor === false && method_exists($this->db_functions, '__dbPrintError')) { /** @phpstan-ignore-line */ [$db_prefix, $db_error_string] = $this->db_functions->__dbPrintError(); } // prefix the master if not the same @@ -1737,7 +1737,7 @@ class IO { if ( !empty($this->dbh) && - $this->dbh instanceof \PgSql\Connection + $this->dbh instanceof \PgSql\Connection /** @phpstan-ignore-line future could be other */ ) { // reset any client encodings set $this->dbResetEncoding(); diff --git a/www/lib/CoreLibs/Language/Core/GetTextReader.php b/www/lib/CoreLibs/Language/Core/GetTextReader.php index 0a5715a7..dd1ef96d 100644 --- a/www/lib/CoreLibs/Language/Core/GetTextReader.php +++ b/www/lib/CoreLibs/Language/Core/GetTextReader.php @@ -190,7 +190,6 @@ class GetTextReader private function loadTables(): void { if ( - is_array($this->cache_translations) && is_array($this->table_originals) && is_array($this->table_translations) ) { @@ -318,10 +317,7 @@ class GetTextReader if ($this->enable_cache) { // Caching enabled, get translated string from cache - if ( - is_array($this->cache_translations) && - array_key_exists($string, $this->cache_translations) - ) { + if (array_key_exists($string, $this->cache_translations)) { return $this->cache_translations[$string]; } else { return $string; @@ -481,7 +477,7 @@ class GetTextReader $key = $single . chr(0) . $plural; if ($this->enable_cache) { - if (is_array($this->cache_translations) && !array_key_exists($key, $this->cache_translations)) { + if (!array_key_exists($key, $this->cache_translations)) { return ($number != 1) ? $plural : $single; } else { $result = $this->cache_translations[$key]; diff --git a/www/lib/CoreLibs/Output/Form/Generate.php b/www/lib/CoreLibs/Output/Form/Generate.php index 62ca7370..68e7a1df 100644 --- a/www/lib/CoreLibs/Output/Form/Generate.php +++ b/www/lib/CoreLibs/Output/Form/Generate.php @@ -474,7 +474,7 @@ class Generate $page_name_camel_case ); try { - /** @var TableArrays\Interface\TableArraysInterface|false $class */ + /** @var TableArrays\Interface\TableArraysInterface $class */ $class = new $class_string($this); } catch (\Throwable $t) { $this->log->critical('CLASS LOADING: Failed loading: ' . $class_string . ' => ' . $t->getMessage()); @@ -1757,14 +1757,9 @@ class Generate $this->dba->setTableArrayEntry($this->dba->getTableArray()[$key]['preset'], $key, 'value'); } } - if (is_array($this->reference_array)) { - if (!is_array($this->reference_array)) { - $this->reference_array = []; - } - reset($this->reference_array); - foreach ($this->reference_array as $key => $value) { - unset($this->reference_array[$key]['selected']); - } + reset($this->reference_array); + foreach ($this->reference_array as $key => $value) { + unset($this->reference_array[$key]['selected']); } $this->warning = 1; $this->msg = $this->l->__('Cleared for new Dataset!'); @@ -1787,20 +1782,15 @@ class Generate $this->dba->unsetTableArrayEntry($key, 'input_value'); } - if (is_array($this->reference_array)) { - // load each reference_table - if (!is_array($this->reference_array)) { - $this->reference_array = []; - } - reset($this->reference_array); - foreach ($this->reference_array as $key => $value) { - unset($this->reference_array[$key]['selected']); - $q = 'SELECT ' . $this->reference_array[$key]['other_table_pk'] - . ' FROM ' . $this->reference_array[$key]['table_name'] - . ' WHERE ' . $this->int_pk_name . ' = ' . $this->dba->getTableArray()[$this->int_pk_name]['value']; - while (is_array($res = $this->dba->dbReturn($q))) { - $this->reference_array[$key]['selected'][] = $res[$this->reference_array[$key]['other_table_pk']]; - } + // load each reference_table + reset($this->reference_array); + foreach ($this->reference_array as $key => $value) { + unset($this->reference_array[$key]['selected']); + $q = 'SELECT ' . $this->reference_array[$key]['other_table_pk'] + . ' FROM ' . $this->reference_array[$key]['table_name'] + . ' WHERE ' . $this->int_pk_name . ' = ' . $this->dba->getTableArray()[$this->int_pk_name]['value']; + while (is_array($res = $this->dba->dbReturn($q))) { + $this->reference_array[$key]['selected'][] = $res[$this->reference_array[$key]['other_table_pk']]; } } $this->warning = 1; @@ -1979,24 +1969,19 @@ class Generate // write the object $this->dba->dbWrite($addslashes, [], true); // write reference array (s) if necessary - if (is_array($this->reference_array)) { - if (!is_array($this->reference_array)) { - $this->reference_array = []; + reset($this->reference_array); + foreach ($this->reference_array as $reference_array) { + $q = 'DELETE FROM ' . $reference_array['table_name'] + . ' WHERE ' . $this->int_pk_name . ' = ' . $this->dba->getTableArray()[$this->int_pk_name]['value']; + $this->dba->dbExec($q); + $q = 'INSERT INTO ' . $reference_array['table_name'] + . ' (' . $reference_array['other_table_pk'] . ', ' . $this->int_pk_name . ') VALUES '; + for ($i = 0, $i_max = count($reference_array['selected']); $i < $i_max; $i++) { + $t_q = '(' . $reference_array['selected'][$i] . ', ' + . $this->dba->getTableArray()[$this->int_pk_name]['value'] . ')'; + $this->dba->dbExec($q . $t_q); } - reset($this->reference_array); - foreach ($this->reference_array as $reference_array) { - $q = 'DELETE FROM ' . $reference_array['table_name'] - . ' WHERE ' . $this->int_pk_name . ' = ' . $this->dba->getTableArray()[$this->int_pk_name]['value']; - $this->dba->dbExec($q); - $q = 'INSERT INTO ' . $reference_array['table_name'] - . ' (' . $reference_array['other_table_pk'] . ', ' . $this->int_pk_name . ') VALUES '; - for ($i = 0, $i_max = count($reference_array['selected']); $i < $i_max; $i++) { - $t_q = '(' . $reference_array['selected'][$i] . ', ' - . $this->dba->getTableArray()[$this->int_pk_name]['value'] . ')'; - $this->dba->dbExec($q . $t_q); - } - } // foreach reference arrays - } // if reference arrays + } // foreach reference arrays // write element list if (!empty($this->element_list)) { $type = []; @@ -2230,16 +2215,11 @@ class Generate public function formDeleteTableArray() { // remove any reference arrays - if (is_array($this->reference_array)) { - if (!is_array($this->reference_array)) { - $this->reference_array = []; - } - reset($this->reference_array); - foreach ($this->reference_array as $reference_array) { - $q = 'DELETE FROM ' . $reference_array['table_name'] - . ' WHERE ' . $this->int_pk_name . ' = ' . $this->dba->getTableArray()[$this->int_pk_name]['value']; - $this->dba->dbExec($q); - } + reset($this->reference_array); + foreach ($this->reference_array as $reference_array) { + $q = 'DELETE FROM ' . $reference_array['table_name'] + . ' WHERE ' . $this->int_pk_name . ' = ' . $this->dba->getTableArray()[$this->int_pk_name]['value']; + $this->dba->dbExec($q); } // remove any element list references if (!empty($this->element_list)) { diff --git a/www/lib/CoreLibs/Output/Image.php b/www/lib/CoreLibs/Output/Image.php index 9fd82619..aa449602 100644 --- a/www/lib/CoreLibs/Output/Image.php +++ b/www/lib/CoreLibs/Output/Image.php @@ -256,8 +256,8 @@ class Image } // check resize parameters if ($inc_width > $thumb_width || $inc_height > $thumb_height) { - $thumb_width_r = 0; - $thumb_height_r = 0; + $thumb_width_r = 1; + $thumb_height_r = 1; // we need to keep the aspect ration on longest side if ( ($inc_height > $inc_width && @@ -288,6 +288,12 @@ class Image !file_exists($thumbnail_write_path . $thumbnail) ) { // image, copy source image, offset in image, source x/y, new size, source image size + if ($thumb_width_r < 1) { + $thumb_width_r = 1; + } + if ($thumb_height_r < 1) { + $thumb_height_r = 1; + } $thumb = imagecreatetruecolor($thumb_width_r, $thumb_height_r); if ($thumb === false) { throw new \RuntimeException( @@ -380,9 +386,7 @@ class Image } } // add output path - if ($thumbnail !== false) { - $thumbnail = $thumbnail_web_path . $thumbnail; - } + $thumbnail = $thumbnail_web_path . $thumbnail; } elseif ($create_dummy === true) { // create dummy image in the thumbnail size // if one side is missing, use the other side to create a square @@ -399,10 +403,10 @@ class Image !file_exists($thumbnail_write_path . $thumbnail) ) { // if both are unset, set to 250 - if ($thumb_height == 0) { + if ($thumb_height < 1) { $thumb_height = 250; } - if ($thumb_width == 0) { + if ($thumb_width < 1) { $thumb_width = 250; } $thumb = imagecreatetruecolor($thumb_width, $thumb_height); diff --git a/www/lib/CoreLibs/UrlRequests/Curl.php b/www/lib/CoreLibs/UrlRequests/Curl.php index fcc8bc14..ced5129f 100644 --- a/www/lib/CoreLibs/UrlRequests/Curl.php +++ b/www/lib/CoreLibs/UrlRequests/Curl.php @@ -599,7 +599,7 @@ class Curl implements Interface\RequestsInterface // for post we set POST option if ($type == "post") { curl_setopt($handle, CURLOPT_POST, true); - } elseif (in_array($type, self::CUSTOM_REQUESTS)) { + } elseif (!empty($type) && in_array($type, self::CUSTOM_REQUESTS)) { curl_setopt($handle, CURLOPT_CUSTOMREQUEST, strtoupper($type)); } // set body data if not null, will send empty [] for empty data @@ -700,12 +700,12 @@ class Curl implements Interface\RequestsInterface // if we have a timeout signal if (!empty($this->config['timeout'])) { $timeout_requires_no_signal = $this->config['timeout'] < 1; - curl_setopt($handle, CURLOPT_TIMEOUT_MS, $this->config['timeout'] * 1000); + curl_setopt($handle, CURLOPT_TIMEOUT_MS, (int)round($this->config['timeout'] * 1000)); } if (!empty($this->config['connection_timeout'])) { $timeout_requires_no_signal = $timeout_requires_no_signal || $this->config['connection_timeout'] < 1; - curl_setopt($handle, CURLOPT_CONNECTTIMEOUT_MS, $this->config['connection_timeout'] * 1000); + curl_setopt($handle, CURLOPT_CONNECTTIMEOUT_MS, (int)round($this->config['connection_timeout'] * 1000, 1)); } if ($timeout_requires_no_signal && strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') { curl_setopt($handle, CURLOPT_NOSIGNAL, true);