Skip to content

Commit e2155ab

Browse files
authored
add support for inArray and oneOf
1 parent 1ede841 commit e2155ab

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ This extension specifies types of values passed to:
5555
* `Assert::implementsInterface`
5656
* `Assert::classExists`
5757
* `Assert::minCount`
58+
* `Assert::inArray`
59+
* `Assert::oneOf`
5860
* `nullOr*` and `all*` variants of the above methods
5961

6062

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,26 @@ private static function getExpressionResolvers(): array
386386
$number->value
387387
);
388388
},
389+
'inArray' => function (Scope $scope, Arg $needle, Arg $array): \PhpParser\Node\Expr {
390+
return new \PhpParser\Node\Expr\FuncCall(
391+
new \PhpParser\Node\Name('in_array'),
392+
[
393+
$needle,
394+
$array,
395+
new Arg(new \PhpParser\Node\Expr\ConstFetch(new \PhpParser\Node\Name('true'))),
396+
]
397+
);
398+
},
399+
'oneOf' => function (Scope $scope, Arg $needle, Arg $array): \PhpParser\Node\Expr {
400+
return new \PhpParser\Node\Expr\FuncCall(
401+
new \PhpParser\Node\Name('in_array'),
402+
[
403+
$needle,
404+
$array,
405+
new Arg(new \PhpParser\Node\Expr\ConstFetch(new \PhpParser\Node\Name('true'))),
406+
]
407+
);
408+
},
389409
];
390410
}
391411

tests/Type/WebMozartAssert/AssertTypeSpecifyingExtensionTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,18 @@ public function testExtension(): void
188188
'Variable $ak is: int',
189189
152,
190190
],
191+
[
192+
'Variable $al is: \'bar\'|\'foo\'',
193+
155,
194+
],
195+
[
196+
'Variable $am is: \'bar\'|\'foo\'|null',
197+
158,
198+
],
199+
[
200+
'Variable $an is: 1|2',
201+
161,
202+
],
191203
]);
192204
}
193205

tests/Type/WebMozartAssert/data/data.php

Lines changed: 10 additions & 1 deletion
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, $n, $o, $p, $r, $s, ?int $t, ?int $u, $x, $aa, array $ab, $ac, $ad, $ae, $af, $ag, array $ah, $ai)
10+
public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, $o, $p, $r, $s, ?int $t, ?int $u, $x, $aa, array $ab, $ac, $ad, $ae, $af, $ag, array $ah, $ai, $al, $am, $an)
1111
{
1212
$a;
1313

@@ -150,6 +150,15 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k,
150150
Assert::minCount($aj, 1);
151151
$ak = array_pop($aj);
152152
$ak;
153+
154+
Assert::inArray($al, ['foo', 'bar']);
155+
$al;
156+
157+
Assert::nullOrInArray($am, ['foo', 'bar']);
158+
$am;
159+
160+
Assert::oneOf($an, [1, 2]);
161+
$an;
153162
}
154163

155164
}

0 commit comments

Comments
 (0)