8000 Fix `ConstraintViolation#getMessageTemplate()` to always return `string` · symfony/symfony@cb47a35 · GitHub
[go: up one dir, main page]

Skip to content

Commit cb47a35

Browse files
committed
Fix ConstraintViolation#getMessageTemplate() to always return string
`ConstraintViolation#getMessageTemplate()`'s inherited signature states that `string` is to be returned by it at all times, yet the implementation returns `null` when no message template had been provided at instantiation. This patch obviates it, returning an empty string when the message template is `null`. Ref: #40415 (comment)
1 parent ce8be3c commit cb47a35

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/Symfony/Component/Validator/ConstraintViolation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function __toString()
100100
*/
101101
public function getMessageTemplate()
102102
{
103-
return $this->messageTemplate;
103+
return (string) $this->messageTemplate;
104104
}
105105

106106
/**

src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,19 @@ public function testMessageObjectMustBeStringable()
156156
null
157157
);
158158
}
159+
160+
public function testRetrievedMessageTemplateIsAStringEvenIfNotSet()
161+
{
162+
self::assertSame(
163+
'',
164+
(new ConstraintViolation(
165+
'irrelevant',
166+
null,
167+
[],
168+
'irrelevant',
169+
'irrelevant',
170+
null
171+
))->getMessageTemplate()
172+
);
173+
}
159174
}

0 commit comments

Comments
 (0)
0