From ed8e74e36b46076d1e4c8920b593b2dd43448049 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 29 Apr 2023 22:32:48 +0700 Subject: [PATCH] [CodingStyle] Skip positive-int and negative-int over int on VarConstantCommentRector (#3709) * [CodingStyle] Skip positive-int over int on VarConstantCommentRector * Fixed :tada: * clean up * negative-int as well * add negative-int * Final touch: add dedicated IntegerRangeType data * reduce get_class() inside on downgrade code by using string * Final touch: clean up * Final touch: clean up --- .../StaticTypeMapper/Mapper/ScalarStringToTypeMapper.php | 9 +++++++++ .../Fixture/skip_negative_int_over_int.php.inc | 9 +++++++++ .../Fixture/skip_positive_int_over_int.php.inc | 9 +++++++++ 3 files changed, 27 insertions(+) create mode 100644 rules-tests/CodingStyle/Rector/ClassConst/VarConstantCommentRector/Fixture/skip_negative_int_over_int.php.inc create mode 100644 rules-tests/CodingStyle/Rector/ClassConst/VarConstantCommentRector/Fixture/skip_positive_int_over_int.php.inc diff --git a/packages/StaticTypeMapper/Mapper/ScalarStringToTypeMapper.php b/packages/StaticTypeMapper/Mapper/ScalarStringToTypeMapper.php index fe6845d71a3..bdfd9cbfd50 100644 --- a/packages/StaticTypeMapper/Mapper/ScalarStringToTypeMapper.php +++ b/packages/StaticTypeMapper/Mapper/ScalarStringToTypeMapper.php @@ -12,6 +12,7 @@ use PHPStan\Type\ClassStringType; use PHPStan\Type\Constant\ConstantBooleanType; use PHPStan\Type\FloatType; +use PHPStan\Type\IntegerRangeType; use PHPStan\Type\IntegerType; use PHPStan\Type\IterableType; use PHPStan\Type\MixedType; @@ -55,6 +56,14 @@ public function mapScalarStringToType(string $scalarName): Type return new ConstantBooleanType(true); } + if ($loweredScalarName === 'positive-int') { + return IntegerRangeType::createAllGreaterThan(0); + } + + if ($loweredScalarName === 'negative-int') { + return IntegerRangeType::createAllSmallerThan(0); + } + foreach (self::SCALAR_NAME_BY_TYPE as $objectType => $scalarNames) { if (! in_array($loweredScalarName, $scalarNames, true)) { continue; diff --git a/rules-tests/CodingStyle/Rector/ClassConst/VarConstantCommentRector/Fixture/skip_negative_int_over_int.php.inc b/rules-tests/CodingStyle/Rector/ClassConst/VarConstantCommentRector/Fixture/skip_negative_int_over_int.php.inc new file mode 100644 index 00000000000..741455bdda3 --- /dev/null +++ b/rules-tests/CodingStyle/Rector/ClassConst/VarConstantCommentRector/Fixture/skip_negative_int_over_int.php.inc @@ -0,0 +1,9 @@ +