Skip to content

Commit

Permalink
PHPStan fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
compositephp committed Jun 18, 2023
1 parent e2264b6 commit e4543eb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
12 changes: 6 additions & 6 deletions src/AbstractTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ public function saveMany(array $entities): void
tableName: $this->getTableName(),
rows: $chunk,
);
if ($multiInsert->sql) {
$stmt = $this->getConnection()->prepare($multiInsert->sql);
$stmt->executeQuery($multiInsert->parameters);
if ($multiInsert->getSql()) {
$stmt = $this->getConnection()->prepare($multiInsert->getSql());
$stmt->executeQuery($multiInsert->getParameters());
}
}
}
Expand Down Expand Up @@ -223,7 +223,7 @@ protected function findOneInternal(array $where): ?array

/**
* @param array<int|string|array<string,mixed>> $pkList
* @return array
* @return array<array<string, mixed>>
* @throws DbException
* @throws EntityException
* @throws \Doctrine\DBAL\Exception
Expand Down Expand Up @@ -253,7 +253,7 @@ protected function findMultiInternal(array $pkList): array
$query = $this->select();
$expressions = [];
foreach ($pkList as $i => $pkArray) {
if (!is_array($pkArray) || array_is_list($pkArray)) {
if (!is_array($pkArray)) {
throw new DbException('For tables with composite keys, input array must consist associative arrays');
}
$pkOrExpr = [];
Expand All @@ -276,7 +276,7 @@ protected function findMultiInternal(array $pkList): array
/**
* @param array<string, mixed> $whereParams
* @param array<string, string>|string $orderBy
* @return array<string, mixed>
* @return list<array<string,mixed>>
* @throws \Doctrine\DBAL\Exception
*/
protected function findAllInternal(
Expand Down
36 changes: 25 additions & 11 deletions src/MultiQuery/MultiInsert.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,47 @@

class MultiInsert
{
public readonly string $sql;
public readonly array $parameters;
private string $sql = '';
/** @var array<string, mixed> */
private array $parameters = [];

/**
* @param string $tableName
* @param list<array<string, mixed>> $rows
*/
public function __construct(string $tableName, array $rows) {
if (!$rows) {
$this->sql = '';
$this->parameters = [];
return;
}
$firstRow = reset($rows);
$columnNames = array_map(fn ($columnName) => "`$columnName`", array_keys($firstRow));
$sql = "INSERT INTO `$tableName` (" . implode(', ', $columnNames) . ") VALUES ";
$valuesSql = $parameters = [];
$columnNames = array_map(fn($columnName) => "`$columnName`", array_keys($firstRow));
$this->sql = "INSERT INTO `$tableName` (" . implode(', ', $columnNames) . ") VALUES ";
$valuesSql = [];

$index = 0;
foreach ($rows as $row) {
$valuePlaceholder = [];
foreach ($row as $column => $value) {
$valuePlaceholder[] = ":$column$index";
$parameters["$column$index"] = $value;
$this->parameters["$column$index"] = $value;
}
$valuesSql[] = '(' . implode(', ', $valuePlaceholder) . ')';
$index++;
}

$sql .= implode(', ', $valuesSql);
$this->sql = $sql . ';';
$this->parameters = $parameters;
$this->sql .= implode(', ', $valuesSql) . ';';
}

public function getSql(): string
{
return $this->sql;
}

/**
* @return array<string, mixed>
*/
public function getParameters(): array
{
return $this->parameters;
}
}
1 change: 1 addition & 0 deletions src/TableConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

class TableConfig
{
/** @var array<class-string, true> */
private readonly array $entityTraits;

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/MultiQuery/MultiInsertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public function test_multiInsertQuery($tableName, $rows, $expectedSql, $expected
{
$multiInserter = new MultiInsert($tableName, $rows);

$this->assertEquals($expectedSql, $multiInserter->sql);
$this->assertEquals($expectedParameters, $multiInserter->parameters);
$this->assertEquals($expectedSql, $multiInserter->getSql());
$this->assertEquals($expectedParameters, $multiInserter->getParameters());
}

public static function multiInsertQuery_dataProvider()
Expand Down

0 comments on commit e4543eb

Please sign in to comment.