From 978e524796e5e81851d09d5967b173cda173d422 Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Fri, 16 Jul 2021 08:36:24 +0200 Subject: [PATCH] Resolve `Assert::minLength($s, 1+)` to `non-empty-string` --- .../AssertTypeSpecifyingExtension.php | 15 +++++++++++++++ tests/Type/WebMozartAssert/data/data.php | 9 ++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php b/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php index 5c9f9ce..6e88bd3 100644 --- a/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php +++ b/src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php @@ -386,6 +386,21 @@ private static function getExpressionResolvers(): array $number->value ); }, + 'minLength' => function (Scope $scope, Arg $value, Arg $length): \PhpParser\Node\Expr { + return new BooleanAnd( + new \PhpParser\Node\Expr\FuncCall( + new \PhpParser\Node\Name('is_string'), + [$value] + ), + new \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual( + new \PhpParser\Node\Expr\FuncCall( + new \PhpParser\Node\Name('strlen'), + [$value] + ), + $length->value + ) + ); + }, 'inArray' => function (Scope $scope, Arg $needle, Arg $array): \PhpParser\Node\Expr { return new \PhpParser\Node\Expr\FuncCall( new \PhpParser\Node\Name('in_array'), diff --git a/tests/Type/WebMozartAssert/data/data.php b/tests/Type/WebMozartAssert/data/data.php index 45c8b0b..a4cec98 100644 --- a/tests/Type/WebMozartAssert/data/data.php +++ b/tests/Type/WebMozartAssert/data/data.php @@ -7,7 +7,7 @@ class Foo { - 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, $ao, $ap, $aq) + 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, $ao, $ap, $aq, $ar) { \PHPStan\Testing\assertType('mixed', $a); @@ -171,6 +171,13 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k, Assert::isArrayAccessible($aq); \PHPStan\Testing\assertType('array|ArrayAccess', $aq); + + Assert::minLength($ar, 0); + \PHPStan\Testing\assertType('string', $ar); + + Assert::minLength($ar, 1); + \PHPStan\Testing\assertType('non-empty-string', $ar); + } }