Skip to content

Commit

Permalink
Use isInteger() (#3593)
Browse files Browse the repository at this point in the history
* Use isInteger() (#3571)

(cherry picked from commit 59cdfc8)

* Fix CS

* Add similar tests to RemoveUselessReturnTagRector from RemoveUselessParamTagRector
  • Loading branch information
yguedidi committed Apr 9, 2023
1 parent 08660ad commit 9629890
Show file tree
Hide file tree
Showing 18 changed files with 91 additions and 27 deletions.
3 changes: 1 addition & 2 deletions packages/NodeTypeResolver/NodeTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -239,7 +238,7 @@ public function getNativeType(Expr $expr): Type
public function isNumberType(Expr $expr): bool
{
$nodeType = $this->getType($expr);
if ($nodeType instanceof IntegerType) {
if ($nodeType->isInteger()->yes()) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -71,7 +70,7 @@ private function isScalarType(Type $type): bool
return true;
}

if ($type instanceof IntegerType) {
if ($type->isInteger()->yes()) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\NodeTypeResolver\TypeComparator;

use PHPStan\Type\ClassStringType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\Type;

/**
Expand All @@ -23,7 +22,7 @@ public function areEqualScalar(Type $firstType, Type $secondType): bool
return $firstTypeClass === $secondTypeClass;
}

if ($firstType instanceof IntegerType && $secondType instanceof IntegerType) {
if ($firstType->isInteger()->yes() && $secondType->isInteger()->yes()) {
return true;
}

Expand Down Expand Up @@ -78,7 +77,7 @@ private function isScalarType(Type $type): bool
return true;
}

if ($type instanceof IntegerType) {
if ($type->isInteger()->yes()) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IterableType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -115,7 +114,7 @@ public function isScalar(UnionType $unionType): bool
continue;
}

if ($type instanceof IntegerType) {
if ($type->isInteger()->yes()) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -248,7 +247,7 @@ private function isPairClassTooDetailed(Type $itemType): bool

private function isIntegerKeyAndNonNestedArray(ArrayType $arrayType): bool
{
if (! $arrayType->getKeyType() instanceof IntegerType) {
if (! $arrayType->getKeyType()->isInteger()->yes()) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\IntegerType;
use PHPStan\Type\TypeCombinator;
use Rector\Core\PhpParser\Node\NodeFactory;
use Rector\StaticTypeMapper\StaticTypeMapper;
Expand Down Expand Up @@ -91,7 +90,7 @@ private function correctItemByParameterReflection(
$clearParameterType = TypeCombinator::removeNull($parameterType);

// correct type
if ($clearParameterType instanceof IntegerType && $item instanceof String_) {
if ($clearParameterType->isInteger()->yes() && $item instanceof String_) {
return new LNumber((int) $item->value);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

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

use Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector\Source\SomeConstantFloatType;

final class KeepConstantFloatType
{
/**
* @return SomeConstantFloatType::*
*/
public function run(): float
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

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

use Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector\Source\SomeConstantIntegerType;

final class KeepConstantIntegerType
{
/**
* @return SomeConstantIntegerType::*
*/
public function run(): int
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

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

use Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector\Source\SomeConstantType;

final class KeepConstantType
{
/**
* @return SomeConstantType::*
*/
public function run(): string
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

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

final class SomeConstantFloatType
{
public const ONE_HALF = 1.5;

public const TWO_HALF = 2.5;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

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

final class SomeConstantIntegerType
{
public const ONE = 1;

public const TWO = 2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

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

final class SomeConstantType
{
public const YES = 'yes';

public const NO = 'no';
}
3 changes: 1 addition & 2 deletions rules/CodeQuality/Rector/If_/ExplicitBoolCompareRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\If_;
use PHPStan\Type\IntegerType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -158,7 +157,7 @@ private function resolveNewConditionNode(Expr $expr, bool $isNegated): ?BinaryOp
}

$exprType = $this->getType($expr);
if ($exprType instanceof IntegerType) {
if ($exprType->isInteger()->yes()) {
return $this->resolveInteger($isNegated, $expr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\LNumber;
use PHPStan\Type\IntegerType;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
Expand Down Expand Up @@ -65,7 +64,7 @@ public function refactor(Node $node): ?Node
}

$secondArgType = $this->getType($node->args[2]->value);
if ($secondArgType instanceof IntegerType) {
if ($secondArgType->isInteger()->yes()) {
return null;
}

Expand Down
3 changes: 1 addition & 2 deletions rules/Php80/NodeAnalyzer/SwitchAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PhpParser\Node\Stmt\Case_;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Switch_;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
Expand Down Expand Up @@ -42,7 +41,7 @@ public function hasDifferentTypeCases(array $cases, Expr $expr): bool
$uniqueTypes = $this->typeFactory->uniquateTypes($types);
$countUniqueTypes = count($uniqueTypes);

if ($countUniqueTypes === 1 && $uniqueTypes[0] instanceof IntegerType) {
if ($countUniqueTypes === 1 && $uniqueTypes[0]->isInteger()->yes()) {
$switchCondType = $this->nodeTypeResolver->getType($expr);
if (! $switchCondType instanceof MixedType && $switchCondType->isString()->maybe()) {
return true;
Expand Down
5 changes: 2 additions & 3 deletions rules/Strict/NodeFactory/ExactCompareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use PHPStan\Type\IntegerType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
Expand All @@ -39,7 +38,7 @@ public function createIdenticalFalsyCompare(
return new Identical($expr, new String_(''));
}

if ($exprType instanceof IntegerType) {
if ($exprType->isInteger()->yes()) {
return new Identical($expr, new LNumber(0));
}

Expand Down Expand Up @@ -71,7 +70,7 @@ public function createNotIdenticalFalsyCompare(
return new NotIdentical($expr, new String_(''));
}

if ($exprType instanceof IntegerType) {
if ($exprType->isInteger()->yes()) {
return new NotIdentical($expr, new LNumber(0));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private function isScalarType(Type $type): bool
return true;
}

if ($type instanceof IntegerType) {
if ($type->isInteger()->yes()) {
return true;
}

Expand Down
7 changes: 3 additions & 4 deletions rules/TypeDeclaration/TypeInferer/ReturnTypeInferer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -145,7 +144,7 @@ static function (Node $subNode): bool {

if ($resolvedType instanceof UnionType) {
$benevolentUnionTypeIntegerType = $this->resolveBenevolentUnionTypeInteger($functionLike, $resolvedType);
if ($benevolentUnionTypeIntegerType instanceof IntegerType) {
if ($benevolentUnionTypeIntegerType->isInteger()->yes()) {
return $benevolentUnionTypeIntegerType;
}
}
Expand All @@ -156,15 +155,15 @@ static function (Node $subNode): bool {
private function resolveBenevolentUnionTypeInteger(
ClassMethod|Function_|Closure|ArrowFunction $functionLike,
UnionType $unionType
): UnionType|IntegerType {
): Type {
$types = $unionType->getTypes();
$countTypes = count($types);

if ($countTypes !== 2) {
return $unionType;
}

if (! ($types[0] instanceof IntegerType && $types[1]->isString()->yes())) {
if (! ($types[0]->isInteger()->yes() && $types[1]->isString()->yes())) {
return $unionType;
}

Expand Down

0 comments on commit 9629890

Please sign in to comment.