-
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TypeDeclaration] Skip intersection with iterable on ReturnTypeDeclar…
…ationRector on php 8.1 feature enabled (#3022) * [TypeDeclaration] Skip intersection with iterable on ReturnTypeDeclarationRector on php 8.1 feature enabled * update fixture * Fixed 🎉 * Fix * final touch: eol * Final touch: clean up manual check Name, use ReflectionProvider instead * Final touch: phpstan * Really Really Final touch: Clean up * rename fixture * Revert rename fixture This reverts commit d775f38. * Add GenericClassStringTypeMapper * phpstan * Final touch: clean up * Final touch: clean up * Final touch: remove unnecessary string type check * Fix CI not load bcadd function * final touch: phpstan load * final touch: map AccessoryLiteralStringTypeMapper to literal-string for PHPStan TypeNode * Final touch: map TypeNode for various String accessory type * Really Final touch: autowire PHPStanStaticTypeMapper on GenericClassStringTypeMapper * Final touch: move GenericTypeNode type from ClassStringTypeMapper to dedicated GenericClassStringTypeMapper * Final touch: move GenericTypeNode type from ClassStringTypeMapper to dedicated GenericClassStringTypeMapper * Really Really Final touch: ensure check support feature Scalar Types when return string name * Really Really Final touch: ensure check support feature Scalar Types when return string name * Really Really Final touch: inject to __construct for GenericClassStringTypeMapper dep * Final touch: avoid manually define list of excluded type by check aginst ObjectType after verify it is not ObjectWithoutClassType * Really Really Really Final touch: Remove unnecessary move logic to private method, as can be checked directly now * Really Final touch: typo fix
- Loading branch information
1 parent
af4cf28
commit 997e5f4
Showing
12 changed files
with
304 additions
and
65 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
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,54 @@ | ||
<?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 | ||
{ | ||
public function __construct( | ||
private readonly 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
97 changes: 97 additions & 0 deletions
97
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,97 @@ | ||
<?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 Symfony\Contracts\Service\Attribute\Required; | ||
|
||
/** | ||
* @implements TypeMapperInterface<GenericClassStringType> | ||
*/ | ||
final class GenericClassStringTypeMapper implements TypeMapperInterface | ||
{ | ||
private PHPStanStaticTypeMapper $phpStanStaticTypeMapper; | ||
|
||
public function __construct( | ||
private readonly 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'); | ||
} | ||
|
||
private function resolveGenericObjectType(GenericClassStringType $genericClassStringType): ObjectType|Type | ||
{ | ||
$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.