Skip to content

Commit c995d4f

Browse files
Merge pull request #19051 from kamil-tekiela/Refactor-import-8
Small refactoring of ImportCsv.php
2 parents a28cd35 + 8abb7d0 commit c995d4f

2 files changed

Lines changed: 19 additions & 45 deletions

File tree

psalm-baseline.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8623,7 +8623,6 @@
86238623
<MixedAssignment>
86248624
<code><![CDATA[$GLOBALS['csv_new_line']]]></code>
86258625
<code><![CDATA[$GLOBALS['errorUrl']]]></code>
8626-
<code><![CDATA[$fields[]]]></code>
86278626
</MixedAssignment>
86288627
<PossiblyInvalidCast>
86298628
<code><![CDATA[$_REQUEST['csv_new_db_name']]]></code>

src/Plugins/Import/ImportCsv.php

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@
2525
use Webmozart\Assert\Assert;
2626

2727
use function __;
28+
use function array_map;
2829
use function array_pad;
2930
use function array_shift;
3031
use function count;
32+
use function in_array;
33+
use function max;
3134
use function mb_strlen;
3235
use function mb_substr;
3336
use function pathinfo;
@@ -192,8 +195,6 @@ public function doImport(File|null $importHandle = null): array
192195
$GLOBALS['csv_columns'],
193196
);
194197

195-
$requiredFields = count($fields);
196-
197198
$sqlStatements = [];
198199

199200
// Defaults for parser
@@ -221,12 +222,8 @@ public function doImport(File|null $importHandle = null): array
221222
$maxLinesConstraint++;
222223
}
223224

