Skip to content

Commit 42b5926

Browse files
herndlmondrejmirtes
authored andcommitted
Support positiveInteger
1 parent 4e2fc5e commit 42b5926

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function demo(?int $a) {
2929
This extension specifies types of values passed to:
3030

3131
* `Assert::integer`
32+
* `Assert::positiveInteger`
3233
* `Assert::string`
3334
* `Assert::stringNotEmpty`
3435
* `Assert::float`

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

+12
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,18 @@ private static function getExpressionResolvers(): array
177177
[$value]
178178
);
179179
},
180+
'positiveInteger' => function (Scope $scope, Arg $value): \PhpParser\Node\Expr {
181+
return new \PhpParser\Node\Expr\BinaryOp\BooleanAnd(
182+
new \PhpParser\Node\Expr\FuncCall(
183+
new \PhpParser\Node\Name('is_int'),
184+
[$value]
185+
),
186+
new \PhpParser\Node\Expr\BinaryOp\Greater(
187+
$value->value,
188+
new \PhpParser\Node\Scalar\LNumber(0)
189+
)
190+
);
191+
},
180192
'string' => function (Scope $scope, Arg $value): \PhpParser\Node\Expr {
181193
return new \PhpParser\Node\Expr\FuncCall(
182194
new \PhpParser\Node\Name('is_string'),

tests/Type/WebMozartAssert/AssertTypeSpecifyingExtensionTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function dataFileAsserts(): iterable
1616
yield from $this->gatherAssertTypes(__DIR__ . '/data/collection.php');
1717
yield from $this->gatherAssertTypes(__DIR__ . '/data/data.php');
1818
yield from $this->gatherAssertTypes(__DIR__ . '/data/string.php');
19+
yield from $this->gatherAssertTypes(__DIR__ . '/data/type.php');
1920
}
2021

2122
/**
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PHPStan\Type\WebMozartAssert;
4+
5+
use Webmozart\Assert\Assert;
6+
7+
class TypeTest
8+
{
9+
/**
10+
* @param mixed $a
11+
*/
12+
public function positiveInteger($a): void
13+
{
14+
Assert::positiveInteger($a);
15+
\PHPStan\Testing\assertType('int<1, max>', $a);
16+
17+
$b = -1;
18+
Assert::positiveInteger($b);
19+
\PHPStan\Testing\assertType('*NEVER*', $b);
20+
}
21+
}

0 commit comments

Comments
 (0)