Updated Rector to commit 3949fc260b51c4355f86d21a26db7d2e4cae0b49

3949fc260b Move instanceof PHPStan Type to ->is*() take 1 (#6416)
This commit is contained in:
Tomas Votruba 2024-11-12 00:20:18 +00:00
parent d606b0a837
commit 0b834e6092
32 changed files with 67 additions and 95 deletions

View File

@ -15,7 +15,6 @@ use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeTraverser;
use PHPStan\Type\NullType;
use PHPStan\Type\UnionType;
use PHPStan\Type\VoidType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
@ -156,7 +155,7 @@ CODE_SAMPLE
$newTypes = [];
$hasChanged = \false;
foreach ($returnType->getTypes() as $type) {
if ($type instanceof VoidType) {
if ($type->isVoid()->yes()) {
$type = new NullType();
$hasChanged = \true;
}

View File

@ -17,8 +17,8 @@ use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Property;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ArrayType;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use Rector\NodeAnalyzer\ExprAnalyzer;
use Rector\Php\ReservedKeywordAnalyzer;
use Rector\PhpParser\AstResolver;
@ -113,7 +113,7 @@ CODE_SAMPLE
}
private function isAllowedExpr(Expr $expr, Scope $scope) : bool
{
if (!$scope->getType($expr) instanceof ArrayType) {
if (!$scope->getType($expr)->isArray()->yes()) {
return \false;
}
if ($expr instanceof Variable) {
@ -136,7 +136,7 @@ CODE_SAMPLE
$phpPropertyReflection = $classReflection->getNativeProperty($propertyName);
$nativeType = $phpPropertyReflection->getNativeType();
if (!$nativeType instanceof MixedType) {
return $nativeType instanceof ArrayType;
return $nativeType->isArray()->yes();
}
$property = $this->astResolver->resolvePropertyFromPropertyReflection($phpPropertyReflection);
/**
@ -149,6 +149,9 @@ CODE_SAMPLE
return \false;
}
$type = $this->allAssignNodePropertyTypeInferer->inferProperty($property, $classReflection, $this->file);
return $type instanceof ArrayType;
if (!$type instanceof Type) {
return \false;
}
return $type->isArray()->yes();
}
}

View File

@ -13,7 +13,6 @@ use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
@ -200,10 +199,10 @@ CODE_SAMPLE
if (\count($types) > 2) {
return null;
}
if ($types[0] instanceof FullyQualifiedObjectType && $types[1] instanceof NullType && $className === $types[0]->getClassName()) {
if ($types[0] instanceof FullyQualifiedObjectType && $types[1]->isNull()->yes() && $className === $types[0]->getClassName()) {
return $this->createDirectReturn($expression, $expr, $unionType);
}
if ($types[0] instanceof NullType && $types[1] instanceof FullyQualifiedObjectType && $className === $types[1]->getClassName()) {
if ($types[0]->isNull()->yes() && $types[1] instanceof FullyQualifiedObjectType && $className === $types[1]->getClassName()) {
return $this->createDirectReturn($expression, $expr, $unionType);
}
if ($this->isNotTypedNullable($types, $className)) {
@ -219,7 +218,7 @@ CODE_SAMPLE
if (!$types[0] instanceof ObjectType) {
return \true;
}
if (!$types[1] instanceof NullType) {
if (!$types[1]->isNull()->yes()) {
return \true;
}
return $className !== $types[0]->getClassName();

View File

@ -6,7 +6,6 @@ namespace Rector\DeadCode\Rector\If_;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Stmt\If_;
use PHPStan\Type\Constant\ConstantBooleanType;
use Rector\DeadCode\NodeAnalyzer\SafeLeftTypeBooleanAndOrAnalyzer;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@ -72,10 +71,7 @@ CODE_SAMPLE
}
$booleanOr = $node->cond;
$conditionStaticType = $this->getType($booleanOr->left);
if (!$conditionStaticType instanceof ConstantBooleanType) {
return null;
}
if ($conditionStaticType->getValue()) {
if (!$conditionStaticType->isFalse()->yes()) {
return null;
}
if (!$this->safeLeftTypeBooleanAndOrAnalyzer->isSafe($booleanOr)) {

View File

@ -17,8 +17,6 @@ use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\If_;
use PhpParser\NodeTraverser;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\IntersectionType;
use Rector\DeadCode\NodeAnalyzer\SafeLeftTypeBooleanAndOrAnalyzer;
use Rector\NodeAnalyzer\ExprAnalyzer;
@ -104,10 +102,7 @@ CODE_SAMPLE
return null;
}
$conditionStaticType = $this->getType($node->cond);
if (!$conditionStaticType instanceof ConstantBooleanType) {
return null;
}
if (!$conditionStaticType->getValue()) {
if (!$conditionStaticType->isTrue()->yes()) {
return null;
}
if ($this->shouldSkipExpr($node->cond)) {
@ -136,7 +131,7 @@ CODE_SAMPLE
$type = $this->getType($variable);
if ($type instanceof IntersectionType) {
foreach ($type->getTypes() as $subType) {
if ($subType instanceof ArrayType) {
if ($subType->isArray()->yes()) {
return \true;
}
}
@ -155,10 +150,7 @@ CODE_SAMPLE
}
$booleanAnd = $if->cond;
$leftType = $this->getType($booleanAnd->left);
if (!$leftType instanceof ConstantBooleanType) {
return null;
}
if (!$leftType->getValue()) {
if (!$leftType->isTrue()->yes()) {
return null;
}
if (!$this->safeLeftTypeBooleanAndOrAnalyzer->isSafe($booleanAnd)) {

View File

@ -20,7 +20,6 @@ use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\While_;
use PhpParser\NodeTraverser;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\ConstantType;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
@ -143,7 +142,7 @@ CODE_SAMPLE
private function processVariableNum(Continue_ $continue, Variable $numVariable)
{
$staticType = $this->getType($numVariable);
if (!$staticType instanceof ConstantType) {
if (!$staticType->isConstantValue()->yes()) {
return $continue;
}
if (!$staticType instanceof ConstantIntegerType) {

View File

@ -10,7 +10,6 @@ use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Continue_;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\ConstantType;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
@ -106,7 +105,7 @@ CODE_SAMPLE
private function processVariableNum($stmt, Variable $numVariable) : ?Node
{
$staticType = $this->getType($numVariable);
if ($staticType instanceof ConstantType) {
if ($staticType->isConstantValue()->yes()) {
if ($staticType instanceof ConstantIntegerType) {
if ($staticType->getValue() === 0) {
$stmt->num = null;

View File

@ -11,7 +11,6 @@ use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Stmt\Class_;
use PhpParser\NodeTraverser;
use PHPStan\Analyser\Scope;
use PHPStan\Type\NullType;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\ValueObject\PhpVersionFeature;
@ -87,7 +86,7 @@ CODE_SAMPLE
}
$firstArgValue = $firstArg->value;
$firstArgType = $this->getType($firstArgValue);
if (!$this->nodeTypeResolver->isNullableType($firstArgValue) && !$firstArgType instanceof NullType) {
if (!$this->nodeTypeResolver->isNullableType($firstArgValue) && !$firstArgType->isNull()->yes()) {
return null;
}
$notIdentical = new NotIdentical($firstArgValue, $this->nodeFactory->createNull());

View File

@ -21,7 +21,6 @@ use PHPStan\Reflection\Native\NativeParameterWithPhpDocsReflection;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Type\ErrorType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\NodeAnalyzer\ArgsAnalyzer;
@ -222,7 +221,7 @@ CODE_SAMPLE
}
private function shouldSkipType(Type $type) : bool
{
return !$type instanceof MixedType && !$type instanceof NullType && !$this->isValidUnionType($type);
return !$type instanceof MixedType && !$type->isNull()->yes() && !$this->isValidUnionType($type);
}
private function shouldSkipTrait(Expr $expr, Type $type, bool $isTrait) : bool
{

View File

@ -20,7 +20,6 @@ use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeWithClassName;
@ -54,7 +53,7 @@ final class ExactCompareFactory
return new Identical($expr, $this->nodeFactory->createFalse());
} elseif ($exprType->isArray()->yes()) {
return new Identical($expr, new Array_([]));
} elseif ($exprType instanceof NullType) {
} elseif ($exprType->isNull()->yes()) {
return new Identical($expr, $this->nodeFactory->createNull());
} elseif (!$exprType instanceof UnionType) {
return null;

View File

@ -9,7 +9,6 @@ use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
@ -107,7 +106,7 @@ final class CallTypesResolver
if (\count($staticTypeByArgumentPosition) !== 1) {
return $staticTypeByArgumentPosition;
}
if (!$staticTypeByArgumentPosition[0] instanceof NullType) {
if (!$staticTypeByArgumentPosition[0]->isNull()->yes()) {
return $staticTypeByArgumentPosition;
}
return [new MixedType()];

View File

@ -21,7 +21,6 @@ use PhpParser\Node\UnionType;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Rector\PhpParser\AstResolver;
@ -82,7 +81,7 @@ final class CallerParamMatcher
if ($this->typeComparator->isSubtype($defaultType, $callParamType)) {
return $callParam->type;
}
if (!$defaultType instanceof NullType) {
if (!$defaultType->isNull()->yes()) {
return null;
}
if ($callParam->type instanceof Name || $callParam->type instanceof Identifier) {

View File

@ -8,7 +8,6 @@ use PhpParser\Node\Expr;
use PhpParser\Node\Identifier;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\CallableType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
@ -106,10 +105,10 @@ final class ClassMethodParamTypeCompleter
}
private function isClosureAndCallableType(Type $parameterStaticType, Type $argumentStaticType) : bool
{
if ($parameterStaticType instanceof CallableType && $this->isClosureObjectType($argumentStaticType)) {
if ($parameterStaticType->isCallable()->yes() && $this->isClosureObjectType($argumentStaticType)) {
return \true;
}
return $argumentStaticType instanceof CallableType && $this->isClosureObjectType($parameterStaticType);
return $argumentStaticType->isCallable()->yes() && $this->isClosureObjectType($parameterStaticType);
}
private function isClosureObjectType(Type $type) : bool
{

View File

@ -24,7 +24,6 @@ use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IterableType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StringType;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareIntersectionTypeNode;
@ -50,7 +49,7 @@ final class TypeExpressionFromVarTagResolver
$arg = new Arg($variable);
return new FuncCall(new Name($scalarTypeFunction), [$arg]);
}
if ($scalarType instanceof NullType) {
if ($scalarType->isNull()->yes()) {
return new Identical($variable, new ConstFetch(new Name('null')));
}
if ($scalarType instanceof ConstantBooleanType) {

View File

@ -28,7 +28,6 @@ use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\BooleanType;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractScopeAwareRector;
@ -177,9 +176,11 @@ CODE_SAMPLE
return \false;
}
foreach ($functionReflection->getVariants() as $parametersAcceptorWithPhpDoc) {
return $parametersAcceptorWithPhpDoc->getNativeReturnType() instanceof BooleanType;
if (!$parametersAcceptorWithPhpDoc->getNativeReturnType()->isBoolean()->yes()) {
return \false;
}
}
return \false;
return \true;
}
private function isBooleanOp(Expr $expr) : bool
{

View File

@ -15,7 +15,6 @@ use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\UnionType as PhpParserUnionType;
use PHPStan\Analyser\Scope;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\UnionType;
use Rector\Php\PhpVersionProvider;
@ -194,7 +193,7 @@ CODE_SAMPLE
private function processSingleUnionType($node, UnionType $unionType, NullableType $nullableType)
{
$types = $unionType->getTypes();
$returnType = $types[0] instanceof ObjectType && $types[1] instanceof NullType ? new NullableType(new FullyQualified($types[0]->getClassName())) : $nullableType;
$returnType = $types[0] instanceof ObjectType && $types[1]->isNull()->yes() ? new NullableType(new FullyQualified($types[0]->getClassName())) : $nullableType;
$node->returnType = $returnType;
return $node;
}

View File

@ -5,7 +5,6 @@ namespace Rector\TypeDeclaration\TypeAnalyzer;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\PropertyProperty;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Type;
use Rector\StaticTypeMapper\StaticTypeMapper;
final class PropertyTypeDefaultValueAnalyzer
@ -26,7 +25,7 @@ final class PropertyTypeDefaultValueAnalyzer
}
// the defaults can be in conflict
$defaultType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($propertyProperty->default);
if ($defaultType instanceof ArrayType && $propertyType instanceof ArrayType) {
if ($defaultType->isArray()->yes() && $propertyType->isArray()->yes()) {
return \false;
}
// type is not matching, skip it

View File

@ -195,7 +195,7 @@ final class TrustedClassMethodPropertyTypeInferer
}
if ($param->default instanceof Expr) {
$defaultValueStaticType = $this->nodeTypeResolver->getType($param->default);
if ($defaultValueStaticType instanceof NullType) {
if ($defaultValueStaticType->isNull()->yes()) {
return \true;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '29e89320f0b5c81e2130320ac55f0821daaf4e14';
public const PACKAGE_VERSION = '3949fc260b51c4355f86d21a26db7d2e4cae0b49';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-11-10 00:33:41';
public const RELEASE_DATE = '2024-11-12 07:18:02';
/**
* @var int
*/

View File

@ -4,8 +4,6 @@ declare (strict_types=1);
namespace Rector\NodeAnalyzer;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\CallableType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
@ -25,7 +23,7 @@ final class PropertyAnalyzer
public function hasForbiddenType(Property $property) : bool
{
$propertyType = $this->nodeTypeResolver->getType($property);
if ($propertyType instanceof NullType) {
if ($propertyType->isNull()->yes()) {
return \true;
}
if ($this->isForbiddenType($propertyType)) {
@ -54,6 +52,6 @@ final class PropertyAnalyzer
if ($type instanceof TypeWithClassName && $type->getClassName() === 'Closure') {
return \false;
}
return $type instanceof CallableType;
return $type->isCallable()->yes();
}
}

View File

@ -381,7 +381,7 @@ final class NodeTypeResolver
}
private function isUnionTypeable(Type $first, Type $second) : bool
{
return !$first instanceof UnionType && !$second instanceof UnionType && !$second instanceof NullType;
return !$first instanceof UnionType && !$second instanceof UnionType && !$second->isNull()->yes();
}
private function isMatchingUnionType(Type $resolvedType, ObjectType $requiredObjectType) : bool
{

View File

@ -7,7 +7,6 @@ use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
@ -41,7 +40,7 @@ final class StaticTypeAnalyzer
if ($type instanceof ObjectType) {
return \true;
}
if ($type instanceof ConstantScalarType && !$type instanceof NullType) {
if ($type instanceof ConstantScalarType && !$type->isNull()->yes()) {
return (bool) $type->getValue();
}
if ($type->isScalar()->yes()) {

View File

@ -6,7 +6,6 @@ namespace Rector\NodeTypeResolver\PHPStan\Type;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
@ -93,15 +92,13 @@ final class TypeFactory
}
private function normalizeBooleanType(bool &$hasFalse, bool &$hasTrue, Type $type) : Type
{
if ($type instanceof ConstantBooleanType) {
if ($type->getValue()) {
$hasTrue = \true;
}
if ($type->getValue() === \false) {
$hasFalse = \true;
}
if ($type->isTrue()->yes()) {
$hasTrue = \true;
}
if ($hasFalse && $hasTrue && $type instanceof ConstantBooleanType) {
if ($type->isFalse()->yes()) {
$hasFalse = \true;
}
if ($hasFalse && $hasTrue && ($type->isTrue()->yes() || $type->isFalse()->yes())) {
return new BooleanType();
}
return $type;
@ -160,7 +157,7 @@ final class TypeFactory
if ($type instanceof ConstantIntegerType) {
return new IntegerType();
}
if ($type instanceof ConstantBooleanType) {
if ($type->isTrue()->yes() || $type->isFalse()->yes()) {
return new BooleanType();
}
return $type;

View File

@ -3,7 +3,6 @@
declare (strict_types=1);
namespace Rector\NodeTypeResolver\TypeComparator;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Type;
/**
* @see \Rector\Tests\NodeTypeResolver\TypeComparator\ScalarTypeComparatorTest
@ -53,7 +52,7 @@ final class ScalarTypeComparator
if (!$firstType->isString()->yes()) {
return \get_class($firstType) !== \get_class($secondType);
}
if (!$secondType instanceof ClassStringType) {
if (!$secondType->isClassStringType()->yes()) {
return \get_class($firstType) !== \get_class($secondType);
}
return \false;

View File

@ -8,7 +8,6 @@ use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\MixedType;
@ -174,7 +173,7 @@ final class TypeComparator
private function normalizeConstantBooleanType(Type $type) : Type
{
return TypeTraverser::map($type, static function (Type $type, callable $callable) : Type {
if ($type instanceof ConstantBooleanType) {
if ($type->isTrue()->yes() || $type->isFalse()->yes()) {
return new BooleanType();
}
return $callable($type);

View File

@ -3,7 +3,6 @@
declare (strict_types=1);
namespace Rector\PHPStanStaticTypeMapper;
use PHPStan\Type\ArrayType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
@ -14,20 +13,20 @@ final class DoctrineTypeAnalyzer
if (!$type instanceof UnionType) {
return \false;
}
$arrayType = null;
$isArrayType = \false;
$hasDoctrineCollectionType = \false;
foreach ($type->getTypes() as $unionedType) {
if ($this->isInstanceOfCollectionType($unionedType)) {
$hasDoctrineCollectionType = \true;
}
if ($unionedType instanceof ArrayType) {
$arrayType = $unionedType;
if ($unionedType->isArray()->yes()) {
$isArrayType = \true;
}
}
if (!$hasDoctrineCollectionType) {
return \false;
}
return $arrayType instanceof ArrayType;
return $isArrayType;
}
public function isInstanceOfCollectionType(Type $type) : bool
{

View File

@ -176,7 +176,7 @@ final class ArrayTypeMapper implements TypeMapperInterface
if (!$arrayType->getKeyType()->isInteger()->yes()) {
return \false;
}
return !$arrayType->getItemType() instanceof ArrayType;
return !$arrayType->getItemType()->isArray()->yes();
}
private function isClassStringArrayType(ArrayType $arrayType) : bool
{

View File

@ -7,7 +7,6 @@ use PhpParser\Node;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Type;
use Rector\Php\PhpVersionProvider;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
@ -49,11 +48,17 @@ final class BooleanTypeMapper implements TypeMapperInterface
if ($typeKind === TypeKind::PROPERTY) {
return new Identifier('bool');
}
if ($typeKind === TypeKind::UNION && $type instanceof ConstantBooleanType && $type->getValue() === \false) {
if ($typeKind === TypeKind::UNION && $type->isFalse()->yes()) {
return new Identifier('false');
}
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::NULL_FALSE_TRUE_STANDALONE_TYPE) && $type instanceof ConstantBooleanType) {
return $type->getValue() ? new Identifier('true') : new Identifier('false');
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::NULL_FALSE_TRUE_STANDALONE_TYPE)) {
return new Identifier('bool');
}
if ($type->isTrue()->yes()) {
return new Identifier('true');
}
if ($type->isFalse()->yes()) {
return new Identifier('false');
}
return new Identifier('bool');
}

View File

@ -12,7 +12,6 @@ use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\NullableType;
use PhpParser\Node\UnionType as PhpParserUnionType;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\CallableType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
@ -164,7 +163,7 @@ final class UnionTypeMapper implements TypeMapperInterface
return null;
}
// special callable type only not allowed on property
if ($typeKind === TypeKind::PROPERTY && $unionedType instanceof CallableType) {
if ($typeKind === TypeKind::PROPERTY && $unionedType->isCallable()->yes()) {
return null;
}
$phpParserUnionedTypes[] = $phpParserNode;

View File

@ -3,7 +3,6 @@
declare (strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\Utils;
use PHPStan\Type\CallableType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeWithClassName;
@ -23,13 +22,13 @@ final class TypeUnwrapper
}
return $type;
}
public function unwrapFirstCallableTypeFromUnionType(Type $type) : ?Type
public function unwrapFirstCallableTypeFromUnionType(Type $type) : Type
{
if (!$type instanceof UnionType) {
return $type;
}
foreach ($type->getTypes() as $unionedType) {
if (!$unionedType instanceof CallableType) {
if (!$unionedType->isCallable()->yes()) {
continue;
}
return $unionedType;

View File

@ -11,7 +11,6 @@ use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\IntegerType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\Php80\ValueObject\AnnotationToAttribute;
@ -71,14 +70,14 @@ final class AnnotationToAttributeIntegerValueCaster
}
private function containsInteger(Type $type) : bool
{
if ($type instanceof IntegerType) {
if ($type->isInteger()->yes()) {
return \true;
}
if (!$type instanceof UnionType) {
return \false;
}
foreach ($type->getTypes() as $unionedType) {
if ($unionedType instanceof IntegerType) {
if ($unionedType->isInteger()->yes()) {
return \true;
}
}

File diff suppressed because one or more lines are too long