-
-
Notifications
You must be signed in to change notification settings - Fork 679
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated Rector to commit 997e5f4ae8c388b8a12d16539e8109fe821e8fd6
rectorphp/rector-src@997e5f4 [TypeDeclaration] Skip intersection with iterable on ReturnTypeDeclarationRector on php 8.1 feature enabled (#3022)
- Loading branch information
1 parent
0f38f37
commit b7fb439
Showing
30 changed files
with
547 additions
and
105 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
packages/PHPStanStaticTypeMapper/TypeMapper/AccessoryLiteralStringTypeMapper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
packages/PHPStanStaticTypeMapper/TypeMapper/GenericClassStringTypeMapper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.