Skip to content

Commit 2c46094

Browse files
herndlmondrejmirtes
authored andcommitted
Fix integerish
1 parent 56d7b27 commit 2c46094

File tree

5 files changed

+52
-5
lines changed

5 files changed

+52
-5
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
],
88
"require": {
99
"php": "^7.1 || ^8.0",
10-
"phpstan/phpstan": "^1.4.7"
10+
"phpstan/phpstan": "^1.4.8"
1111
},
1212
"require-dev": {
1313
"nikic/php-parser": "^4.13.0",

src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
use PhpParser\Node\Expr\BinaryOp;
1313
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
1414
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
15+
use PhpParser\Node\Expr\BinaryOp\Equal;
1516
use PhpParser\Node\Expr\BinaryOp\Greater;
1617
use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual;
1718
use PhpParser\Node\Expr\BinaryOp\Identical;
1819
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
1920
use PhpParser\Node\Expr\BinaryOp\Smaller;
2021
use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
2122
use PhpParser\Node\Expr\BooleanNot;
23+
use PhpParser\Node\Expr\Cast\Int_;
2224
use PhpParser\Node\Expr\ConstFetch;
2325
use PhpParser\Node\Expr\FuncCall;
2426
use PhpParser\Node\Expr\Instanceof_;
@@ -236,9 +238,17 @@ private static function getExpressionResolvers(): array
236238
);
237239
},
238240
'integerish' => static function (Scope $scope, Arg $value): Expr {
239-
return new FuncCall(
240-
new Name('is_numeric'),
241-
[$value]
241+
return new BooleanAnd(
242+
new FuncCall(
243+
new Name('is_numeric'),
244+
[$value]
245+
),
246+
new Equal(
247+
$value->value,
248+
new Int_(
249+
$value->value
250+
)
251+
)
242252
);
243253
},
244254
'numeric' => static function (Scope $scope, Arg $value): Expr {

tests/Type/WebMozartAssert/ImpossibleCheckTypeMethodCallRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ public function testBug8(): void
6565
]);
6666
}
6767

68+
public function testBug32(): void
69+
{
70+
$this->analyse([__DIR__ . '/data/bug-32.php'], []);
71+
}
72+
6873
public function testBug33(): void
6974
{
7075
$this->analyse([__DIR__ . '/data/bug-33.php'], []);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WebmozartAssertBug32;
6+
7+
use Webmozart\Assert\Assert;
8+
9+
/**
10+
* @param numeric-string $numericString
11+
*/
12+
function test(float $float, int $int, string $numericString): void
13+
{
14+
Assert::integerish($float);
15+
Assert::integerish($int);
16+
Assert::integerish($numericString);
17+
}

tests/Type/WebMozartAssert/data/type.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,28 @@ public function integer($a, $b): void
3535
\PHPStan\Testing\assertType('int|null', $b);
3636
}
3737

38-
public function integerish($a, $b): void
38+
/**
39+
* @param numeric-string $e
40+
*/
41+
public function integerish($a, $b, int $c, float $d, string $e, string $f): void
3942
{
4043
Assert::integerish($a);
4144
\PHPStan\Testing\assertType('float|int|numeric-string', $a);
4245

4346
Assert::nullOrIntegerish($b);
4447
\PHPStan\Testing\assertType('float|int|numeric-string|null', $b);
48+
49+
Assert::integerish($c);
50+
\PHPStan\Testing\assertType('int', $c);
51+
52+
Assert::integerish($d);
53+
\PHPStan\Testing\assertType('float', $d);
54+
55+
Assert::integerish($e);
56+
\PHPStan\Testing\assertType('numeric-string', $e);
57+
58+
Assert::integerish($f);
59+
\PHPStan\Testing\assertType('numeric-string', $f);
4560
}
4661

4762
public function positiveInteger($a, $b, $c): void

0 commit comments

Comments
 (0)