Skip to content

Commit

Permalink
Complete string length type specifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm authored and ondrejmirtes committed Nov 29, 2021
1 parent f53f3cb commit 29c8c6c
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 8 deletions.
58 changes: 56 additions & 2 deletions src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,13 +404,13 @@ private static function getExpressionResolvers(): array
$number->value
);
},
'minLength' => function (Scope $scope, Arg $value, Arg $length): \PhpParser\Node\Expr {
'length' => 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\BinaryOp\Identical(
new \PhpParser\Node\Expr\FuncCall(
new \PhpParser\Node\Name('strlen'),
[$value]
Expand All @@ -419,6 +419,60 @@ private static function getExpressionResolvers(): array
)
);
},
'minLength' => function (Scope $scope, Arg $value, Arg $min): \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]
),
$min->value
)
);
},
'maxLength' => function (Scope $scope, Arg $value, Arg $max): \PhpParser\Node\Expr {
return new BooleanAnd(
new \PhpParser\Node\Expr\FuncCall(
new \PhpParser\Node\Name('is_string'),
[$value]
),
new \PhpParser\Node\Expr\BinaryOp\SmallerOrEqual(
new \PhpParser\Node\Expr\FuncCall(
new \PhpParser\Node\Name('strlen'),
[$value]
),
$max->value
)
);
},
'lengthBetween' => function (Scope $scope, Arg $value, Arg $min, Arg $max): \PhpParser\Node\Expr {
return new BooleanAnd(
new \PhpParser\Node\Expr\FuncCall(
new \PhpParser\Node\Name('is_string'),
[$value]
),
new BooleanAnd(
new \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual(
new \PhpParser\Node\Expr\FuncCall(
new \PhpParser\Node\Name('strlen'),
[$value]
),
$min->value
),
new \PhpParser\Node\Expr\BinaryOp\SmallerOrEqual(
new \PhpParser\Node\Expr\FuncCall(
new \PhpParser\Node\Name('strlen'),
[$value]
),
$max->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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function dataFileAsserts(): iterable
{
yield from $this->gatherAssertTypes(__DIR__ . '/data/array.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/data.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/string.php');
}

/**
Expand Down
6 changes: 0 additions & 6 deletions tests/Type/WebMozartAssert/data/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,6 @@ 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);

Assert::interfaceExists($ag);
\PHPStan\Testing\assertType('class-string', $ag);
}
Expand Down
52 changes: 52 additions & 0 deletions tests/Type/WebMozartAssert/data/string.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\WebMozartAssert;

use Webmozart\Assert\Assert;

class Foo
{

public function length(string $a, string $b): void
{
Assert::length($a, 0);
\PHPStan\Testing\assertType('\'\'', $a);

Assert::length($b, 1);
\PHPStan\Testing\assertType('non-empty-string', $b);
}

public function minLength(string $a, string $b): void
{
Assert::minLength($a, 0);
\PHPStan\Testing\assertType('string', $a);

Assert::minLength($b, 1);
\PHPStan\Testing\assertType('non-empty-string', $b);
}

public function maxLength(string $a, string $b): void
{
Assert::maxLength($a, 0);
\PHPStan\Testing\assertType('\'\'', $a);

Assert::maxLength($b, 1);
\PHPStan\Testing\assertType('string', $b);
}

public function lengthBetween(string $a, string $b, string $c, string $d): void
{
Assert::lengthBetween($a, 0, 0);
\PHPStan\Testing\assertType('\'\'', $a);

Assert::lengthBetween($b, 0, 1);
\PHPStan\Testing\assertType('string', $b);

Assert::lengthBetween($c, 1, 0);
\PHPStan\Testing\assertType('*NEVER*', $c);

Assert::lengthBetween($d, 1, 1);
\PHPStan\Testing\assertType('non-empty-string', $d);
}

}

0 comments on commit 29c8c6c

Please sign in to comment.