Skip to content

Commit

Permalink
Resolve Assert::minLength($s, 1+) to non-empty-string
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod authored and ondrejmirtes committed Jul 16, 2021
1 parent 719bc82 commit 978e524
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
9 changes: 8 additions & 1 deletion tests/Type/WebMozartAssert/data/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

}

}
Expand Down

0 comments on commit 978e524

Please sign in to comment.