Skip to content

Commit

Permalink
[CodingStyle] Skip positive-int and negative-int over int on VarConst…
Browse files Browse the repository at this point in the history
…antCommentRector (#3709)

* [CodingStyle] Skip positive-int over int on VarConstantCommentRector

* Fixed 🎉

* 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
  • Loading branch information
samsonasik committed Apr 29, 2023
1 parent 2bb26e3 commit ed8e74e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/StaticTypeMapper/Mapper/ScalarStringToTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\ClassConst\VarConstantCommentRector\Fixture;

final class SkipNegativeIntOverInt
{
/** @var negative-int */
public const POSITIVE_INT = -5;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\ClassConst\VarConstantCommentRector\Fixture;

final class SkipPositiveIntOverInt
{
/** @var positive-int */
public const POSITIVE_INT = 5;
}

0 comments on commit ed8e74e

Please sign in to comment.