From 2fc6a5f96d444c210bc69e3971510d2726a53ce2 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Thu, 22 Oct 2020 13:25:43 +0200 Subject: [PATCH] Implement `positive-int` and `negative-int` types --- resources/functionMap.php | 2 +- src/PhpDoc/TypeNodeResolver.php | 7 +++++ .../Analyser/NodeScopeResolverTest.php | 6 +++++ .../PHPStan/Analyser/data/extra-int-types.php | 26 +++++++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 tests/PHPStan/Analyser/data/extra-int-types.php diff --git a/resources/functionMap.php b/resources/functionMap.php index 920d19d515..09796ccf7b 100644 --- a/resources/functionMap.php +++ b/resources/functionMap.php @@ -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'], diff --git a/src/PhpDoc/TypeNodeResolver.php b/src/PhpDoc/TypeNodeResolver.php index 85563630d1..952883b6ea 100644 --- a/src/PhpDoc/TypeNodeResolver.php +++ b/src/PhpDoc/TypeNodeResolver.php @@ -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; @@ -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(); diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 68e29fba16..e7fc85b588 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -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 @@ -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 diff --git a/tests/PHPStan/Analyser/data/extra-int-types.php b/tests/PHPStan/Analyser/data/extra-int-types.php new file mode 100644 index 0000000000..530e75ddbd --- /dev/null +++ b/tests/PHPStan/Analyser/data/extra-int-types.php @@ -0,0 +1,26 @@ +', $positiveInt); + assertType('int', $negativeInt); + assertType('false', strpos('u', $str) === -1); + assertType('true', strpos('u', $str) !== -1); + } + +}