Skip to content

Fix integerish #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"require": {
"php": "^7.1 || ^8.0",
"phpstan/phpstan": "^1.4.7"
"phpstan/phpstan": "^1.4.8"
},
"require-dev": {
"nikic/php-parser": "^4.13.0",
Expand Down
16 changes: 13 additions & 3 deletions src/Type/WebMozartAssert/AssertTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Expr\BinaryOp\Greater;
use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BinaryOp\Smaller;
use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Cast\Int_;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
Expand Down Expand Up @@ -236,9 +238,17 @@ private static function getExpressionResolvers(): array
);
},
'integerish' => static function (Scope $scope, Arg $value): Expr {
return new FuncCall(
new Name('is_numeric'),
[$value]
return new BooleanAnd(
new FuncCall(
new Name('is_numeric'),
[$value]
),
new Equal(
$value->value,
new Int_(
$value->value
)
)
);
},
'numeric' => static function (Scope $scope, Arg $value): Expr {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public function testBug8(): void
]);
}

public function testBug32(): void
{
$this->analyse([__DIR__ . '/data/bug-32.php'], []);
}

public function testBug33(): void
{
$this->analyse([__DIR__ . '/data/bug-33.php'], []);
Expand Down
17 changes: 17 additions & 0 deletions tests/Type/WebMozartAssert/data/bug-32.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace WebmozartAssertBug32;

use Webmozart\Assert\Assert;

/**
* @param numeric-string $numericString
*/
function test(float $float, int $int, string $numericString): void
{
Assert::integerish($float);
Assert::integerish($int);
Assert::integerish($numericString);
}
17 changes: 16 additions & 1 deletion tests/Type/WebMozartAssert/data/type.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,28 @@ public function integer($a, $b): void
\PHPStan\Testing\assertType('int|null', $b);
}

public function integerish($a, $b): void
/**
* @param numeric-string $e
*/
public function integerish($a, $b, int $c, float $d, string $e, string $f): void
{
Assert::integerish($a);
\PHPStan\Testing\assertType('float|int|numeric-string', $a);

Assert::nullOrIntegerish($b);
\PHPStan\Testing\assertType('float|int|numeric-string|null', $b);

Assert::integerish($c);
\PHPStan\Testing\assertType('int', $c);

Assert::integerish($d);
\PHPStan\Testing\assertType('float', $d);

Assert::integerish($e);
\PHPStan\Testing\assertType('numeric-string', $e);

Assert::integerish($f);
\PHPStan\Testing\assertType('numeric-string', $f);
}

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