diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon
index 4345807710..7622a87284 100644
--- a/phpstan-baseline.neon
+++ b/phpstan-baseline.neon
@@ -11260,6 +11260,11 @@ parameters:
count: 1
path: src/Plugins/Import/ImportCsv.php
+ -
+ message: "#^Cannot cast mixed to int\\.$#"
+ count: 1
+ path: src/Plugins/Import/ImportCsv.php
+
-
message: "#^Cannot cast mixed to string\\.$#"
count: 8
@@ -11275,11 +11280,6 @@ parameters:
count: 2
path: src/Plugins/Import/ImportCsv.php
- -
- message: "#^Method PhpMyAdmin\\\\Plugins\\\\Import\\\\ImportCsv\\:\\:getTableNameFromImport\\(\\) should return string but returns mixed\\.$#"
- count: 1
- path: src/Plugins/Import/ImportCsv.php
-
-
message: "#^Only booleans are allowed in &&, string given on the right side\\.$#"
count: 1
diff --git a/psalm-baseline.xml b/psalm-baseline.xml
index e19390cc82..e2e4e98f69 100644
--- a/psalm-baseline.xml
+++ b/psalm-baseline.xml
@@ -8576,23 +8576,9 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/src/Plugins/Import/ImportCsv.php b/src/Plugins/Import/ImportCsv.php
index 9cee17f1f0..9ee2799146 100644
--- a/src/Plugins/Import/ImportCsv.php
+++ b/src/Plugins/Import/ImportCsv.php
@@ -23,7 +23,6 @@ use PhpMyAdmin\Properties\Options\Items\NumberPropertyItem;
use PhpMyAdmin\Properties\Options\Items\TextPropertyItem;
use PhpMyAdmin\Properties\Plugins\ImportPluginProperties;
use PhpMyAdmin\Util;
-use Webmozart\Assert\Assert;
use function __;
use function array_map;
@@ -34,6 +33,7 @@ use function in_array;
use function max;
use function mb_strlen;
use function mb_substr;
+use function min;
use function pathinfo;
use function preg_split;
use function rtrim;
@@ -61,6 +61,10 @@ class ImportCsv extends AbstractImportCsv
private string $escaped = '';
private string $newLine = '';
private string $columns = '';
+ private int $maxLines = 0;
+ private bool $csvHasColumnNames = false;
+ private string $newDatabaseName = '';
+ private string $newTableName = '';
/** @psalm-return non-empty-lowercase-string */
public function getName(): string
@@ -172,6 +176,10 @@ class ImportCsv extends AbstractImportCsv
$this->escaped = (string) $request->getParsedBodyParam('csv_escaped');
$this->newLine = (string) $request->getParsedBodyParam('csv_new_line');
$this->columns = (string) $request->getParsedBodyParam('csv_columns');
+ $this->maxLines = min(0, (int) $request->getParsedBodyParam('csv_partial_import'));
+ $this->csvHasColumnNames = $request->getParsedBodyParam('csv_col_names') !== null;
+ $this->newDatabaseName = (string) $request->getParsedBodyParam('csv_new_db_name');
+ $this->newTableName = (string) $request->getParsedBodyParam('csv_new_tbl_name');
}
/**
@@ -217,20 +225,10 @@ class ImportCsv extends AbstractImportCsv
$lasti = -1;
$values = [];
$csvFinish = false;
- $maxLines = 0; // defaults to 0 (get all the lines)
- /**
- * If we get a negative value, probably someone changed min value
- * attribute in DOM or there is an integer overflow, whatever be
- * the case, get all the lines.
- */
- if (isset($_REQUEST['csv_partial_import']) && $_REQUEST['csv_partial_import'] > 0) {
- $maxLines = $_REQUEST['csv_partial_import'];
- }
-
- $maxLinesConstraint = $maxLines + 1;
+ $maxLinesConstraint = $this->maxLines + 1;
// if the first row has to be counted as column names, include one more row in the max lines
- if (isset($_REQUEST['csv_col_names'])) {
+ if ($this->csvHasColumnNames) {
$maxLinesConstraint++;
}
@@ -549,13 +547,13 @@ class ImportCsv extends AbstractImportCsv
$i = 0;
$lasti = -1;
$ch = mb_substr($buffer, 0, 1);
- if ($maxLines > 0 && $line == $maxLinesConstraint) {
+ if ($this->maxLines > 0 && $line == $maxLinesConstraint) {
ImportSettings::$finished = true;
break;
}
}
- if ($maxLines > 0 && $line == $maxLinesConstraint) {
+ if ($this->maxLines > 0 && $line == $maxLinesConstraint) {
ImportSettings::$finished = true;
break;
}
@@ -569,7 +567,7 @@ class ImportCsv extends AbstractImportCsv
$colNames = [];
/* Remove the first row if it contains the column names */
- if (isset($_REQUEST['csv_col_names'])) {
+ if ($this->csvHasColumnNames) {
$colNames = array_shift($rows);
}
@@ -587,9 +585,8 @@ class ImportCsv extends AbstractImportCsv
* Otherwise, check if user provided the database name in the request,
* if not, set the default name
*/
- if (isset($_REQUEST['csv_new_db_name']) && (string) $_REQUEST['csv_new_db_name'] !== '') {
- $newDb = $_REQUEST['csv_new_db_name'];
- Assert::string($newDb);
+ if ($this->newDatabaseName !== '') {
+ $newDb = $this->newDatabaseName;
} else {
$result = $dbi->fetchResult('SHOW DATABASES');
@@ -693,8 +690,8 @@ class ImportCsv extends AbstractImportCsv
private function getTableNameFromImport(string $databaseName): string
{
// get new table name, if user didn't provide one, set the default name
- if (isset($_REQUEST['csv_new_tbl_name']) && (string) $_REQUEST['csv_new_tbl_name'] !== '') {
- return $_REQUEST['csv_new_tbl_name'];
+ if ($this->newTableName !== '') {
+ return $this->newTableName;
}
return $this->import->getNextAvailableTableName(
@@ -710,7 +707,7 @@ class ImportCsv extends AbstractImportCsv
*/
private function getColumnNames(array $columnNames, int $maxCols): array
{
- if (isset($_REQUEST['csv_col_names'])) {
+ if ($this->csvHasColumnNames) {
// MySQL column names can't end with a space character.
$columnNames = array_map(rtrim(...), $columnNames);
}
diff --git a/tests/unit/Plugins/Import/ImportCsvTest.php b/tests/unit/Plugins/Import/ImportCsvTest.php
index 02c3d6e63c..2ee5b5142f 100644
--- a/tests/unit/Plugins/Import/ImportCsvTest.php
+++ b/tests/unit/Plugins/Import/ImportCsvTest.php
@@ -131,10 +131,17 @@ class ImportCsvTest extends AbstractTestCase
$importHandle = new File(ImportSettings::$importFile);
$importHandle->open();
- ImportSettings::$importFile = 'tests/test_data/db_test_partial_import.csv';
- $_REQUEST['csv_new_tbl_name'] = 'ImportTestTable';
- $_REQUEST['csv_new_db_name'] = 'ImportTestDb';
- $_REQUEST['csv_partial_import'] = 5;
+ $request = ServerRequestFactory::create()->createServerRequest('POST', 'http://example.com/')
+ ->withParsedBody([
+ 'csv_terminated' => ',',
+ 'csv_enclosed' => '"',
+ 'csv_escaped' => '"',
+ 'csv_new_line' => 'auto',
+ 'csv_columns' => null,
+ 'csv_new_tbl_name' => 'ImportTestTable',
+ 'csv_new_db_name' => 'ImportTestDb',
+ ]);
+ $this->object->setImportOptions($request);
DatabaseInterface::$instance = $this->getMockBuilder(DatabaseInterface::class)
->disableOriginalConstructor()
@@ -154,10 +161,6 @@ class ImportCsvTest extends AbstractTestCase
);
self::assertTrue(ImportSettings::$finished);
-
- unset($_REQUEST['csv_new_tbl_name']);
- unset($_REQUEST['csv_new_db_name']);
- unset($_REQUEST['csv_partial_import']);
}
/**
@@ -267,11 +270,10 @@ class ImportCsvTest extends AbstractTestCase
'csv_escaped' => '"',
'csv_new_line' => 'auto',
'csv_columns' => null,
+ 'csv_col_names' => 'yes',
]);
$this->object->setImportOptions($request);
- $_REQUEST['csv_col_names'] = 'something';
-
$dummyDbi = $this->createDbiDummy();
$dbi = $this->createDatabaseInterface($dummyDbi);
DatabaseInterface::$instance = $dbi;