Ignore smarty4 checks, phan/phpstan fixes

OPEN: resource to resource/object/false check in phpstan
This commit is contained in:
Clemens Schwaighofer
2022-01-20 11:03:58 +09:00
parent f4e72dd7b1
commit 2a697817fd
7 changed files with 95 additions and 46 deletions

View File

@@ -111,6 +111,9 @@ return [
// symlink ignore
'www/lib/smarty-3.1.30/SmartyBC.class.php',
'www/lib/htmlMimeMail-2.5.1/HtmlMimeMailCreate.php',
// current not checking smarty4 test file
'www/lib/CoreLibs/Template/SmartyExtend4.php',
'www/admin/class_test.smarty4.php',
],
// what not to show as problem

View File

@@ -43,6 +43,9 @@ parameters:
- www/lib/smarty-*/
# ignore composer
- www/vendor
# temp ignore smarty 4
- www/lib/CoreLibs/Template/SmartyExtend4.php
- www/admin/class_test.smarty4.php
# ignore errores with
ignoreErrors:
# this is ignored for now

View File

@@ -103,7 +103,7 @@ print "CONV ENCODING: $_string: ".$basic->convertEncoding($_string, 'ISO-2022-JP
print "D/__MBMIMEENCODE: ".$basic->__mbMimeEncode('Some Text', 'UTF-8')."<br>"; */
// error message
print $basic->printErrorMsg();
print $log->printErrorMsg();
print "</body></html>";

View File

@@ -52,9 +52,9 @@ $log = new CoreLibs\Debug\Logging([
'file_id' => LOG_FILE_ID,
'print_file_date' => true,
'per_class' => true,
'debug_all' => $DEBUG_ALL,
'echo_all' => $ECHO_ALL,
'print_all' => $PRINT_ALL,
'debug_all' => $DEBUG_ALL ?? false,
'echo_all' => $ECHO_ALL ?? false,
'print_all' => $PRINT_ALL ?? false,
]);
// automatic hide for DEBUG messages on live server
// can be overridden when setting DEBUG_ALL_OVERRIDE on top of the script

View File

@@ -252,6 +252,9 @@ declare(strict_types=1);
namespace CoreLibs\DB;
// this is for running in php 8.0 phan
/** @#phan-file-suppress PhanUndeclaredTypeProperty,PhanUndeclaredTypeParameter,PhanUndeclaredTypeReturnType */
class IO
{
// 0: normal read, store in cache
@@ -276,7 +279,7 @@ class IO
public $query; // the query string at the moment
// only inside
// basic vars
/** @var PgSql\Connection|resource|bool|int|null */
/** @var object|resource|bool|int|null */ // replace object with PgSql\Connection|
private $dbh; // the dbh handler, if disconnected by command is null, bool:false/int:-1 on error,
/** @var bool */
public $db_debug = false; // DB_DEBUG ... (if set prints out debug msgs)
@@ -303,7 +306,7 @@ class IO
/** @var array<mixed,mixed> */
public $cursor_ext; // hash of hashes
// per query vars
/** @var PgSql\Result|resource */
/** @var object|resource|bool */ // replace object with PgSql\Result
public $cursor; // actual cursor (DBH)
/** @var int */
public $num_rows; // how many rows have been found
@@ -418,6 +421,7 @@ class IO
$this->error_string['31'] = 'Could not fetch PK after query insert';
$this->error_string['32'] = 'Multiple PK return as array';
$this->error_string['33'] = 'Returning PK was not found';
$this->error_string['34'] = 'Cursor invalid for fetch PK after query insert';
$this->error_string['40'] = 'Query async call failed.';
$this->error_string['41'] = 'Connection is busy with a different query. Cannot execute.';
$this->error_string['42'] = 'Cannot check for async query, none has been started yet.';
@@ -624,10 +628,11 @@ class IO
* if error_id set, writes long error string into error_msg
* NOTE:
* needed to make public so it can be called from DB.Array.IO too
* @param PgSql\Result|resource|bool $cursor current cursor for pg_result_error, mysql uses dbh,
* pg_last_error too, but pg_result_error is more accurate
* @param string $msg optional message
* @return void has no return
* @param object|resource|bool $cursor current cursor for pg_result_error,
* pg_last_error too, but pg_result_error
* is more accurate (PgSql\Result)
* @param string $msg optional message
* @return void has no return
*/
public function __dbError($cursor = false, string $msg = ''): void
{
@@ -904,11 +909,11 @@ class IO
* insert_id_ext [DEPRECATED, all in insert_id_arr]
* - holds all returning as array
* TODO: Only use insert_id_arr and use functions to get ok array or single
* @param boolean $returning_id
* @param string $query
* @param string|null $pk_name
* @param PgSql\Result|resource $cursor
* @param string|null $stm_name If not null, is dbExecutre run
* @param boolean $returning_id
* @param string $query
* @param string|null $pk_name
* @param object|resource|bool $cursor (PgSql\Result)
* @param string|null $stm_name If not null, is dbExecutre run
* @return void
*/
private function __dbSetInsertId(
@@ -926,6 +931,22 @@ class IO
$this->insert_id_arr = [];
// set the primary key name
$this->insert_id_pk_name = $pk_name ?? '';
// abort if cursor is empty
if ($cursor === false) {
// failed to get insert id
$this->warning_id = 34;
if ($stm_name === null) {
$this->__dbError($cursor, '[dbExec]');
} else {
$this->__dbError();
$this->__dbDebug(
'db',
$stm_name . ': CURSOR is null',
'DB_WARNING'
);
}
return;
}
// set insert_id
// if we do not have a returning
// we try to get it via the primary key and another select
@@ -1518,7 +1539,7 @@ class IO
* if pk name is table name and _id, pk_name
* is not needed to be set
* if NULL is given here, no RETURNING will be auto added
* @return PgSql\Result|resource|false cursor for this query or false on error
* @return object|resource|bool cursor for this query or false on error (PgSql\Result)
*/
public function dbExec(string $query = '', string $pk_name = '')
{
@@ -1576,8 +1597,9 @@ class IO
/**
* checks a previous async query and returns data if finished
* NEEDS : dbExecAsync
* @return bool|PgSql\Result|resource cursor resource if the query is still running,
* false if an error occured or cursor of that query
* @return bool|object|resource cursor resource if the query is still running,
* false if an error occured or cursor of that query
* (PgSql\Result)
*/
public function dbCheckAsync()
{
@@ -1606,7 +1628,7 @@ class IO
$this->__dbDebug(
'db',
'<span style="color: red;"><b>DB-Error</b> No async query '
. 'has been started yet.</span>',
. 'has beedbFetchArrayn started yet.</span>',
'DB_ERROR'
);
return false;
@@ -1615,12 +1637,15 @@ class IO
/**
* executes a cursor and returns the data, if no more data 0 will be returned
* @param PgSql\Result|resource|false $cursor the cursor from db_exec or
* pg_query/pg_exec/mysql_query
* if not set will use internal cursor,
* if not found, stops with 0 (error)
* @param bool $assoc_only false is default, if true only assoc rows
* @return array<mixed>|bool row array or false on error
* @param object|resource|bool $cursor the cursor from db_exec or
* pg_query/pg_exec/mysql_query
* if not set will use internal cursor,
* if not found, stops with 0 (error)
* (PgSql\Result)
* @param bool $assoc_only false is default,
* if true only named rows,
* not numbered index rows
* @return array<mixed>|bool row array or false on error
*/
public function dbFetchArray($cursor = false, bool $assoc_only = false)
{
@@ -1752,7 +1777,8 @@ class IO
* @param string $stm_name statement name
* @param string $query queryt string to run
* @param string $pk_name optional primary key
* @return bool|PgSql\Result|resource false on error, true on warning or result on full ok
* @return bool|object|resource false on error, true on warning or
* result on full ok (PgSql\Result)
*/
public function dbPrepare(string $stm_name, string $query, string $pk_name = '')
{

View File

@@ -47,12 +47,14 @@ declare(strict_types=1);
namespace CoreLibs\DB\SQL;
/** @#phan-file-suppress PhanUndeclaredTypeProperty,PhanUndeclaredTypeParameter,PhanUndeclaredTypeReturnType */
class PgSQL
{
/** @var string */
private $last_error_query;
// NOTE for PHP 8.1 this is no longer a resource
/** @var PgSql\Connection|resource|bool */
/** @var object|resource|bool */ // replace object with PgSql\Connection
private $dbh;
/**
@@ -78,7 +80,7 @@ class PgSQL
/**
* wrapper for gp_query, catches error and stores it in class var
* @param string $query query string
* @return PgSql\Result|resource|bool query result
* @return object|resource|bool query result (PgSql\Result)
*/
public function __dbQuery(string $query)
{
@@ -110,7 +112,7 @@ class PgSQL
/**
* wrapper for pg_get_result
* @return PgSql\Result|resource|bool resource handler or false for error
* @return object|resource|bool resource handler or false for error (PgSql\Result)
*/
public function __dbGetResult()
{
@@ -147,7 +149,7 @@ class PgSQL
* wrapper for pg_prepare
* @param string $name statement name
* @param string $query query string
* @return PgSql\Result|resource|bool prepare statement handler or false for error
* @return object|resource|bool prepare statement handler or false for error (PgSql\Result)
*/
public function __dbPrepare(string $name, string $query)
{
@@ -165,7 +167,7 @@ class PgSQL
* wrapper for pg_execute for running a prepared statement
* @param string $name statement name
* @param array<mixed> $data data array
* @return PgSql\Result|resource|bool returns status or false for error
* @return object|resource|bool returns status or false for error (PgSql\Result)
*/
public function __dbExecute(string $name, array $data)
{
@@ -181,44 +183,56 @@ class PgSQL
/**
* wrapper for pg_num_rows
* @param PgSql\Result|resource $cursor cursor PgSql\Result (former resource)
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
* @return int number of rows, -1 on error
*/
public function __dbNumRows($cursor): int
{
if (!$cursor) {
return -1;
}
return pg_num_rows($cursor);
}
/**
* wrapper for pg_num_fields
* @param PgSql\Result|resource $cursor cursor PgSql\Result (former resource)
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
* @return int number for fields in result, -1 on error
*/
public function __dbNumFields($cursor): int
{
if (!$cursor) {
return -1;
}
return pg_num_fields($cursor);
}
/**
* wrapper for pg_field_name
* @param PgSql\Result|resource $cursor cursor PgSql\Result (former resource)
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
* @param int $i field position
* @return string|bool name or false on error
*/
public function __dbFieldName($cursor, $i)
{
if (!$cursor) {
return false;
}
return pg_field_name($cursor, $i);
}
/**
* wrapper for pg_fetch_array
* if through/true false, use __dbResultType(true)
* @param PgSql\Result|resource $cursor cursor PgSql\Result (former resource)
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
* @param int $result_type result type as int number
* @return array<mixed>|bool array result data or false on end/error
*/
public function __dbFetchArray($cursor, int $result_type = PGSQL_BOTH)
{
if (!$cursor) {
return false;
}
// result type is passed on as is [should be checked]
return pg_fetch_array($cursor, null, $result_type);
}
@@ -239,21 +253,27 @@ class PgSQL
/**
* wrapper for pg_fetch_all
* @param PgSql\Result|resource $cursor cursor PgSql\Result (former resource)
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
* @return array<mixed>|bool data array or false for end/error
*/
public function __dbFetchAll($cursor)
{
if (!$cursor) {
return false;
}
return pg_fetch_all($cursor);
}
/**
* wrapper for pg_affected_rows
* @param PgSql\Result|resource $cursor cursor PgSql\Result (former resource)
* @return int affected rows, 0 for none
* @param object|resource|bool $cursor cursor PgSql\Result (former resource)
* @return int affected rows, 0 for none, -1 for error
*/
public function __dbAffectedRows($cursor): int
{
if (!$cursor) {
return -1;
}
return pg_affected_rows($cursor);
}
@@ -292,10 +312,6 @@ class PgSQL
$q = "SELECT CURRVAL('$seq') AS insert_id";
// I have to do manually or I overwrite the original insert internal vars ...
if ($q = $this->__dbQuery($q)) {
// abort if this is not an resource
if ($q === false) {
return false;
}
if (is_array($res = $this->__dbFetchArray($q))) {
list($id) = $res;
} else {
@@ -379,7 +395,7 @@ class PgSQL
* @param string $db_name databse name
* @param integer $db_port port (int, 5432 is default)
* @param string $db_ssl SSL (allow is default)
* @return PgSql\Connection|resource|bool db handler PgSql\Connection or false on error
* @return object|resource|bool db handler PgSql\Connection or false on error
*/
public function __dbConnect(
string $db_host,
@@ -404,10 +420,11 @@ class PgSQL
/**
* reads the last error for this cursor and returns
* html formatted string with error name
* @param null|PgSql\Result|resource $cursor cursor PgSql\Result (former resource) or null
* @param bool|object|resource $cursor cursor PgSql\Result (former resource)
* or null
* @return string error string
*/
public function __dbPrintError($cursor = null): string
public function __dbPrintError($cursor = false): string
{
if ($this->dbh === false) {
return '';

View File

@@ -371,7 +371,7 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
$this->load_query = $config_array['load_query'];
}
$this->archive_pk_name = 'a_' . $this->pk_name;
$this->col_name = str_replace('_id', '', $this->pk_name ?? '');
$this->col_name = str_replace('_id', '', $this->pk_name);
$this->int_pk_name = $this->pk_name;
// check if reference_arrays are given and proceed them
if (isset($config_array['reference_arrays']) && is_array($config_array['reference_arrays'])) {