Skip to content

Commit

Permalink
bug #40416 Fix ConstraintViolation#getMessageTemplate() to always r…
Browse files Browse the repository at this point in the history
…eturn `string` (Ocramius)

This PR was merged into the 5.2 branch.

Discussion
----------

Fix `ConstraintViolation#getMessageTemplate()` to always return `string`

| Q             | A
| ------------- | ---
| Branch?       | 5.2
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        |

`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: symfony/symfony#40415 (comment)

Commits
-------

72a464e449 Fix `ConstraintViolation#getMessageTemplate()` to always return `string`
  • Loading branch information
fabpot committed Mar 11, 2021
2 parents 94dc16d + 63134a5 commit 09c5430
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ConstraintViolation.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function __toString()
*/
public function getMessageTemplate()
{
return $this->messageTemplate;
return (string) $this->messageTemplate;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions Tests/ConstraintViolationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,19 @@ public function testRetrievedPropertyPathIsAStringEvenIfNotSet()
))->getPropertyPath()
);
}

public function testRetrievedMessageTemplateIsAStringEvenIfNotSet()
{
self::assertSame(
'',
(new ConstraintViolation(
'irrelevant',
null,
[],
'irrelevant',
'irrelevant',
null
))->getMessageTemplate()
);
}
}

0 comments on commit 09c5430

Please sign in to comment.