diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 343c396463..d19e367f1f 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -9441,7 +9441,7 @@ parameters: - message: '#^Argument of an invalid type SimpleXMLElement\|null supplied for foreach, only iterables are supported\.$#' identifier: foreach.nonIterable - count: 1 + count: 2 path: src/Plugins/Import/ImportXml.php - @@ -9477,7 +9477,7 @@ parameters: - message: '#^Offset ''name'' might not exist on SimpleXMLElement\|null\.$#' identifier: offsetAccess.notFound - count: 4 + count: 3 path: src/Plugins/Import/ImportXml.php - diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 1e730967b8..710e0e2f90 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -6287,11 +6287,8 @@ - table]]> + database)]]> - - - diff --git a/src/Plugins/Import/ImportXml.php b/src/Plugins/Import/ImportXml.php index 33c17596f4..b4099fff16 100644 --- a/src/Plugins/Import/ImportXml.php +++ b/src/Plugins/Import/ImportXml.php @@ -157,35 +157,42 @@ class ImportXml extends ImportPlugin /** * Move down the XML tree to the actual data */ - $databaseXml = $xml->database; $analyses = null; /** @var ImportTable[] $tables */ $tables = []; - $databaseName = (string) $databaseXml['name']; + $databaseName = ''; + // SimpleXMLElement does not obey typical PHP rules. + // Children can be fetched through the magical __get() method,but if the child does not exist, + // it will return an empty SimpleXMLElement instead of null. + // This means that we need to check for the existence of the child before trying to access it. + if (isset($xml->database)) { + $databaseXml = $xml->database; + $databaseName = (string) $databaseXml['name']; - /** @var SimpleXMLElement $tableRowXml */ - foreach ($databaseXml->table ?? [] as $tableRowXml) { - $tableName = (string) $tableRowXml['name']; + /** @var SimpleXMLElement $tableRowXml */ + foreach ($databaseXml->table as $tableRowXml) { + $tableName = (string) $tableRowXml['name']; - $table = $tables[$tableName] ?? new ImportTable($tableName); + $table = $tables[$tableName] ?? new ImportTable($tableName); - $tableRow = []; - /** @var SimpleXMLElement $tableCellXml */ - foreach ($tableRowXml->column as $tableCellXml) { - /** @psalm-suppress PossiblyNullArrayAccess */ - $columnName = (string) $tableCellXml['name']; - if (! in_array($columnName, $table->columns, true)) { - $table->columns[] = $columnName; + $tableRow = []; + /** @var SimpleXMLElement $tableCellXml */ + foreach ($tableRowXml->column as $tableCellXml) { + /** @psalm-suppress PossiblyNullArrayAccess */ + $columnName = (string) $tableCellXml['name']; + if (! in_array($columnName, $table->columns, true)) { + $table->columns[] = $columnName; + } + + $tableRow[] = (string) $tableCellXml; } - $tableRow[] = (string) $tableCellXml; + $table->rows[] = $tableRow; + + $tables[$tableName] = $table; } - - $table->rows[] = $tableRow; - - $tables[$tableName] = $table; } unset($xml);