2121use function array_combine ;
2222use function array_diff ;
2323use function array_filter ;
24+ use function array_keys ;
2425use function array_map ;
2526use function array_merge ;
2627use function array_unique ;
2728use function array_values ;
29+ use function count ;
2830use function implode ;
2931use function in_array ;
3032use function is_array ;
3133use function is_string ;
3234use function json_encode ;
3335use function preg_match ;
36+ use function sort ;
3437
3538abstract class AbstractDMLQueryBuilder implements DMLQueryBuilderInterface
3639{
@@ -41,10 +44,6 @@ public function __construct(
4144 ) {
4245 }
4346
44- /**
45- * @psalm-param string[] $columns
46- * @psalm-suppress MixedArrayOffset
47- */
4847 public function batchInsert (string $ table , array $ columns , iterable |Generator $ rows , array &$ params = []): string
4948 {
5049 if (empty ($ rows )) {
@@ -65,7 +64,7 @@ public function batchInsert(string $table, array $columns, iterable|Generator $r
6564 $ placeholders = [];
6665 foreach ($ row as $ index => $ value ) {
6766 if (isset ($ columns [$ index ], $ mappedNames [$ columns [$ index ]], $ columnSchemas [$ mappedNames [$ columns [$ index ]]])) {
68- /** @var mixed $value */
67+ /** @psalm- var mixed $value */
6968 $ value = $ this ->getTypecastValue ($ value , $ columnSchemas [$ mappedNames [$ columns [$ index ]]]);
7069 }
7170
@@ -91,9 +90,6 @@ public function batchInsert(string $table, array $columns, iterable|Generator $r
9190 . ' ( ' . implode (', ' , $ columns ) . ') VALUES ' . implode (', ' , $ values );
9291 }
9392
94- /**
95- * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException
96- */
9793 public function delete (string $ table , array |string $ condition , array &$ params ): string
9894 {
9995 $ sql = 'DELETE FROM ' . $ this ->quoter ->quoteTableName ($ table );
@@ -117,38 +113,27 @@ public function insert(string $table, QueryInterface|array $columns, array &$par
117113 . (!empty ($ placeholders ) ? ' VALUES ( ' . implode (', ' , $ placeholders ) . ') ' : $ values );
118114 }
119115
120- /**
121- * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException
122- */
123116 public function insertWithReturningPks (string $ table , QueryInterface |array $ columns , array &$ params = []): string
124117 {
125118 throw new NotSupportedException (__METHOD__ . '() is not supported by this DBMS. ' );
126119 }
127120
128- /**
129- * @throws NotSupportedException
130- */
131121 public function resetSequence (string $ tableName , int |string |null $ value = null ): string
132122 {
133123 throw new NotSupportedException (__METHOD__ . '() is not supported by this DBMS. ' );
134124 }
135125
136- /**
137- * @psalm-suppress MixedArgument
138- */
139126 public function update (string $ table , array $ columns , array |string $ condition , array &$ params = []): string
140127 {
141128 /** @psalm-var string[] $lines */
142129 [$ lines , $ params ] = $ this ->prepareUpdateSets ($ table , $ columns , $ params );
143130 $ sql = 'UPDATE ' . $ this ->quoter ->quoteTableName ($ table ) . ' SET ' . implode (', ' , $ lines );
131+ /** @psalm-var array $params */
144132 $ where = $ this ->queryBuilder ->buildWhere ($ condition , $ params );
145133
146134 return $ where === '' ? $ sql : $ sql . ' ' . $ where ;
147135 }
148136
149- /**
150- * @throws NotSupportedException
151- */
152137 public function upsert (
153138 string $ table ,
154139 QueryInterface |array $ insertColumns ,
@@ -162,12 +147,15 @@ public function upsert(
162147 * Prepare select-subQuery and field names for INSERT INTO ... SELECT SQL statement.
163148 *
164149 * @param QueryInterface $columns Object, which represents select query.
165- * @param array $params the parameters to be bound to the generated SQL statement. These parameters will be included
150+ * @param array $params The parameters to be bound to the generated SQL statement. These parameters will be included
166151 * in the result with the additional parameters generated during the query building process.
167152 *
168- * @throws Exception|InvalidArgumentException|InvalidConfigException|NotSupportedException
153+ * @throws Exception
154+ * @throws InvalidArgumentException
155+ * @throws InvalidConfigException
156+ * @throws NotSupportedException
169157 *
170- * @return array array of column names, values and params.
158+ * @return array Array of column names, values and params.
171159 */
172160 protected function prepareInsertSelectSubQuery (QueryInterface $ columns , array $ params = []): array
173161 {
@@ -201,6 +189,14 @@ protected function prepareInsertSelectSubQuery(QueryInterface $columns, array $p
201189 return [$ names , $ values , $ params ];
202190 }
203191
192+ /**
193+ * Prepare column names and placeholders for INSERT SQL statement.
194+ *
195+ * @throws Exception
196+ * @throws InvalidConfigException
197+ * @throws InvalidArgumentException
198+ * @throws NotSupportedException
199+ */
204200 protected function prepareInsertValues (string $ table , array |QueryInterface $ columns , array $ params = []): array
205201 {
206202 $ tableSchema = $ this ->schema ->getTableSchema ($ table );
@@ -214,7 +210,7 @@ protected function prepareInsertValues(string $table, array|QueryInterface $colu
214210 } else {
215211 $ columns = $ this ->normalizeColumnNames ($ table , $ columns );
216212 /**
217- * @var mixed $value
213+ * @psalm- var mixed $value
218214 * @psalm-var array<string, mixed> $columns
219215 */
220216 foreach ($ columns as $ name => $ value ) {
@@ -233,14 +229,19 @@ protected function prepareInsertValues(string $table, array|QueryInterface $colu
233229 return [$ names , $ placeholders , $ values , $ params ];
234230 }
235231
232+ /**
233+ * Prepare column names and placeholders for UPDATE SQL statement.
234+ *
235+ * @throws Exception
236+ * @throws InvalidConfigException
237+ * @throws InvalidArgumentException
238+ * @throws NotSupportedException
239+ */
236240 protected function prepareUpdateSets (string $ table , array $ columns , array $ params = []): array
237241 {
238242 $ tableSchema = $ this ->schema ->getTableSchema ($ table );
239-
240243 $ columnSchemas = $ tableSchema !== null ? $ tableSchema ->getColumns () : [];
241-
242244 $ sets = [];
243-
244245 $ columns = $ this ->normalizeColumnNames ($ table , $ columns );
245246
246247 /**
@@ -263,9 +264,15 @@ protected function prepareUpdateSets(string $table, array $columns, array $param
263264 }
264265
265266 /**
266- * @psalm-param Constraint[] $constraints
267+ * Prepare column names and placeholders for UPSERT SQL statement.
267268 *
268- * @throws Exception|InvalidArgumentException|InvalidConfigException|JsonException|NotSupportedException
269+ * @throws Exception
270+ * @throws InvalidArgumentException
271+ * @throws InvalidConfigException
272+ * @throws JsonException
273+ * @throws NotSupportedException
274+ *
275+ * @psalm-param Constraint[] $constraints
269276 */
270277 protected function prepareUpsertColumns (
271278 string $ table ,
@@ -313,19 +320,19 @@ protected function prepareUpsertColumns(
313320 *
314321 * The column list will be unique by column names.
315322 *
316- * @param string $name table name. The table name may contain schema name if any. Do not quote the table name.
317- * @param string[] $columns source column list.
318- * @param Constraint[] $constraints this parameter optionally receives a matched constraint list. The constraints
323+ * @param string $name The table name, may contain schema name if any. Do not quote the table name.
324+ * @param string[] $columns Source column list.
325+ * @param array $constraints This parameter optionally receives a matched constraint list. The constraints
319326 * will be unique by their column names.
320327 *
321328 * @throws JsonException
322329 *
323- * @return array column list.
324- * @psalm-suppress ReferenceConstraintViolation
330+ * @return array The column list.
331+ *
332+ * @psalm-param Constraint[] $constraints
325333 */
326334 private function getTableUniqueColumnNames (string $ name , array $ columns , array &$ constraints = []): array
327335 {
328- $ constraints = [];
329336 $ primaryKey = $ this ->schema ->getTablePrimaryKey ($ name );
330337
331338 if ($ primaryKey !== null ) {
@@ -343,7 +350,11 @@ private function getTableUniqueColumnNames(string $name, array $columns, array &
343350
344351 $ constraints = array_merge ($ constraints , $ this ->schema ->getTableUniques ($ name ));
345352
346- /** Remove duplicates */
353+ /**
354+ * Remove duplicates
355+ *
356+ * @psalm-var Constraint[] $constraints
357+ */
347358 $ constraints = array_combine (
348359 array_map (
349360 static function (Constraint $ constraint ) {
@@ -386,10 +397,13 @@ static function (Constraint $constraint) use ($quoter, $columns, &$columnNames)
386397 )
387398 );
388399
389- /** @psalm-var array $columnNames */
400+ /** @psalm-var Constraint[] $columnNames */
390401 return array_unique ($ columnNames );
391402 }
392403
404+ /**
405+ * @return mixed The typecast value of the given column.
406+ */
393407 protected function getTypecastValue (mixed $ value , ColumnSchemaInterface $ columnSchema = null ): mixed
394408 {
395409 if ($ columnSchema ) {
@@ -400,31 +414,30 @@ protected function getTypecastValue(mixed $value, ColumnSchemaInterface $columnS
400414 }
401415
402416 /**
403- * Normalizes column names
417+ * Normalizes the column names for the given table.
404418 *
405- * @param string $table the table that data will be saved into.
406- * @param array $columns the column data (name => value) to be saved into the table or instance of
407- * {@see QueryInterface} to perform INSERT INTO ... SELECT SQL statement. Passing of
408- * {@see QueryInterface}.
419+ * @param string $table The table that data will be saved into.
420+ * @param array $columns The column data (name => value) to be saved into the table or instance of
421+ * {@see QueryInterface} to perform INSERT INTO ... SELECT SQL statement. Passing of {@see QueryInterface}.
409422 *
410- * @return array normalized columns .
423+ * @return array The normalized column names (name => value) .
411424 */
412425 protected function normalizeColumnNames (string $ table , array $ columns ): array
413426 {
414427 /** @var string[] $columnsList */
415428 $ columnsList = array_keys ($ columns );
416429 $ mappedNames = $ this ->getNormalizeColumnNames ($ table , $ columnsList );
417430
418- /** @psalm-var mixed[] $normalizedColumns */
431+ /** @psalm-var array $normalizedColumns */
419432 $ normalizedColumns = [];
420433
421434 /**
422- * @var string $name
423- * @var mixed $value
435+ * @psalm- var string $name
436+ * @psalm- var mixed $value
424437 */
425438 foreach ($ columns as $ name => $ value ) {
426439 $ mappedName = $ mappedNames [$ name ] ?? $ name ;
427- /** @psalm-suppress MixedAssignment */
440+ /** @psalm-var mixed */
428441 $ normalizedColumns [$ mappedName ] = $ value ;
429442 }
430443
@@ -434,10 +447,11 @@ protected function normalizeColumnNames(string $table, array $columns): array
434447 /**
435448 * Get map of normalized columns
436449 *
437- * @param string $table
438- * @param string[] $columns
450+ * @param string $table The table that data will be saved into.
451+ * @param string[] $columns The column data (name => value) to be saved into the table or instance of
452+ * {@see QueryInterface} to perform INSERT INTO ... SELECT SQL statement. Passing of {@see QueryInterface}.
439453 *
440- * @return string[]
454+ * @return string[] Map of normalized columns.
441455 */
442456 protected function getNormalizeColumnNames (string $ table , array $ columns ): array
443457 {
0 commit comments