Skip to content

Commit

Permalink
Support isMap, adapt isList to work the same
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm authored and ondrejmirtes committed Feb 15, 2022
1 parent 4fe6508 commit 086010d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ This extension specifies types of values passed to:
* `Assert::minCount`
* `Assert::maxCount`
* `Assert::countBetween`
* `Assert::isList`
* `Assert::isMap`
* `Assert::inArray`
* `Assert::oneOf`
* `Assert::methodExists`
Expand Down
23 changes: 19 additions & 4 deletions src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,26 @@ private static function getExpressionResolvers(): array
[$expr]
),
new Identical(
$expr->value,
new FuncCall(
new Name('array_values'),
[$expr]
)
new Name('array_filter'),
[$expr, new Arg(new String_('is_int')), new Arg(new ConstFetch(new Name('ARRAY_FILTER_USE_KEY')))]
),
$expr->value
)
);
},
'isMap' => static function (Scope $scope, Arg $expr): Expr {
return new BooleanAnd(
new FuncCall(
new Name('is_array'),
[$expr]
),
new Identical(
new FuncCall(
new Name('array_filter'),
[$expr, new Arg(new String_('is_string')), new Arg(new ConstFetch(new Name('ARRAY_FILTER_USE_KEY')))]
),
$expr->value
)
);
},
Expand Down
9 changes: 9 additions & 0 deletions tests/Type/WebMozartAssert/data/array.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,13 @@ public function isList($a, $b): void
\PHPStan\Testing\assertType('array<int, mixed>|null', $b);
}

public function isMap($a, $b): void
{
Assert::isMap($a);
\PHPStan\Testing\assertType('array<string, mixed>', $a);

Assert::nullOrIsMap($b);
\PHPStan\Testing\assertType('array<string, mixed>|null', $b);
}

}

0 comments on commit 086010d

Please sign in to comment.