Skip to content

Commit 978e524

Browse files
simPodondrejmirtes
authored andcommitted
Resolve Assert::minLength($s, 1+) to non-empty-string
1 parent 719bc82 commit 978e524

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

+15
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,21 @@ private static function getExpressionResolvers(): array
386386
$number->value
387387
);
388388
},
389+
'minLength' => function (Scope $scope, Arg $value, Arg $length): \PhpParser\Node\Expr {
390+
return new BooleanAnd(
391+
new \PhpParser\Node\Expr\FuncCall(
392+
new \PhpParser\Node\Name('is_string'),
393+
[$value]
394+
),
395+
new \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual(
396+
new \PhpParser\Node\Expr\FuncCall(
397+
new \PhpParser\Node\Name('strlen'),
398+
[$value]
399+
),
400+
$length->value
401+
)
402+
);
403+
},
389404
'inArray' => function (Scope $scope, Arg $needle, Arg $array): \PhpParser\Node\Expr {
390405
return new \PhpParser\Node\Expr\FuncCall(
391406
new \PhpParser\Node\Name('in_array'),

tests/Type/WebMozartAssert/data/data.php

+8-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, $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)
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, $ao, $ap, $aq, $ar)
1111
{
1212
\PHPStan\Testing\assertType('mixed', $a);
1313

@@ -171,6 +171,13 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k,
171171

172172
Assert::isArrayAccessible($aq);
173173
\PHPStan\Testing\assertType('array|ArrayAccess', $aq);
174+
175+
Assert::minLength($ar, 0);
176+
\PHPStan\Testing\assertType('string', $ar);
177+
178+
Assert::minLength($ar, 1);
179+
\PHPStan\Testing\assertType('non-empty-string', $ar);
180+
174181
}
175182

176183
}

0 commit comments

Comments
 (0)