phpstan fixes for DB IO, add array key modify method that keeps value

This commit is contained in:
Clemens Schwaighofer
2025-04-22 11:11:55 +09:00
parent bacd31d0e3
commit e0003beae2
4 changed files with 151 additions and 6 deletions

View File

@@ -551,6 +551,36 @@ class ArrayHandler
ARRAY_FILTER_USE_KEY
);
}
/**
* Modifieds the key of an array with a prefix and/or suffix and returns it with the original value
* does not change order in array
*
* @param array<string|int,mixed> $in_array
* @param string $key_mod_prefix [default=''] key prefix string
* @param string $key_mod_suffix [default=''] key suffix string
* @return array<string|int,mixed>
*/
public static function arrayModifyKey(
array $in_array,
string $key_mod_prefix = '',
string $key_mod_suffix = ''
): array {
// skip if array is empty or neither prefix or suffix are set
if (
$in_array == [] ||
($key_mod_prefix == '' && $key_mod_suffix == '')
) {
return $in_array;
}
return array_combine(
array_map(
fn($key) => $key_mod_prefix . $key . $key_mod_suffix,
array_keys($in_array)
),
array_values($in_array)
);
}
}
// __END__

View File

@@ -39,9 +39,9 @@ class ArrayIO extends \CoreLibs\DB\IO
{
// main calss variables
/** @var array<mixed> */
private array $table_array; // the array from the table to work on
private array $table_array = []; // the array from the table to work on
/** @var string */
private string $table_name; // the table_name
private string $table_name = ''; // the table_name
/** @var string */
private string $pk_name = ''; // the primary key from this table
/** @var int|string|null */
@@ -127,9 +127,9 @@ class ArrayIO extends \CoreLibs\DB\IO
public function getTableArray(bool $reset = false): array
{
if (!$reset) {
return $this->table_array ?? [];
return $this->table_array;
}
$table_array = $this->table_array ?? [];
$table_array = $this->table_array;
reset($table_array);
return $table_array;
}
@@ -194,7 +194,7 @@ class ArrayIO extends \CoreLibs\DB\IO
*/
public function getTableName(): string
{
return $this->table_name ?? '';
return $this->table_name;
}
/**

View File

@@ -2544,7 +2544,10 @@ class IO
} // only go if NO cursor exists
// if cursor exists ...
if ($this->cursor_ext[$query_hash]['cursor']) {
if (
$this->cursor_ext[$query_hash]['cursor'] instanceof \PgSql\Result ||
$this->cursor_ext[$query_hash]['cursor'] == 1
) {
if ($first_call === true) {
$this->cursor_ext[$query_hash]['log'][] = 'First call';
// count the rows returned (if select)

View File

@@ -1286,6 +1286,118 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase
)
);
}
/**
* provider for arrayModifyKey
*
* @return array<string,array<mixed>>
*/
public function providerArrayModifyKey(): array
{
return [
'prefix and suffix add' => [
'array' => [
'a' => 'foo',
'b' => 'bar',
'c' => 'foobar',
],
'prefix' => 'Prefix: ',
'suffix' => '.suffix',
'expected' => [
'Prefix: a.suffix' => 'foo',
'Prefix: b.suffix' => 'bar',
'Prefix: c.suffix' => 'foobar',
],
],
'prefix add only' => [
'array' => [
'a' => 'foo',
'b' => 'bar',
'c' => 'foobar',
],
'prefix' => 'Prefix: ',
'suffix' => '',
'expected' => [
'Prefix: a' => 'foo',
'Prefix: b' => 'bar',
'Prefix: c' => 'foobar',
],
],
'suffix add only' => [
'array' => [
'a' => 'foo',
'b' => 'bar',
'c' => 'foobar',
],
'prefix' => '',
'suffix' => '.suffix',
'expected' => [
'a.suffix' => 'foo',
'b.suffix' => 'bar',
'c.suffix' => 'foobar',
],
],
'empty array' => [
'array' => [],
'prefix' => '',
'suffix' => '.suffix',
'expected' => [],
],
'no suffix or prefix' => [
'array' => [
'a' => 'foo',
'b' => 'bar',
'c' => 'foobar',
],
'prefix' => '',
'suffix' => '',
'expected' => [
'a' => 'foo',
'b' => 'bar',
'c' => 'foobar',
],
],
'integer index mixed' => [
'array' => [
'a' => 'foo',
'b' => 'bar',
3 => 'foobar',
],
'prefix' => '',
'suffix' => '.suffix',
'expected' => [
'a.suffix' => 'foo',
'b.suffix' => 'bar',
'3.suffix' => 'foobar',
],
]
];
}
/**
* Undocumented function
*
* @covers ::arrayModifyKey
* @dataProvider providerArrayModifyKey
* @testdox arrayModifyKey check that key is correctly modified with $key_mod_prefix and $key_mod_suffix [$_dataName]
*
* @param array<mixed> $in_array
* @param string $key_mod_prefix
* @param string $key_mod_suffix
* @param array<mixed> $expected
* @return void
*/
public function testArrayModifyKey(
array $in_array,
string $key_mod_prefix,
string $key_mod_suffix,
array $expected
): void {
$this->assertEquals(
\CoreLibs\Combined\ArrayHandler::arrayModifyKey($in_array, $key_mod_prefix, $key_mod_suffix),
$expected
);
}
}
// __END__