Skip to content

Commit 29c8c6c

Browse files
herndlmondrejmirtes
authored andcommitted
Complete string length type specifiers
1 parent f53f3cb commit 29c8c6c

File tree

4 files changed

+109
-8
lines changed

4 files changed

+109
-8
lines changed

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

+56-2
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,13 @@ private static function getExpressionResolvers(): array
404404
$number->value
405405
);
406406
},
407-
'minLength' => function (Scope $scope, Arg $value, Arg $length): \PhpParser\Node\Expr {
407+
'length' => function (Scope $scope, Arg $value, Arg $length): \PhpParser\Node\Expr {
408408
return new BooleanAnd(
409409
new \PhpParser\Node\Expr\FuncCall(
410410
new \PhpParser\Node\Name('is_string'),
411411
[$value]
412412
),
413-
new \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual(
413+
new \PhpParser\Node\Expr\BinaryOp\Identical(
414414
new \PhpParser\Node\Expr\FuncCall(
415415
new \PhpParser\Node\Name('strlen'),
416416
[$value]
@@ -419,6 +419,60 @@ private static function getExpressionResolvers(): array
419419
)
420420
);
421421
},
422+
'minLength' => function (Scope $scope, Arg $value, Arg $min): \PhpParser\Node\Expr {
423+
return new BooleanAnd(
424+
new \PhpParser\Node\Expr\FuncCall(
425+
new \PhpParser\Node\Name('is_string'),
426+
[$value]
427+
),
428+
new \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual(
429+
new \PhpParser\Node\Expr\FuncCall(
430+
new \PhpParser\Node\Name('strlen'),
431+
[$value]
432+
),
433+
$min->value
434+
)
435+
);
436+
},
437+
'maxLength' => function (Scope $scope, Arg $value, Arg $max): \PhpParser\Node\Expr {
438+
return new BooleanAnd(
439+
new \PhpParser\Node\Expr\FuncCall(
440+
new \PhpParser\Node\Name('is_string'),
441+
[$value]
442+
),
443+
new \PhpParser\Node\Expr\BinaryOp\SmallerOrEqual(
444+
new \PhpParser\Node\Expr\FuncCall(
445+
new \PhpParser\Node\Name('strlen'),
446+
[$value]
447+
),
448+
$max->value
449+
)
450+
);
451+
},
452+
'lengthBetween' => function (Scope $scope, Arg $value, Arg $min, Arg $max): \PhpParser\Node\Expr {
453+
return new BooleanAnd(
454+
new \PhpParser\Node\Expr\FuncCall(
455+
new \PhpParser\Node\Name('is_string'),
456+
[$value]
457+
),
458+
new BooleanAnd(
459+
new \PhpParser\Node\Expr\BinaryOp\GreaterOrEqual(
460+
new \PhpParser\Node\Expr\FuncCall(
461+
new \PhpParser\Node\Name('strlen'),
462+
[$value]
463+
),
464+
$min->value
465+
),
466+
new \PhpParser\Node\Expr\BinaryOp\SmallerOrEqual(
467+
new \PhpParser\Node\Expr\FuncCall(
468+
new \PhpParser\Node\Name('strlen'),
469+
[$value]
470+
),
471+
$max->value
472+
)
473+
)
474+
);
475+
},
422476
'inArray' => function (Scope $scope, Arg $needle, Arg $array): \PhpParser\Node\Expr {
423477
return new \PhpParser\Node\Expr\FuncCall(
424478
new \PhpParser\Node\Name('in_array'),

tests/Type/WebMozartAssert/AssertTypeSpecifyingExtensionTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public function dataFileAsserts(): iterable
1414
{
1515
yield from $this->gatherAssertTypes(__DIR__ . '/data/array.php');
1616
yield from $this->gatherAssertTypes(__DIR__ . '/data/data.php');
17+
yield from $this->gatherAssertTypes(__DIR__ . '/data/string.php');
1718
}
1819

1920
/**

tests/Type/WebMozartAssert/data/data.php

-6
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,6 @@ public function doFoo($a, $b, array $c, iterable $d, $e, $f, $g, $h, $i, $j, $k,
172172
Assert::isArrayAccessible($aq);
173173
\PHPStan\Testing\assertType('array|ArrayAccess', $aq);
174174

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-
181175
Assert::interfaceExists($ag);
182176
\PHPStan\Testing\assertType('class-string', $ag);
183177
}
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\WebMozartAssert;
4+
5+
use Webmozart\Assert\Assert;
6+
7+
class Foo
8+
{
9+
10+
public function length(string $a, string $b): void
11+
{
12+
Assert::length($a, 0);
13+
\PHPStan\Testing\assertType('\'\'', $a);
14+
15+
Assert::length($b, 1);
16+
\PHPStan\Testing\assertType('non-empty-string', $b);
17+
}
18+
19+
public function minLength(string $a, string $b): void
20+
{
21+
Assert::minLength($a, 0);
22+
\PHPStan\Testing\assertType('string', $a);
23+
24+
Assert::minLength($b, 1);
25+
\PHPStan\Testing\assertType('non-empty-string', $b);
26+
}
27+
28+
public function maxLength(string $a, string $b): void
29+
{
30+
Assert::maxLength($a, 0);
31+
\PHPStan\Testing\assertType('\'\'', $a);
32+
33+
Assert::maxLength($b, 1);
34+
\PHPStan\Testing\assertType('string', $b);
35+
}
36+
37+
public function lengthBetween(string $a, string $b, string $c, string $d): void
38+
{
39+
Assert::lengthBetween($a, 0, 0);
40+
\PHPStan\Testing\assertType('\'\'', $a);
41+
42+
Assert::lengthBetween($b, 0, 1);
43+
\PHPStan\Testing\assertType('string', $b);
44+
45+
Assert::lengthBetween($c, 1, 0);
46+
\PHPStan\Testing\assertType('*NEVER*', $c);
47+
48+
Assert::lengthBetween($d, 1, 1);
49+
\PHPStan\Testing\assertType('non-empty-string', $d);
50+
}
51+
52+
}

0 commit comments

Comments
 (0)