Skip to content

Commit

Permalink
[TypeMapper] Use Identifier instead of Name on non-object types (#3375)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Feb 13, 2023
1 parent 7f97040 commit dbaf0da
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;

use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\CallableType;
Expand Down Expand Up @@ -59,6 +59,6 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
return null;
}

return new Name('callable');
return new Identifier('callable');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;

use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\FloatType;
Expand Down Expand Up @@ -49,6 +49,6 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
return null;
}

return new Name('float');
return new Identifier('float');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;

use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\MixedType;
Expand Down Expand Up @@ -53,6 +53,6 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
return null;
}

return new Name('mixed');
return new Identifier('mixed');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;

use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\NullType;
Expand Down Expand Up @@ -53,6 +53,6 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
return null;
}

return new Name('null');
return new Identifier('null');
}
}
17 changes: 1 addition & 16 deletions packages/PHPStanStaticTypeMapper/TypeMapper/ObjectTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,7 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
return null;
}

if (! $type instanceof GenericObjectType) {
// fallback
return new FullyQualified($type->getClassName());
}

if ($type->getClassName() === 'iterable') {
// fallback
return new Name('iterable');
}

if ($type->getClassName() !== 'object') {
// fallback
return new FullyQualified($type->getClassName());
}

return new Name('object');
return new FullyQualified($type->getClassName());
}

#[Required]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;

use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\StrictMixedType;
Expand Down Expand Up @@ -43,6 +43,6 @@ public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind): TypeNo
*/
public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
{
return new Name(self::MIXED);
return new Identifier(self::MIXED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ private function shouldSkipIterable(UnionType $unionType): bool
return $unionTypeAnalysis->hasArray();
}

private function matchArrayTypes(UnionType $unionType): Name | NullableType | PhpParserUnionType | null
private function matchArrayTypes(UnionType $unionType): Identifier | NullableType | PhpParserUnionType | null
{
$unionTypeAnalysis = $this->unionTypeAnalyzer->analyseForNullableAndIterable($unionType);
if (! $unionTypeAnalysis instanceof UnionTypeAnalysis) {
Expand All @@ -236,7 +236,7 @@ private function matchArrayTypes(UnionType $unionType): Name | NullableType | Ph
return $this->resolveNullableType(new NullableType($type));
}

return new Name($type);
return new Identifier($type);
}

private function resolveUnionTypes(PhpParserUnionType $phpParserUnionType): ?PhpParserUnionType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;

use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -60,6 +60,6 @@ public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
return null;
}

return new Name(self::VOID);
return new Identifier(self::VOID);
}
}
4 changes: 2 additions & 2 deletions rules/Php80/Rector/Class_/StringableForToStringRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Cast\String_ as CastString_;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
Expand Down Expand Up @@ -118,7 +118,7 @@ public function refactor(Node $node): ?Node
// add return type

if ($toStringClassMethod->returnType === null) {
$toStringClassMethod->returnType = new Name('string');
$toStringClassMethod->returnType = new Identifier('string');
}

return $node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
Expand Down Expand Up @@ -88,7 +88,7 @@ public function refactor(Node $node): ?Node

if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::NEVER_TYPE)) {
// never-type supported natively
$node->returnType = new Name('never');
$node->returnType = new Identifier('never');
} else {
// static anlysis based never type
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
Expand Down

0 comments on commit dbaf0da

Please sign in to comment.