Skip to content

Commit 07b88f8

Browse files
simPodondrejmirtes
authored andcommittedOct 10, 2019
isJsonString should implicitly assert value is of string type
Resolves #8
1 parent 3fd0b0b commit 07b88f8

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ This extension specifies types of values passed to:
4646
* `Assertion::notNull`
4747
* `Assertion::same`
4848
* `Assertion::notSame`
49+
* `Assertion::isJsonString`
4950
* `nullOr*` and `all*` variants of the above methods
5051

5152
`Assert::that`, `Assert::thatNullOr` and `Assert::thatAll` chaining methods are also supported.

‎src/Type/BeberleiAssert/AssertHelper.php

+6
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,12 @@ private static function getExpressionResolvers(): array
356356
]
357357
);
358358
},
359+
'isJsonString' => function (Scope $scope, Arg $value): \PhpParser\Node\Expr {
360+
return new \PhpParser\Node\Expr\FuncCall(
361+
new \PhpParser\Node\Name('is_string'),
362+
[$value]
363+
);
364+
},
359365
];
360366
}
361367

‎tests/Type/BeberleiAssert/AssertTypeSpecifyingExtensionTest.php

+19-15
Original file line numberDiff line numberDiff line change
@@ -181,65 +181,69 @@ public function testExtension(): void
181181
'Variable $ab is: array<PHPStan\Type\BeberleiAssert\Foo>',
182182
110,
183183
],
184+
[
185+
'Variable $ac is: string',
186+
113,
187+
],
184188
[
185189
'Variable $that is: Assert\AssertionChain<int|null>',
186-
116,
190+
119,
187191
],
188192
[
189193
'Variable $thatOrNull is: Assert\AssertionChain<int|null>',
190-
119,
194+
122,
191195
],
192196
[
193197
'Variable $a is: int',
194-
120,
198+
123,
195199
],
196200
[
197201
'Variable $assertNullOr is: Assert\AssertionChain<mixed>-nullOr',
198-
123,
202+
126,
199203
],
200204
[
201205
'Variable $b is: string|null',
202-
125,
206+
128,
203207
],
204208
[
205209
'Variable $assertNullOr2 is: Assert\AssertionChain<mixed>-nullOr',
206-
128,
210+
131,
207211
],
208212
[
209213
'Variable $c is: string|null',
210-
130,
214+
133,
211215
],
212216
[
213217
'Variable $assertAll is: Assert\AssertionChain<array>-all',
214-
133,
218+
136,
215219
],
216220
[
217221
'Variable $d is: array<string>',
218-
135,
222+
138,
219223
],
220224
[
221225
'Variable $assertAll2 is: Assert\AssertionChain<iterable>-all',
222-
138,
226+
141,
223227
],
224228
[
225229
'Variable $e is: iterable<string>',
226-
140,
230+
143,
227231
],
228232
[
229233
'Variable $f is: array<string>',
230-
145,
234+
148,
231235
],
232236
[
233237
'Variable $assertThatFunction is: Assert\AssertionChain<mixed>',
234-
148,
238+
151,
235239
],
236240
[
237241
'Variable $assertThatNullOrFunction is: Assert\AssertionChain<mixed>-nullOr',
238-
151,
242+
154,
239243
],
240244
[
241245
'Variable $assertThatAllFunction is: Assert\AssertionChain<mixed>-all',
242-
154,
246+
157,
243247
],
244248
]);
245249
}

‎tests/Type/BeberleiAssert/data/data.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class Foo
88
{
99

10-
public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, string $n, $p, $r, $s, ?int $t, ?int $u, $x, $aa, array $ab)
10+
public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, string $n, $p, $r, $s, ?int $t, ?int $u, $x, $aa, array $ab, ?string $ac)
1111
{
1212
$a;
1313

@@ -108,6 +108,9 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k,
108108

109109
Assertion::allSubclassOf($ab, self::class);
110110
$ab;
111+
112+
Assertion::isJsonString($ac);
113+
$ac;
111114
}
112115

113116
public function doBar(?int $a, $b, $c, array $d, iterable $e, $g)

0 commit comments

Comments
 (0)