Skip to content

Commit 2fc6a5f

Browse files
committed
Implement positive-int and negative-int types
1 parent 912e66b commit 2fc6a5f

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

resources/functionMap.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11785,7 +11785,7 @@
1178511785
'strncasecmp' => ['int', 'str1'=>'string', 'str2'=>'string', 'len'=>'int'],
1178611786
'strncmp' => ['int', 'str1'=>'string', 'str2'=>'string', 'len'=>'int'],
1178711787
'strpbrk' => ['string|false', 'haystack'=>'string', 'char_list'=>'string'],
11788-
'strpos' => ['int|false', 'haystack'=>'string', 'needle'=>'string|int', 'offset='=>'int'],
11788+
'strpos' => ['positive-int|0|false', 'haystack'=>'string', 'needle'=>'string|int', 'offset='=>'int'],
1178911789
'strptime' => ['array|false', 'datestr'=>'string', 'format'=>'string'],
1179011790
'strrchr' => ['string|false', 'haystack'=>'string', 'needle'=>'mixed'],
1179111791
'strrev' => ['string', 'str'=>'string'],

src/PhpDoc/TypeNodeResolver.php

+7
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
use PHPStan\Type\FloatType;
4646
use PHPStan\Type\Generic\GenericClassStringType;
4747
use PHPStan\Type\Generic\GenericObjectType;
48+
use PHPStan\Type\IntegerRangeType;
4849
use PHPStan\Type\IntegerType;
4950
use PHPStan\Type\IntersectionType;
5051
use PHPStan\Type\IterableType;
@@ -129,6 +130,12 @@ private function resolveIdentifierTypeNode(IdentifierTypeNode $typeNode, NameSco
129130
case 'integer':
130131
return new IntegerType();
131132

133+
case 'positive-int':
134+
return IntegerRangeType::fromInterval(1, null);
135+
136+
case 'negative-int':
137+
return IntegerRangeType::fromInterval(null, -1);
138+
132139
case 'string':
133140
return new StringType();
134141

tests/PHPStan/Analyser/NodeScopeResolverTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -10199,6 +10199,11 @@ public function dataBug1924(): array
1019910199
return $this->gatherAssertTypes(__DIR__ . '/data/bug-1924.php');
1020010200
}
1020110201

10202+
public function dataExtraIntTypes(): array
10203+
{
10204+
return $this->gatherAssertTypes(__DIR__ . '/data/extra-int-types.php');
10205+
}
10206+
1020210207
/**
1020310208
* @dataProvider dataBug2574
1020410209
* @dataProvider dataBug2577
@@ -10281,6 +10286,7 @@ public function dataBug1924(): array
1028110286
* @dataProvider dataClassConstantOnExpression
1028210287
* @dataProvider dataBug3961
1028310288
* @dataProvider dataBug1924
10289+
* @dataProvider dataExtraIntTypes
1028410290
* @param string $assertType
1028510291
* @param string $file
1028610292
* @param mixed ...$args
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace ExtraIntTypes;
4+
5+
use function PHPStan\Analyser\assertType;
6+
7+
class Foo
8+
{
9+
10+
/**
11+
* @param positive-int $positiveInt
12+
* @param negative-int $negativeInt
13+
*/
14+
public function doFoo(
15+
int $positiveInt,
16+
int $negativeInt,
17+
string $str
18+
): void
19+
{
20+
assertType('int<1, max>', $positiveInt);
21+
assertType('int<min, -1>', $negativeInt);
22+
assertType('false', strpos('u', $str) === -1);
23+
assertType('true', strpos('u', $str) !== -1);
24+
}
25+
26+
}

0 commit comments

Comments
 (0)