Skip to content

Commit

Permalink
Updated Rector to commit 997e5f4ae8c388b8a12d16539e8109fe821e8fd6
Browse files Browse the repository at this point in the history
rectorphp/rector-src@997e5f4 [TypeDeclaration] Skip intersection with iterable on ReturnTypeDeclarationRector on php 8.1 feature enabled (#3022)
  • Loading branch information
TomasVotruba committed Oct 29, 2022
1 parent 0f38f37 commit b7fb439
Show file tree
Hide file tree
Showing 30 changed files with 547 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare (strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;

use PhpParser\Node;
use PhpParser\Node\Name;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Accessory\AccessoryLiteralStringType;
use PHPStan\Type\Type;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
/**
* @implements TypeMapperInterface<AccessoryLiteralStringType>
*/
final class AccessoryLiteralStringTypeMapper implements TypeMapperInterface
{
/**
* @readonly
* @var \Rector\Core\Php\PhpVersionProvider
*/
private $phpVersionProvider;
public function __construct(PhpVersionProvider $phpVersionProvider)
{
$this->phpVersionProvider = $phpVersionProvider;
}
/**
* @return class-string<Type>
*/
public function getNodeClass() : string
{
return AccessoryLiteralStringType::class;
}
/**
* @param AccessoryLiteralStringType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind) : TypeNode
{
return new IdentifierTypeNode('literal-string');
}
/**
* @param AccessoryLiteralStringType $type
*/
public function mapToPhpParserNode(Type $type, string $typeKind) : ?Node
{
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return new Name('string');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,23 @@
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Type;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
/**
* @implements TypeMapperInterface<AccessoryNonEmptyStringType>
*/
final class AccessoryNonEmptyStringTypeMapper implements TypeMapperInterface
{
/**
* @readonly
* @var \Rector\Core\Php\PhpVersionProvider
*/
private $phpVersionProvider;
public function __construct(PhpVersionProvider $phpVersionProvider)
{
$this->phpVersionProvider = $phpVersionProvider;
}
/**
* @return class-string<Type>
*/
Expand All @@ -27,13 +38,16 @@ public function getNodeClass() : string
*/
public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind) : TypeNode
{
return new IdentifierTypeNode('string');
return new IdentifierTypeNode('non-empty-string');
}
/**
* @param AccessoryNonEmptyStringType $type
*/
public function mapToPhpParserNode(Type $type, string $typeKind) : ?Node
{
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return new Name('string');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,23 @@
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\Type;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
/**
* @implements TypeMapperInterface<AccessoryNonFalsyStringType>
*/
final class AccessoryNonFalsyStringTypeMapper implements TypeMapperInterface
{
/**
* @readonly
* @var \Rector\Core\Php\PhpVersionProvider
*/
private $phpVersionProvider;
public function __construct(PhpVersionProvider $phpVersionProvider)
{
$this->phpVersionProvider = $phpVersionProvider;
}
/**
* @return class-string<Type>
*/
Expand All @@ -27,13 +38,16 @@ public function getNodeClass() : string
*/
public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind) : TypeNode
{
return new IdentifierTypeNode('string');
return new IdentifierTypeNode('non-falsy-string');
}
/**
* @param AccessoryNonFalsyStringType $type
*/
public function mapToPhpParserNode(Type $type, string $typeKind) : ?Node
{
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return new Name('string');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,23 @@
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Type;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
/**
* @implements TypeMapperInterface<AccessoryNumericStringType>
*/
final class AccessoryNumericStringTypeMapper implements TypeMapperInterface
{
/**
* @readonly
* @var \Rector\Core\Php\PhpVersionProvider
*/
private $phpVersionProvider;
public function __construct(PhpVersionProvider $phpVersionProvider)
{
$this->phpVersionProvider = $phpVersionProvider;
}
/**
* @return class-string<Type>
*/
Expand All @@ -27,13 +38,16 @@ public function getNodeClass() : string
*/
public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind) : TypeNode
{
return new IdentifierTypeNode('string');
return new IdentifierTypeNode('numeric-string');
}
/**
* @param AccessoryNumericStringType $type
*/
public function mapToPhpParserNode(Type $type, string $typeKind) : ?Node
{
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return new Name('string');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,35 @@
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Name;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
use RectorPrefix202210\Symfony\Contracts\Service\Attribute\Required;
/**
* @implements TypeMapperInterface<ClassStringType>
*/
final class ClassStringTypeMapper implements TypeMapperInterface
{
/**
* @var \Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper
* @readonly
* @var \Rector\PHPStanStaticTypeMapper\TypeMapper\GenericClassStringTypeMapper
*/
private $phpStanStaticTypeMapper;
private $genericClassStringTypeMapper;
/**
* @readonly
* @var \Rector\Core\Php\PhpVersionProvider
*/
private $phpVersionProvider;
public function __construct(\Rector\PHPStanStaticTypeMapper\TypeMapper\GenericClassStringTypeMapper $genericClassStringTypeMapper, PhpVersionProvider $phpVersionProvider)
{
$this->genericClassStringTypeMapper = $genericClassStringTypeMapper;
$this->phpVersionProvider = $phpVersionProvider;
}
/**
* @return class-string<Type>
*/
Expand All @@ -37,49 +45,19 @@ public function getNodeClass() : string
*/
public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind) : TypeNode
{
$attributeAwareIdentifierTypeNode = new IdentifierTypeNode('class-string');
if ($type instanceof GenericClassStringType) {
$genericType = $this->resolveGenericObjectType($type);
$genericTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($genericType, $typeKind);
return new GenericTypeNode($attributeAwareIdentifierTypeNode, [$genericTypeNode]);
return $this->genericClassStringTypeMapper->mapToPHPStanPhpDocTypeNode($type, $typeKind);
}
return $attributeAwareIdentifierTypeNode;
return new IdentifierTypeNode('class-string');
}
/**
* @param ClassStringType $type
*/
public function mapToPhpParserNode(Type $type, string $typeKind) : ?Node
{
return new Name('string');
}
/**
* @required
*/
public function autowire(PHPStanStaticTypeMapper $phpStanStaticTypeMapper) : void
{
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
}
private function normalizeType(string $classType) : string
{
if (\is_a($classType, Expr::class, \true)) {
return Expr::class;
}
if (\is_a($classType, Node::class, \true)) {
return Node::class;
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return $classType;
}
/**
* @return \PHPStan\Type\ObjectType|\PHPStan\Type\Type
*/
private function resolveGenericObjectType(GenericClassStringType $genericClassStringType)
{
$genericType = $genericClassStringType->getGenericType();
if (!$genericType instanceof ObjectType) {
return $genericType;
}
$className = $genericType->getClassName();
$className = $this->normalizeType($className);
return new ObjectType($className);
return new Name('string');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

declare (strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Name;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
use RectorPrefix202210\Symfony\Contracts\Service\Attribute\Required;
/**
* @implements TypeMapperInterface<GenericClassStringType>
*/
final class GenericClassStringTypeMapper implements TypeMapperInterface
{
/**
* @var \Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper
*/
private $phpStanStaticTypeMapper;
/**
* @readonly
* @var \Rector\Core\Php\PhpVersionProvider
*/
private $phpVersionProvider;
public function __construct(PhpVersionProvider $phpVersionProvider)
{
$this->phpVersionProvider = $phpVersionProvider;
}
/**
* @required
*/
public function autowire(PHPStanStaticTypeMapper $phpStanStaticTypeMapper) : void
{
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
}
/**
* @return class-string<Type>
*/
public function getNodeClass() : string
{
return GenericClassStringType::class;
}
/**
* @param GenericClassStringType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type, string $typeKind) : TypeNode
{
$attributeAwareIdentifierTypeNode = new IdentifierTypeNode('class-string');
$genericType = $this->resolveGenericObjectType($type);
$genericTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($genericType, $typeKind);
return new GenericTypeNode($attributeAwareIdentifierTypeNode, [$genericTypeNode]);
}
/**
* @param GenericClassStringType $type
*/
public function mapToPhpParserNode(Type $type, string $typeKind) : ?Node
{
if (!$this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return new Name('string');
}
/**
* @return \PHPStan\Type\ObjectType|\PHPStan\Type\Type
*/
private function resolveGenericObjectType(GenericClassStringType $genericClassStringType)
{
$genericType = $genericClassStringType->getGenericType();
if (!$genericType instanceof ObjectType) {
return $genericType;
}
$className = $genericType->getClassName();
$className = $this->normalizeType($className);
return new ObjectType($className);
}
private function normalizeType(string $classType) : string
{
if (\is_a($classType, Expr::class, \true)) {
return Expr::class;
}
if (\is_a($classType, Node::class, \true)) {
return Node::class;
}
return $classType;
}
}
Loading

0 comments on commit b7fb439

Please sign in to comment.