From 9d1c4a5a34273f0356a07214626b0e2c4e33bc65 Mon Sep 17 00:00:00 2001 From: Shish Date: Wed, 19 Feb 2025 13:21:01 +0000 Subject: [PATCH] Retain specific `int` details in docblocks https://github.com/thecodingmachine/phpstan-safe-rule/issues/52 --- .../PhpStanFunctions/PhpStanTypeTest.php | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/generator/tests/PhpStanFunctions/PhpStanTypeTest.php b/generator/tests/PhpStanFunctions/PhpStanTypeTest.php index 4a787f8d..e17ce65a 100644 --- a/generator/tests/PhpStanFunctions/PhpStanTypeTest.php +++ b/generator/tests/PhpStanFunctions/PhpStanTypeTest.php @@ -166,13 +166,6 @@ public function testNotEmptyStringBecomingString(): void $this->assertEquals('string', $param->getSignatureType(ErrorType::FALSY)); } - public function testPositiveIntBecomingInt(): void - { - $param = new PhpStanType('positive-int'); - $this->assertEquals('int', $param->getDocBlockType()); - $this->assertEquals('int', $param->getSignatureType()); - } - public function testListBecomingArray(): void { $param = new PhpStanType('list|false'); @@ -180,10 +173,22 @@ public function testListBecomingArray(): void $this->assertEquals('array', $param->getSignatureType(ErrorType::FALSY)); } - public function testNumbersAreRemoved(): void + public function testIntGeneralisation(): void { + // PHP only supports "int" rather than specific values or ranges, + // but being specific in the docblock brings significant benefits + // when combined with tools like phpstan, see: + // https://github.com/thecodingmachine/phpstan-safe-rule/issues/52 + $param = new PhpStanType('positive-int'); + $this->assertEquals('positive-int', $param->getDocBlockType()); + $this->assertEquals('int', $param->getSignatureType()); + $param = new PhpStanType('0|positive-int'); - $this->assertEquals('int', $param->getDocBlockType()); + $this->assertEquals('0|positive-int', $param->getDocBlockType()); + $this->assertEquals('int', $param->getSignatureType()); + + $param = new PhpStanType('0|1'); + $this->assertEquals('0|1', $param->getDocBlockType()); $this->assertEquals('int', $param->getSignatureType()); }