Skip to content

Commit

Permalink
Implement positive-int and negative-int types
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 22, 2020
1 parent 912e66b commit 2fc6a5f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion resources/functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -11785,7 +11785,7 @@
'strncasecmp' => ['int', 'str1'=>'string', 'str2'=>'string', 'len'=>'int'],
'strncmp' => ['int', 'str1'=>'string', 'str2'=>'string', 'len'=>'int'],
'strpbrk' => ['string|false', 'haystack'=>'string', 'char_list'=>'string'],
'strpos' => ['int|false', 'haystack'=>'string', 'needle'=>'string|int', 'offset='=>'int'],
'strpos' => ['positive-int|0|false', 'haystack'=>'string', 'needle'=>'string|int', 'offset='=>'int'],
'strptime' => ['array|false', 'datestr'=>'string', 'format'=>'string'],
'strrchr' => ['string|false', 'haystack'=>'string', 'needle'=>'mixed'],
'strrev' => ['string', 'str'=>'string'],
Expand Down
7 changes: 7 additions & 0 deletions src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use PHPStan\Type\FloatType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\IterableType;
Expand Down Expand Up @@ -129,6 +130,12 @@ private function resolveIdentifierTypeNode(IdentifierTypeNode $typeNode, NameSco
case 'integer':
return new IntegerType();

case 'positive-int':
return IntegerRangeType::fromInterval(1, null);

case 'negative-int':
return IntegerRangeType::fromInterval(null, -1);

case 'string':
return new StringType();

Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10199,6 +10199,11 @@ public function dataBug1924(): array
return $this->gatherAssertTypes(__DIR__ . '/data/bug-1924.php');
}

public function dataExtraIntTypes(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/extra-int-types.php');
}

/**
* @dataProvider dataBug2574
* @dataProvider dataBug2577
Expand Down Expand Up @@ -10281,6 +10286,7 @@ public function dataBug1924(): array
* @dataProvider dataClassConstantOnExpression
* @dataProvider dataBug3961
* @dataProvider dataBug1924
* @dataProvider dataExtraIntTypes
* @param string $assertType
* @param string $file
* @param mixed ...$args
Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/data/extra-int-types.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace ExtraIntTypes;

use function PHPStan\Analyser\assertType;

class Foo
{

/**
* @param positive-int $positiveInt
* @param negative-int $negativeInt
*/
public function doFoo(
int $positiveInt,
int $negativeInt,
string $str
): void
{
assertType('int<1, max>', $positiveInt);
assertType('int<min, -1>', $negativeInt);
assertType('false', strpos('u', $str) === -1);
assertType('true', strpos('u', $str) !== -1);
}

}

0 comments on commit 2fc6a5f

Please sign in to comment.