Skip to content

Commit 839a7e7

Browse files
committed
Fix ImportXML existence check
Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
1 parent e454326 commit 839a7e7

3 files changed

Lines changed: 30 additions & 26 deletions

File tree

phpstan-baseline.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9441,7 +9441,7 @@ parameters:
94419441
-
94429442
message: '#^Argument of an invalid type SimpleXMLElement\|null supplied for foreach, only iterables are supported\.$#'
94439443
identifier: foreach.nonIterable
9444-
count: 1
9444+
count: 2
94459445
path: src/Plugins/Import/ImportXml.php
94469446

94479447
-
@@ -9477,7 +9477,7 @@ parameters:
94779477
-
94789478
message: '#^Offset ''name'' might not exist on SimpleXMLElement\|null\.$#'
94799479
identifier: offsetAccess.notFound
9480-
count: 4
9480+
count: 3
94819481
path: src/Plugins/Import/ImportXml.php
94829482

94839483
-

psalm-baseline.xml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6287,11 +6287,8 @@
62876287
<code><![CDATA[string[]]]></code>
62886288
</PossiblyUnusedReturnValue>
62896289
<RedundantCondition>
6290-
<code><![CDATA[$databaseXml->table]]></code>
6290+
<code><![CDATA[isset($xml->database)]]></code>
62916291
</RedundantCondition>
6292-
<TypeDoesNotContainType>
6293-
<code><![CDATA[[]]]></code>
6294-
</TypeDoesNotContainType>
62956292
<UnnecessaryVarAnnotation>
62966293
<code><![CDATA[SimpleXMLElement]]></code>
62976294
<code><![CDATA[SimpleXMLElement]]></code>

src/Plugins/Import/ImportXml.php

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -157,35 +157,42 @@ public function doImport(File|null $importHandle = null): array
157157
/**
158158
* Move down the XML tree to the actual data
159159
*/
160-
$databaseXml = $xml->database;
161160

162161
$analyses = null;
163162
/** @var ImportTable[] $tables */
164163
$tables = [];
165164

166-
$databaseName = (string) $databaseXml['name'];
167-
168-
/** @var SimpleXMLElement $tableRowXml */
169-
foreach ($databaseXml->table ?? [] as $tableRowXml) {
170-
$tableName = (string) $tableRowXml['name'];
171-
172-
$table = $tables[$tableName] ?? new ImportTable($tableName);
165+
$databaseName = '';
166+
// SimpleXMLElement does not obey typical PHP rules.
167+
// Children can be fetched through the magical __get() method,but if the child does not exist,
168+
// it will return an empty SimpleXMLElement instead of null.
169+
// This means that we need to check for the existence of the child before trying to access it.
170+
if (isset($xml->database)) {
171+
$databaseXml = $xml->database;
172+
$databaseName = (string) $databaseXml['name'];
173+
174+
/** @var SimpleXMLElement $tableRowXml */
175+
foreach ($databaseXml->table as $tableRowXml) {
176+
$tableName = (string) $tableRowXml['name'];
177+
178+
$table = $tables[$tableName] ?? new ImportTable($tableName);
179+
180+
$tableRow = [];
181+
/** @var SimpleXMLElement $tableCellXml */
182+
foreach ($tableRowXml->column as $tableCellXml) {
183+
/** @psalm-suppress PossiblyNullArrayAccess */
184+
$columnName = (string) $tableCellXml['name'];
185+
if (! in_array($columnName, $table->columns, true)) {
186+
$table->columns[] = $columnName;
187+
}
173188

174-
$tableRow = [];
175-
/** @var SimpleXMLElement $tableCellXml */
176-
foreach ($tableRowXml->column as $tableCellXml) {
177-
/** @psalm-suppress PossiblyNullArrayAccess */
178-
$columnName = (string) $tableCellXml['name'];
179-
if (! in_array($columnName, $table->columns, true)) {
180-
$table->columns[] = $columnName;
189+
$tableRow[] = (string) $tableCellXml;
181190
}
182191

183-
$tableRow[] = (string) $tableCellXml;
184-
}
185-
186-
$table->rows[] = $tableRow;
192+
$table->rows[] = $tableRow;
187193

188-
$tables[$tableName] = $table;
194+
$tables[$tableName] = $table;
195+
}
189196
}
190197

191198
unset($xml);

0 commit comments

Comments
 (0)