Skip to content

Commit b287c26

Browse files
committed
Cleanup throws annotations
1 parent 3fc08ac commit b287c26

File tree

7 files changed

+14
-36
lines changed

7 files changed

+14
-36
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ You can find and compare releases at the [GitHub release page](https://github.co
99

1010
## Unreleased
1111

12+
## 4.1.0
13+
14+
### Changed
15+
16+
- Improve error message when values can not be coerced into strings
17+
1218
## 4.0.0
1319

1420
### Added

src/Utils.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,21 @@ public static function extractStringFromLiteral(Node $valueNode): string
4444
}
4545

4646
/**
47-
* Convert the value to a string and throw an exception if it is not possible.
47+
* Convert the value to a string or throw.
4848
*
49+
* @template T of \Throwable
4950
* @param mixed $value Any value
51+
* @param class-string<T> $exceptionClass
5052
*
51-
* @throws \Exception of type $exceptionClass
53+
* @throws T
5254
*/
5355
public static function coerceToString($value, string $exceptionClass): string
5456
{
5557
if (!self::canBeString($value)) {
5658
$safeValue = GraphQLUtils::printSafeJson($value);
5759

5860
throw new $exceptionClass(
59-
"The given value {$safeValue} can not be serialized."
61+
"The given value {$safeValue} can not be coerced to a string."
6062
);
6163
}
6264

tests/EmailTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class EmailTest extends TestCase
1515
public function testSerializeThrowsIfUnserializableValueIsGiven(): void
1616
{
1717
$this->expectException(InvariantViolation::class);
18-
$this->expectExceptionMessageMatches(/** @lang RegExp */ '/^The given value .* can not be serialized\./');
18+
$this->expectExceptionMessageMatches(/** @lang RegExp */ '/^The given value .* can not be coerced to a string\./');
1919

2020
(new Email())->serialize(
2121
new class() {

tests/MyRegex.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ class MyRegex extends Regex
1010
{
1111
public $description = 'Bar';
1212

13-
/**
14-
* Return the Regex that the values are validated against.
15-
*/
1613
public static function regex(): string
1714
{
1815
return /** @lang RegExp */'/foo/';

tests/MyStringScalar.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ class MyStringScalar extends StringScalar
1010
{
1111
public $description = 'Bar';
1212

13-
/**
14-
* Check if the given string is exactly "foo".
15-
*/
1613
protected function isValid(string $stringValue): bool
1714
{
1815
return $stringValue === 'foo';

tests/RegexTest.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,18 @@ public function __toString(): string
113113

114114
/**
115115
* @dataProvider regexClassProvider
116-
*
117-
* @throws Error
118116
*/
119117
public function testParseValueThrowsIfValueCantBeString(Regex $regex): void
120118
{
121119
$this->expectException(Error::class);
122-
$this->expectExceptionMessageMatches(/** @lang RegExp */'/can not be serialized/');
120+
$this->expectExceptionMessageMatches(/** @lang RegExp */'/can not be coerced to a string/');
123121

124122
$regex->parseValue(new class() {
125123
});
126124
}
127125

128126
/**
129127
* @dataProvider regexClassProvider
130-
*
131-
* @throws Error
132128
*/
133129
public function testParseValueThrowsIfValueDoesNotMatch(Regex $regex): void
134130
{
@@ -140,8 +136,6 @@ public function testParseValueThrowsIfValueDoesNotMatch(Regex $regex): void
140136

141137
/**
142138
* @dataProvider regexClassProvider
143-
*
144-
* @throws Error
145139
*/
146140
public function testParseValuePassesOnMatch(Regex $regex): void
147141
{
@@ -153,8 +147,6 @@ public function testParseValuePassesOnMatch(Regex $regex): void
153147

154148
/**
155149
* @dataProvider regexClassProvider
156-
*
157-
* @throws Error
158150
*/
159151
public function testParseLiteralThrowsIfNotString(Regex $regex): void
160152
{
@@ -166,8 +158,6 @@ public function testParseLiteralThrowsIfNotString(Regex $regex): void
166158

167159
/**
168160
* @dataProvider regexClassProvider
169-
*
170-
* @throws Error
171161
*/
172162
public function testParseLiteralThrowsIfValueDoesNotMatch(Regex $regex): void
173163
{
@@ -179,8 +169,6 @@ public function testParseLiteralThrowsIfValueDoesNotMatch(Regex $regex): void
179169

180170
/**
181171
* @dataProvider regexClassProvider
182-
*
183-
* @throws Error
184172
*/
185173
public function testParseLiteralPassesOnMatch(Regex $regex): void
186174
{

tests/StringScalarTest.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,22 +119,18 @@ public function __toString(): string
119119

120120
/**
121121
* @dataProvider stringClassProvider
122-
*
123-
* @throws Error
124122
*/
125123
public function testParseValueThrowsIfValueCantBeString(StringScalar $stringScalar): void
126124
{
127125
$this->expectException(Error::class);
128-
$this->expectExceptionMessageMatches(/** @lang RegExp */'/can not be serialized/');
126+
$this->expectExceptionMessageMatches(/** @lang RegExp */'/can not be coerced to a string/');
129127

130128
$stringScalar->parseValue(new class() {
131129
});
132130
}
133131

134132
/**
135133
* @dataProvider stringClassProvider
136-
*
137-
* @throws Error
138134
*/
139135
public function testParseValueThrowsIfValueDoesNotMatch(StringScalar $stringScalar): void
140136
{
@@ -146,8 +142,6 @@ public function testParseValueThrowsIfValueDoesNotMatch(StringScalar $stringScal
146142

147143
/**
148144
* @dataProvider stringClassProvider
149-
*
150-
* @throws Error
151145
*/
152146
public function testParseValuePassesOnMatch(StringScalar $stringScalar): void
153147
{
@@ -159,8 +153,6 @@ public function testParseValuePassesOnMatch(StringScalar $stringScalar): void
159153

160154
/**
161155
* @dataProvider stringClassProvider
162-
*
163-
* @throws Error
164156
*/
165157
public function testParseLiteralThrowsIfNotString(StringScalar $stringScalar): void
166158
{
@@ -172,8 +164,6 @@ public function testParseLiteralThrowsIfNotString(StringScalar $stringScalar): v
172164

173165
/**
174166
* @dataProvider stringClassProvider
175-
*
176-
* @throws Error
177167
*/
178168
public function testParseLiteralThrowsIfValueDoesNotMatch(StringScalar $stringScalar): void
179169
{
@@ -185,8 +175,6 @@ public function testParseLiteralThrowsIfValueDoesNotMatch(StringScalar $stringSc
185175

186176
/**
187177
* @dataProvider stringClassProvider
188-
*
189-
* @throws Error
190178
*/
191179
public function testParseLiteralPassesOnMatch(StringScalar $stringScalar): void
192180
{

0 commit comments

Comments
 (0)