Skip to content

Commit

Permalink
Fix integer range phpdoc types being treated as useless (#3825)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlherren committed May 13, 2023
1 parent 156d28c commit 623a077
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Iterator;
use PHPStan\Type\BooleanType;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPUnit\Framework\Attributes\DataProvider;
Expand All @@ -31,12 +33,14 @@ public function test(Type $firstType, Type $secondType, bool $areExpectedEqual):
}

/**
* @return Iterator<bool[]|BooleanType[]|StringType[]>
* @return Iterator<array{Type, Type, bool}>
*/
public static function provideData(): Iterator
{
yield [new StringType(), new BooleanType(), false];
yield [new StringType(), new StringType(), true];
yield [new StringType(), new ClassStringType(), false];
yield [new IntegerType(), new IntegerType(), true];
yield [new IntegerType(), IntegerRangeType::fromInterval(1, 10), false];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ public function areEqualScalar(Type $firstType, Type $secondType): bool
}

if ($firstType->isInteger()->yes() && $secondType->isInteger()->yes()) {
return true;
// prevents "int<min, max>" vs "int"
$firstTypeClass = $firstType::class;
$secondTypeClass = $secondType::class;

return $firstTypeClass === $secondTypeClass;
}

if ($firstType->isFloat()->yes() && $secondType->isFloat()->yes()) {
Expand Down Expand Up @@ -56,6 +60,10 @@ public function areDifferentScalarTypes(Type $firstType, Type $secondType): bool
return false;
}

if ($firstType->isInteger()->yes() && $secondType->isInteger()->yes()) {
return false;
}

if (! $firstType->isString()->yes()) {
return $firstType::class !== $secondType::class;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector\Fixture;

final class SkipIntegerRangeType
{
/**
* @param positive-int $n
*/
public function run(int $n)
{
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector\Fixture;

final class SkipIntegerRangeType
{
/**
* @return positive-int
*/
public function run(): int
{
}
}

0 comments on commit 623a077

Please sign in to comment.