Skip to content

Commit bb36e1d

Browse files
committed
adding missing validation of the default value for valueByName
1 parent 5cd282c commit bb36e1d

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/Parameters.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,10 @@ public function valueByName(
417417
bool|string $required = false,
418418
Bytes|Token|DisplayString|DateTimeImmutable|string|int|float|bool|null $default = null
419419
): Bytes|Token|DisplayString|DateTimeImmutable|string|int|float|bool|null {
420+
if (null !== $default && null === Type::tryFromVariable($default)) {
421+
throw new SyntaxError('The default parameter is invalid.');
422+
}
423+
420424
try {
421425
return $this->getByName($name, $validate)->value();
422426
} catch (InvalidOffset $exception) {

tests/ItemTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,16 @@ public function it_can_validate_the_item_parameter_value(): void
509509
self::assertFalse($item->parameterByName(name: 'b', default: false));
510510
}
511511

512+
#[Test]
513+
public function it_will_fail_if_the_default_value_is_invalid_for_the_parameter_name(): void
514+
{
515+
$item = Item::fromAssociative(Token::fromString('babayaga'), ['a' => true]);
516+
517+
$this->expectException(SyntaxError::class);
518+
519+
$item->parameterByName('b', default:'bébé');
520+
}
521+
512522
#[Test]
513523
public function it_can_validate_and_trigger_a_custom_error_message(): void
514524
{

0 commit comments

Comments
 (0)