224-
$tempRow = [];
225225
$rows = [];
226-
$colNames = [];
227-
228226
$buffer = '';
229-
$colCount = 0;
230227
$maxCols = 0;
231228
$csvTerminatedLen = mb_strlen($GLOBALS['csv_terminated']);
232229
$dbi = DatabaseInterface::getInstance();
@@ -246,7 +243,7 @@ public function doImport(File|null $importHandle = null): array
246243
// Force a trailing new line at EOF to prevent parsing problems
247244
if (ImportSettings::$finished && $buffer) {
248245
$finalch = mb_substr($buffer, -1);
249-
if ($GLOBALS['csv_new_line'] === 'auto' && $finalch != "\r" && $finalch != "\n") {
246+
if ($GLOBALS['csv_new_line'] === 'auto' && $finalch !== "\r" && $finalch !== "\n") {
250247
$buffer .= "\n";
251248
} elseif ($GLOBALS['csv_new_line'] !== 'auto' && $finalch != $GLOBALS['csv_new_line']) {
252249
$buffer .= $GLOBALS['csv_new_line'];
@@ -337,7 +334,7 @@ public function doImport(File|null $importHandle = null): array
337334
&& ! ($ch == $GLOBALS['csv_terminated']
338335
|| $ch == $GLOBALS['csv_new_line']
339336
|| ($GLOBALS['csv_new_line'] === 'auto'
340-
&& ($ch == "\r" || $ch == "\n"))))
337+
&& ($ch === "\r" || $ch === "\n"))))
341338
) {
342339
if ($ch == $GLOBALS['csv_escaped']) {
343340
if ($i === $len - 1) {
@@ -357,7 +354,7 @@ public function doImport(File|null $importHandle = null): array
357354
&& ($ch == $GLOBALS['csv_terminated']
358355
|| $ch == $GLOBALS['csv_new_line']
359356
|| ($GLOBALS['csv_new_line'] === 'auto'
360-
&& ($ch == "\r" || $ch == "\n")))
357+
&& ($ch === "\r" || $ch === "\n")))
361358
) {
362359
break;
363360
}
@@ -422,7 +419,7 @@ public function doImport(File|null $importHandle = null): array
422419
// Are we at the end?
423420
if (
424421
$ch == $GLOBALS['csv_new_line']
425-
|| ($GLOBALS['csv_new_line'] === 'auto' && ($ch == "\r" || $ch == "\n"))
422+
|| ($GLOBALS['csv_new_line'] === 'auto' && ($ch === "\r" || $ch === "\n"))
426423
|| (ImportSettings::$finished && $i === $len - 1)
427424
) {
428425
$csvFinish = true;
@@ -456,17 +453,17 @@ public function doImport(File|null $importHandle = null): array
456453
if (
457454
! $csvFinish
458455
&& $ch != $GLOBALS['csv_new_line']
459-
&& ($GLOBALS['csv_new_line'] !== 'auto' || ($ch != "\r" && $ch != "\n"))
456+
&& ($GLOBALS['csv_new_line'] !== 'auto' || ($ch !== "\r" && $ch !== "\n"))
460457
) {
461458
continue;
462459
}
463460

464-
if ($GLOBALS['csv_new_line'] === 'auto' && $ch == "\r") { // Handle "\r\n"
461+
if ($GLOBALS['csv_new_line'] === 'auto' && $ch === "\r") { // Handle "\r\n"
465462
if ($i >= ($len - 2) && ! ImportSettings::$finished) {
466463
break; // We need more data to decide new line
467464
}
468465

469-
if (mb_substr($buffer, $i + 1, 1) == "\n") {
466+
if (mb_substr($buffer, $i + 1, 1) === "\n") {
470467
$i++;
471468
}
472469
}
@@ -478,22 +475,11 @@ public function doImport(File|null $importHandle = null): array
478475
}
479476

480477
if ($this->analyze) {
481-
foreach ($values as $val) {
482-
$tempRow[] = $val;
483-
++$colCount;
484-
}
485-
486-
if ($colCount > $maxCols) {
487-
$maxCols = $colCount;
488-
}
489-
490-
$colCount = 0;
491-
492-
$rows[] = $tempRow;
493-
$tempRow = [];
478+
$maxCols = max($maxCols, count($values));
479+
$rows[] = $values;
494480
} else {
495481
// Do we have correct count of values?
496-
if (count($values) !== $requiredFields) {
482+
if (count($values) !== count($fields)) {
497483
// Hack for excel
498484
if ($values[count($values) - 1] !== ';') {
499485
$GLOBALS['message'] = Message::error(
@@ -530,8 +516,7 @@ public function doImport(File|null $importHandle = null): array
530516
$sql .= ' ON DUPLICATE KEY UPDATE ';
531517
foreach ($fields as $field) {
532518
$fieldName = Util::backquote($field);
533-
$sql .= $fieldName . ' = VALUES(' . $fieldName
534-
. '), ';
519+
$sql .= $fieldName . ' = VALUES(' . $fieldName . '), ';
535520
}
536521

537522
$sql = rtrim($sql, ', ');
@@ -570,6 +555,7 @@ public function doImport(File|null $importHandle = null): array
570555
$rows[$i] = array_pad($row, $maxCols, 'NULL');
571556
}
572557

558+
$colNames = [];
573559
/* Remove the first row if it contains the column names */
574560
if (isset($_REQUEST['csv_col_names'])) {
575561
$colNames = array_shift($rows);
@@ -611,7 +597,7 @@ public function doImport(File|null $importHandle = null): array
611597
// Commit any possible data in buffers
612598
$this->import->runQuery('', $sqlStatements);
613599

614-
if (count($values) == 0 || $GLOBALS['error'] !== false) {
600+
if ($values === [] || $GLOBALS['error'] !== false) {
615601
return $sqlStatements;
616602
}
617603

@@ -714,9 +700,7 @@ private function getColumnNames(array $columnNames, int $maxCols): array
714700
{
715701
if (isset($_REQUEST['csv_col_names'])) {
716702
// MySQL column names can't end with a space character.
717-
foreach ($columnNames as $key => $colName) {
718-
$columnNames[$key] = rtrim($colName);
719-
}
703+
$columnNames = array_map(rtrim(...), $columnNames);
720704
}
721705

722706
if (count($columnNames) !== $maxCols) {
@@ -767,15 +751,8 @@ private function getSqlTemplateAndRequiredFields(
767751

768752
/* Trim also `, if user already included backquoted fields */
769753
$val = trim($val, " \t\r\n\0\x0B`");
770-
$found = false;
771-
foreach ($tmpFields as $field) {
772-
if ($field === $val) {
773-
$found = true;
774-
break;
775-
}
776-
}
777754

778-
if (! $found) {
755+
if (! in_array($val, $tmpFields, true)) {
779756
$GLOBALS['message'] = Message::error(
780757
__(
781758
'Invalid column (%s) specified! Ensure that columns'
@@ -788,9 +765,7 @@ private function getSqlTemplateAndRequiredFields(
788765
break;
789766
}
790767

791-
if (isset($field)) {
792-
$fields[] = $field;
793-
}
768+
$fields[] = $val;
794769

795770
$sqlTemplate .= Util::backquote($val);
796771
}

0 commit comments

Comments
 (0)