Skip to content

Commit 086010d

Browse files
herndlmondrejmirtes
authored andcommitted
Support isMap, adapt isList to work the same
1 parent 4fe6508 commit 086010d

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ This extension specifies types of values passed to:
6464
* `Assert::minCount`
6565
* `Assert::maxCount`
6666
* `Assert::countBetween`
67+
* `Assert::isList`
68+
* `Assert::isMap`
6769
* `Assert::inArray`
6870
* `Assert::oneOf`
6971
* `Assert::methodExists`

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

+19-4
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,26 @@ private static function getExpressionResolvers(): array
314314
[$expr]
315315
),
316316
new Identical(
317-
$expr->value,
318317
new FuncCall(
319-
new Name('array_values'),
320-
[$expr]
321-
)
318+
new Name('array_filter'),
319+
[$expr, new Arg(new String_('is_int')), new Arg(new ConstFetch(new Name('ARRAY_FILTER_USE_KEY')))]
320+
),
321+
$expr->value
322+
)
323+
);
324+
},
325+
'isMap' => static function (Scope $scope, Arg $expr): Expr {
326+
return new BooleanAnd(
327+
new FuncCall(
328+
new Name('is_array'),
329+
[$expr]
330+
),
331+
new Identical(
332+
new FuncCall(
333+
new Name('array_filter'),
334+
[$expr, new Arg(new String_('is_string')), new Arg(new ConstFetch(new Name('ARRAY_FILTER_USE_KEY')))]
335+
),
336+
$expr->value
322337
)
323338
);
324339
},

tests/Type/WebMozartAssert/data/array.php

+9
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,13 @@ public function isList($a, $b): void
106106
\PHPStan\Testing\assertType('array<int, mixed>|null', $b);
107107
}
108108

109+
public function isMap($a, $b): void
110+
{
111+
Assert::isMap($a);
112+
\PHPStan\Testing\assertType('array<string, mixed>', $a);
113+
114+
Assert::nullOrIsMap($b);
115+
\PHPStan\Testing\assertType('array<string, mixed>|null', $b);
116+
}
117+
109118
}

0 commit comments

Comments
 (0)