fix(RichObjectStrings): Make exception messages for invalid parameters more useful for debugging

Signed-off-by: provokateurin <kate@provokateurin.de>
This commit is contained in:
provokateurin 2025-03-12 14:42:37 +01:00
parent a59c89eb6a
commit 4f27d82471
No known key found for this signature in database
3 changed files with 9 additions and 9 deletions

View file

@ -34,7 +34,7 @@ class Event implements IEvent {
protected $subjectParsed = '';
/** @var string */
protected $subjectRich = '';
/** @var array */
/** @var array<string, array<string, string>> */
protected $subjectRichParameters = [];
/** @var string */
protected $message = '';
@ -44,7 +44,7 @@ class Event implements IEvent {
protected $messageParsed = '';
/** @var string */
protected $messageRich = '';
/** @var array */
/** @var array<string, array<string, string>> */
protected $messageRichParameters = [];
/** @var string */
protected $objectType = '';

View file

@ -56,7 +56,7 @@ class Validator implements IValidator {
throw new InvalidObjectExeption('Parameter is malformed');
}
$this->validateParameter($parameter);
$this->validateParameter($placeholder, $parameter);
}
}
@ -64,7 +64,7 @@ class Validator implements IValidator {
* @param array $parameter
* @throws InvalidObjectExeption
*/
protected function validateParameter(array $parameter): void {
protected function validateParameter(string $placeholder, array $parameter): void {
if (!isset($parameter['type'])) {
throw new InvalidObjectExeption('Object type is undefined');
}
@ -74,15 +74,15 @@ class Validator implements IValidator {
$missingKeys = array_diff($requiredParameters, array_keys($parameter));
if (!empty($missingKeys)) {
throw new InvalidObjectExeption('Object is invalid, missing keys:' . json_encode($missingKeys));
throw new InvalidObjectExeption('Object for placeholder ' . $placeholder . ' is invalid, missing keys:' . json_encode($missingKeys));
}
foreach ($parameter as $key => $value) {
if (!is_string($key)) {
throw new InvalidObjectExeption('Object is invalid, key ' . $key . ' is not a string');
throw new InvalidObjectExeption('Object for placeholder ' . $placeholder . ' is invalid, key ' . $key . ' is not a string');
}
if (!is_string($value)) {
throw new InvalidObjectExeption('Object is invalid, value ' . $value . ' is not a string');
throw new InvalidObjectExeption('Object for placeholder ' . $placeholder . ' is invalid, value ' . $value . ' for key ' . $key . ' is not a string');
}
}
}

View file

@ -39,7 +39,7 @@ class ValidatorTest extends TestCase {
$this->expectException(InvalidObjectExeption::class);
$this->expectExceptionMessage('Object is invalid, value 123 is not a string');
$this->expectExceptionMessage('Object for placeholder string1 is invalid, value 123 for key key is not a string');
$v->validate('test {string1} test.', [
'string1' => [
'type' => 'user',
@ -49,7 +49,7 @@ class ValidatorTest extends TestCase {
],
]);
$this->expectExceptionMessage('Object is invalid, key 456 is not a string');
$this->expectExceptionMessage('Object for placeholder string1 is invalid, key 456 is not a string');
$v->validate('test {string1} test.', [
'string1' => [
'type' => 'user',