Skip to content

Commit

Permalink
Improve error messages when a mapping file mixes strings and arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
EreMaijala committed Dec 12, 2023
1 parent ddd26c8 commit 8206b5c
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/RecordManager/Base/Utils/FieldMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
namespace RecordManager\Base\Utils;

use function is_array;
use function is_string;

/**
* Field value mapper
Expand Down Expand Up @@ -326,16 +327,23 @@ protected function readMappingFile($filename)
}
if (!isset($parts[1])) {
fclose($handle);
throw new \Exception(
"Unable to parse mapping file '$filename' line "
. "(no ' = ' found): ($lineno) $line"
);
throw new \Exception("Unable to parse mapping file $filename: no ' = ' found on line $lineno: $line");
}
$key = trim($parts[0]);
$value = trim($parts[1]);
if (substr($key, -2) == '[]') {
$mappings[substr($key, 0, -2)][] = $value;
$key = substr($key, 0, -2);
// @phpstan-ignore-next-line
if (is_string($mappings[$key] ?? null)) {
throw new \Exception("$key already defined as a single value in $filename line $lineno: $line");
}
$mappings[$key][] = $value;
} else {
if (is_array($mappings[$key] ?? null)) {
throw new \Exception(
"$key already defined as an array of values in $filename line $lineno: $line"
);
}
$mappings[$key] = $value;
}
}
Expand Down

0 comments on commit 8206b5c

Please sign in to comment.