Merge pull request #2664 from rectorphp/static-type-mapper-collector

Add TypeMapper collector - part #1
This commit is contained in:
Tomas Votruba 2020-01-14 21:39:28 +01:00 committed by GitHub
commit f0c0f03c6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 917 additions and 144 deletions

View File

@ -325,7 +325,7 @@ final class MyFirstRector extends AbstractRector
public function refactor(Node $node): ?Node
{
// we only care about "set*" method names
if (! $this->isName($node, 'set*')) {
if (! $this->isName($node->name, 'set*')) {
// return null to skip it
return null;
}

View File

@ -763,7 +763,7 @@ const SOME_CLASS_CONSTANT = 'default value';
#### `PhpParser\Node\Stmt\ClassMethod`
```php
function someMethod()
public function someMethod()
{
}
```

View File

@ -6,3 +6,4 @@ services:
Rector\PHPStanStaticTypeMapper\:
resource: '../src'
exclude:
- '../src/ValueObject/*'

View File

@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\Contract;
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
interface PHPStanStaticTypeMapperAwareInterface
{
public function setPHPStanStaticTypeMapper(PHPStanStaticTypeMapper $phpStanStaticTypeMapper): void;
}

View File

@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\Contract;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\NullableType;
use PhpParser\Node\UnionType;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Type;
interface TypeMapperInterface
{
public function getNodeClass(): string;
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode;
/**
* @return Identifier|Name|NullableType|UnionType|null
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node;
}

View File

@ -8,15 +8,15 @@ use Closure;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\NullableType;
use PhpParser\Node\UnionType as PhpParserUnionType;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\CallableType;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\ClosureType;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
@ -30,10 +30,10 @@ use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\ResourceType;
use PHPStan\Type\StaticType;
use PHPStan\Type\StringType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use PHPStan\Type\VoidType;
use Rector\AttributeAwarePhpDoc\Ast\Type\AttributeAwareUnionTypeNode;
use Rector\Exception\NotImplementedException;
@ -42,9 +42,10 @@ use Rector\NodeTypeResolver\ClassExistenceStaticHelper;
use Rector\Php\PhpVersionProvider;
use Rector\PHPStan\Type\AliasedObjectType;
use Rector\PHPStan\Type\FullyQualifiedObjectType;
use Rector\PHPStan\Type\ParentStaticType;
use Rector\PHPStan\Type\SelfObjectType;
use Rector\PHPStan\Type\ShortenedObjectType;
use Rector\PHPStanStaticTypeMapper\Contract\PHPStanStaticTypeMapperAwareInterface;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\ValueObject\PhpVersionFeature;
use Traversable;
@ -55,30 +56,24 @@ final class PHPStanStaticTypeMapper
*/
private $phpVersionProvider;
public function __construct(PhpVersionProvider $phpVersionProvider)
/**
* @var TypeMapperInterface[]
*/
private $typeMappers = [];
/**
* @param TypeMapperInterface[] $typeMappers
*/
public function __construct(PhpVersionProvider $phpVersionProvider, array $typeMappers)
{
$this->phpVersionProvider = $phpVersionProvider;
$this->typeMappers = $typeMappers;
}
public function mapToPHPStanPhpDocTypeNode(Type $phpStanType)
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
if ($phpStanType instanceof MixedType) {
return new IdentifierTypeNode('mixed');
}
if ($phpStanType instanceof UnionType) {
$unionTypesNodes = [];
foreach ($phpStanType->getTypes() as $unionedType) {
$unionTypesNodes[] = $this->mapToPHPStanPhpDocTypeNode($unionedType);
}
$unionTypesNodes = array_unique($unionTypesNodes);
return new AttributeAwareUnionTypeNode($unionTypesNodes);
}
if ($phpStanType instanceof ArrayType || $phpStanType instanceof IterableType) {
$itemTypeNode = $this->mapToPHPStanPhpDocTypeNode($phpStanType->getItemType());
if ($type instanceof ArrayType || $type instanceof IterableType) {
$itemTypeNode = $this->mapToPHPStanPhpDocTypeNode($type->getItemType());
if ($itemTypeNode instanceof UnionTypeNode) {
return $this->convertUnionArrayTypeNodesToArrayTypeOfUnionTypeNodes($itemTypeNode);
@ -87,39 +82,20 @@ final class PHPStanStaticTypeMapper
return new ArrayTypeNode($itemTypeNode);
}
if ($phpStanType instanceof IntegerType) {
return new IdentifierTypeNode('int');
foreach ($this->typeMappers as $typeMapper) {
if (! is_a($type, $typeMapper->getNodeClass(), true)) {
continue;
}
// prevents circular dependency
if ($typeMapper instanceof PHPStanStaticTypeMapperAwareInterface) {
$typeMapper->setPHPStanStaticTypeMapper($this);
}
return $typeMapper->mapToPHPStanPhpDocTypeNode($type);
}
if ($phpStanType instanceof ClassStringType) {
return new IdentifierTypeNode('class-string');
}
if ($phpStanType instanceof StringType) {
return new IdentifierTypeNode('string');
}
if ($phpStanType instanceof BooleanType) {
return new IdentifierTypeNode('bool');
}
if ($phpStanType instanceof FloatType) {
return new IdentifierTypeNode('float');
}
if ($phpStanType instanceof ObjectType) {
return new IdentifierTypeNode('\\' . $phpStanType->getClassName());
}
if ($phpStanType instanceof NullType) {
return new IdentifierTypeNode('null');
}
if ($phpStanType instanceof NeverType) {
return new IdentifierTypeNode('mixed');
}
throw new NotImplementedException(__METHOD__ . ' for ' . get_class($phpStanType));
throw new NotImplementedException(__METHOD__ . ' for ' . get_class($type));
}
/**
@ -144,54 +120,19 @@ final class PHPStanStaticTypeMapper
return new Identifier('self');
}
if ($phpStanType instanceof IntegerType) {
if ($this->phpVersionProvider->isAtLeast(PhpVersionFeature::SCALAR_TYPES)) {
return new Identifier('int');
foreach ($this->typeMappers as $typeMapper) {
// it cannot be is_a for SelfObjectType, because type classes inherit from each other
if (! is_a($phpStanType, $typeMapper->getNodeClass(), true)) {
continue;
}
return null;
}
if ($phpStanType instanceof StringType) {
if ($this->phpVersionProvider->isAtLeast(PhpVersionFeature::SCALAR_TYPES)) {
return new Identifier('string');
}
return null;
}
if ($phpStanType instanceof BooleanType) {
if ($this->phpVersionProvider->isAtLeast(PhpVersionFeature::SCALAR_TYPES)) {
return new Identifier('bool');
}
return null;
}
if ($phpStanType instanceof FloatType) {
if ($this->phpVersionProvider->isAtLeast(PhpVersionFeature::SCALAR_TYPES)) {
return new Identifier('float');
}
return null;
return $typeMapper->mapToPhpParserNode($phpStanType);
}
if ($phpStanType instanceof ArrayType) {
return new Identifier('array');
}
if ($phpStanType instanceof IterableType) {
return new Identifier('iterable');
}
if ($phpStanType instanceof ThisType) {
return new Identifier('self');
}
if ($phpStanType instanceof ParentStaticType) {
return new Identifier('parent');
}
if ($phpStanType instanceof StaticType) {
return null;
}
@ -204,14 +145,6 @@ final class PHPStanStaticTypeMapper
return new Identifier('callable');
}
if ($phpStanType instanceof ShortenedObjectType) {
return new Name\FullyQualified($phpStanType->getFullyQualifiedName());
}
if ($phpStanType instanceof AliasedObjectType) {
return new Name($phpStanType->getClassName());
}
if ($phpStanType instanceof TypeWithClassName) {
$lowerCasedClassName = strtolower($phpStanType->getClassName());
if ($lowerCasedClassName === 'callable') {
@ -230,7 +163,7 @@ final class PHPStanStaticTypeMapper
return null;
}
return new Name\FullyQualified($phpStanType->getClassName());
return new FullyQualified($phpStanType->getClassName());
}
if ($phpStanType instanceof UnionType) {
@ -263,11 +196,8 @@ final class PHPStanStaticTypeMapper
return new NullableType($nullabledTypeNode);
}
if ($phpStanType instanceof NeverType ||
$phpStanType instanceof VoidType ||
$phpStanType instanceof MixedType ||
$phpStanType instanceof ResourceType ||
$phpStanType instanceof NullType
if ($phpStanType instanceof VoidType ||
$phpStanType instanceof ResourceType
) {
return null;
}
@ -331,16 +261,8 @@ final class PHPStanStaticTypeMapper
return '\\' . Closure::class;
}
if ($phpStanType instanceof StringType) {
return 'string';
}
if ($phpStanType instanceof IntegerType) {
return 'int';
}
if ($phpStanType instanceof NullType) {
return 'null';
if ($phpStanType instanceof StringType || $phpStanType instanceof NullType || $phpStanType instanceof IntegerType || $phpStanType instanceof MixedType || $phpStanType instanceof FloatType || $phpStanType instanceof CallableType || $phpStanType instanceof ResourceType) {
return $phpStanType->describe(VerbosityLevel::typeOnly());
}
if ($phpStanType instanceof ArrayType) {
@ -369,16 +291,8 @@ final class PHPStanStaticTypeMapper
return implode('|', $docStringTypes);
}
if ($phpStanType instanceof MixedType) {
return 'mixed';
}
if ($phpStanType instanceof FloatType) {
return 'float';
}
if ($phpStanType instanceof VoidType) {
if ($this->phpVersionProvider->isAtLeast('7.1')) {
if ($this->phpVersionProvider->isAtLeast(PhpVersionFeature::SCALAR_TYPES)) {
// the void type is better done in PHP code
return '';
}
@ -387,12 +301,8 @@ final class PHPStanStaticTypeMapper
return 'void';
}
if ($phpStanType instanceof BooleanType) {
return 'bool';
}
if ($phpStanType instanceof IterableType) {
if ($this->phpVersionProvider->isAtLeast('7.1')) {
if ($this->phpVersionProvider->isAtLeast(PhpVersionFeature::SCALAR_TYPES)) {
// the void type is better done in PHP code
return '';
}
@ -400,18 +310,14 @@ final class PHPStanStaticTypeMapper
return 'iterable';
}
if ($phpStanType instanceof BooleanType) {
return 'bool';
}
if ($phpStanType instanceof NeverType) {
return 'mixed';
}
if ($phpStanType instanceof CallableType) {
return 'callable';
}
if ($phpStanType instanceof ResourceType) {
return 'resource';
}
throw new NotImplementedException(__METHOD__ . ' for ' . get_class($phpStanType));
}
@ -456,7 +362,7 @@ final class PHPStanStaticTypeMapper
}
/**
* @return Name|Name\FullyQualified|PhpParserUnionType|null
* @return Name|FullyQualified|PhpParserUnionType|null
*/
private function matchTypeForUnionedObjectTypes(UnionType $unionType): ?Node
{
@ -481,7 +387,7 @@ final class PHPStanStaticTypeMapper
}
}
return new Name\FullyQualified($unionedType->getClassName());
return new FullyQualified($unionedType->getClassName());
}
return null;

View File

@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeAnalyzer;
use PHPStan\Type\ArrayType;
use PHPStan\Type\IterableType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\UnionType;
use Rector\PHPStanStaticTypeMapper\ValueObject\UnionTypeAnalysis;
use Traversable;
final class UnionTypeAnalyzer
{
public function analyseForNullableAndIterable(UnionType $unionType): ?UnionTypeAnalysis
{
$isNullableType = false;
$hasIterable = false;
foreach ($unionType->getTypes() as $unionedType) {
if ($unionedType instanceof IterableType) {
$hasIterable = true;
continue;
}
if ($unionedType instanceof ArrayType) {
continue;
}
if ($unionedType instanceof NullType) {
$isNullableType = true;
continue;
}
if ($unionedType instanceof ObjectType) {
if ($unionedType->getClassName() === Traversable::class) {
$hasIterable = true;
continue;
}
}
return null;
}
return new UnionTypeAnalysis($isNullableType, $hasIterable);
}
}

View File

@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Type;
use Rector\Php\PhpVersionProvider;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\ValueObject\PhpVersionFeature;
final class BooleanTypeMapper implements TypeMapperInterface
{
/**
* @var PhpVersionProvider
*/
private $phpVersionProvider;
public function __construct(PhpVersionProvider $phpVersionProvider)
{
$this->phpVersionProvider = $phpVersionProvider;
}
public function getNodeClass(): string
{
return BooleanType::class;
}
/**
* @param BooleanType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
return new IdentifierTypeNode('bool');
}
/**
* @param BooleanType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
if (! $this->phpVersionProvider->isAtLeast(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return new Identifier('bool');
}
}

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Type;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
final class ClassStringTypeMapper implements TypeMapperInterface
{
public function getNodeClass(): string
{
return ClassStringType::class;
}
/**
* @param ClassStringType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
return new IdentifierTypeNode('class-string');
}
/**
* @param ClassStringType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
return null;
}
}

View File

@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\FloatType;
use PHPStan\Type\Type;
use Rector\Php\PhpVersionProvider;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\ValueObject\PhpVersionFeature;
final class FloatTypeMapper implements TypeMapperInterface
{
/**
* @var PhpVersionProvider
*/
private $phpVersionProvider;
public function __construct(PhpVersionProvider $phpVersionProvider)
{
$this->phpVersionProvider = $phpVersionProvider;
}
public function getNodeClass(): string
{
return FloatType::class;
}
/**
* @param FloatType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
return new IdentifierTypeNode('float');
}
/**
* @param FloatType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
if (! $this->phpVersionProvider->isAtLeast(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return new Identifier('float');
}
}

View File

@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\IntegerType;
use PHPStan\Type\Type;
use Rector\Php\PhpVersionProvider;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\ValueObject\PhpVersionFeature;
final class IntegerTypeMapper implements TypeMapperInterface
{
/**
* @var PhpVersionProvider
*/
private $phpVersionProvider;
public function __construct(PhpVersionProvider $phpVersionProvider)
{
$this->phpVersionProvider = $phpVersionProvider;
}
public function getNodeClass(): string
{
return IntegerType::class;
}
/**
* @param IntegerType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
return new IdentifierTypeNode('int');
}
/**
* @param IntegerType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
if (! $this->phpVersionProvider->isAtLeast(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return new Identifier('int');
}
}

View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\IterableType;
use PHPStan\Type\Type;
use Rector\Exception\NotImplementedException;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
final class IterableTypeMapper implements TypeMapperInterface
{
public function getNodeClass(): string
{
return IterableType::class;
}
/**
* @param IterableType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
throw new NotImplementedException();
}
/**
* @param IterableType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
return new Identifier('iterable');
}
}

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
final class MixedTypeMapper implements TypeMapperInterface
{
public function getNodeClass(): string
{
return MixedType::class;
}
/**
* @param MixedType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
return new IdentifierTypeNode('mixed');
}
/**
* @param MixedType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
return null;
}
}

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
final class NeverTypeMapper implements TypeMapperInterface
{
public function getNodeClass(): string
{
return NeverType::class;
}
/**
* @param NeverType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
return new IdentifierTypeNode('mixed');
}
/**
* @param NeverType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
return null;
}
}

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
final class NullTypeMapper implements TypeMapperInterface
{
public function getNodeClass(): string
{
return NullType::class;
}
/**
* @param NullType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
return new IdentifierTypeNode('null');
}
/**
* @param NullType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
return null;
}
}

View File

@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\PHPStan\Type\AliasedObjectType;
use Rector\PHPStan\Type\FullyQualifiedObjectType;
use Rector\PHPStan\Type\ShortenedObjectType;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
final class ObjectTypeMapper implements TypeMapperInterface
{
public function getNodeClass(): string
{
return ObjectType::class;
}
/**
* @param ObjectType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
return new IdentifierTypeNode('\\' . $type->getClassName());
}
/**
* @param ObjectType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
if ($type instanceof ShortenedObjectType) {
return new FullyQualified($type->getFullyQualifiedName());
}
if ($type instanceof AliasedObjectType) {
return new Name($type->getClassName());
}
if ($type instanceof FullyQualifiedObjectType) {
return new FullyQualified($type->getClassName());
}
if ($type instanceof GenericObjectType) {
if ($type->getClassName() === 'object') {
return new Identifier('object');
}
}
// fallback
return new FullyQualified($type->getClassName());
}
}

View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Type;
use Rector\PHPStan\Type\ParentStaticType;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
final class ParentStaticTypeMapper implements TypeMapperInterface
{
public function getNodeClass(): string
{
return ParentStaticType::class;
}
/**
* @param ParentStaticType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
return new IdentifierTypeNode('parent');
}
/**
* @param ParentStaticType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
return new Identifier('parent');
}
}

View File

@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use Rector\Php\PhpVersionProvider;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\ValueObject\PhpVersionFeature;
final class StringTypeMapper implements TypeMapperInterface
{
/**
* @var PhpVersionProvider
*/
private $phpVersionProvider;
public function __construct(PhpVersionProvider $phpVersionProvider)
{
$this->phpVersionProvider = $phpVersionProvider;
}
public function getNodeClass(): string
{
return StringType::class;
}
/**
* @param StringType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
return new IdentifierTypeNode('string');
}
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
if (! $this->phpVersionProvider->isAtLeast(PhpVersionFeature::SCALAR_TYPES)) {
return null;
}
return new Identifier('string');
}
}

View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\ThisTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
final class ThisTypeMapper implements TypeMapperInterface
{
public function getNodeClass(): string
{
return ThisType::class;
}
/**
* @param ThisType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
return new ThisTypeNode();
}
/**
* @param ThisType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
return new Identifier('self');
}
}

View File

@ -0,0 +1,219 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\NullableType;
use PhpParser\Node\UnionType as PhpParserUnionType;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use Rector\AttributeAwarePhpDoc\Ast\Type\AttributeAwareUnionTypeNode;
use Rector\Exception\ShouldNotHappenException;
use Rector\Php\PhpVersionProvider;
use Rector\PHPStanStaticTypeMapper\Contract\PHPStanStaticTypeMapperAwareInterface;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeAnalyzer;
use Rector\ValueObject\PhpVersionFeature;
final class UnionTypeMapper implements TypeMapperInterface, PHPStanStaticTypeMapperAwareInterface
{
/**
* @var PHPStanStaticTypeMapper
*/
private $phpStanStaticTypeMapper;
/**
* @var PhpVersionProvider
*/
private $phpVersionProvider;
/**
* @var UnionTypeAnalyzer
*/
private $unionTypeAnalyzer;
public function __construct(PhpVersionProvider $phpVersionProvider, UnionTypeAnalyzer $unionTypeAnalyzer)
{
$this->phpVersionProvider = $phpVersionProvider;
$this->unionTypeAnalyzer = $unionTypeAnalyzer;
}
public function getNodeClass(): string
{
return UnionType::class;
}
/**
* @param UnionType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
$unionTypesNodes = [];
foreach ($type->getTypes() as $unionedType) {
$unionTypesNodes[] = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($unionedType);
}
$unionTypesNodes = array_unique($unionTypesNodes);
return new AttributeAwareUnionTypeNode($unionTypesNodes);
}
/**
* @required
*/
public function setPHPStanStaticTypeMapper(PHPStanStaticTypeMapper $phpStanStaticTypeMapper): void
{
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
}
/**
* @param UnionType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
{
// match array types
$arrayNode = $this->matchArrayTypes($type);
if ($arrayNode !== null) {
return $arrayNode;
}
// special case for nullable
$nullabledType = $this->matchTypeForNullableUnionType($type);
if ($nullabledType === null) {
// use first unioned type in case of unioned object types
return $this->matchTypeForUnionedObjectTypes($type);
}
$nullabledTypeNode = $this->phpStanStaticTypeMapper->mapToPhpParserNode($nullabledType);
if ($nullabledTypeNode === null) {
return null;
}
if ($nullabledTypeNode instanceof NullableType) {
return $nullabledTypeNode;
}
if ($nullabledTypeNode instanceof PhpParserUnionType) {
throw new ShouldNotHappenException();
}
return new NullableType($nullabledTypeNode);
}
private function matchArrayTypes(UnionType $unionType): ?Identifier
{
$unionTypeAnalysis = $this->unionTypeAnalyzer->analyseForNullableAndIterable($unionType);
if ($unionTypeAnalysis === null) {
return null;
}
$type = $unionTypeAnalysis->hasIterable() ? 'iterable' : 'array';
if ($unionTypeAnalysis->isNullableType()) {
return new Identifier('?' . $type);
}
return new Identifier($type);
}
private function matchTypeForNullableUnionType(UnionType $unionType): ?Type
{
if (count($unionType->getTypes()) !== 2) {
return null;
}
$firstType = $unionType->getTypes()[0];
$secondType = $unionType->getTypes()[1];
if ($firstType instanceof NullType) {
return $secondType;
}
if ($secondType instanceof NullType) {
return $firstType;
}
return null;
}
/**
* @return Name|FullyQualified|PhpParserUnionType|null
*/
private function matchTypeForUnionedObjectTypes(UnionType $unionType): ?Node
{
$phpParserUnionType = $this->matchPhpParserUnionType($unionType);
if ($phpParserUnionType !== null) {
return $phpParserUnionType;
}
// the type should be compatible with all other types, e.g. A extends B, B
$compatibleObjectCandidate = $this->resolveCompatibleObjectCandidate($unionType);
if ($compatibleObjectCandidate === null) {
return null;
}
return new FullyQualified($compatibleObjectCandidate);
}
private function matchPhpParserUnionType(UnionType $unionType): ?PhpParserUnionType
{
if (! $this->phpVersionProvider->isAtLeast(PhpVersionFeature::UNION_TYPES)) {
return null;
}
$phpParserUnionedTypes = [];
foreach ($unionType->getTypes() as $unionedType) {
/** @var Identifier|Name|null $phpParserNode */
$phpParserNode = $this->phpStanStaticTypeMapper->mapToPhpParserNode($unionedType);
if ($phpParserNode === null) {
return null;
}
$phpParserUnionedTypes[] = $phpParserNode;
}
return new PhpParserUnionType($phpParserUnionedTypes);
}
private function areTypeWithClassNamesRelated(TypeWithClassName $firstType, TypeWithClassName $secondType): bool
{
if (is_a($firstType->getClassName(), $secondType->getClassName(), true)) {
return true;
}
return is_a($secondType->getClassName(), $firstType->getClassName(), true);
}
private function resolveCompatibleObjectCandidate(UnionType $unionType): ?string
{
foreach ($unionType->getTypes() as $unionedType) {
if (! $unionedType instanceof TypeWithClassName) {
return null;
}
foreach ($unionType->getTypes() as $nestedUnionedType) {
if (! $nestedUnionedType instanceof TypeWithClassName) {
return null;
}
if (! $this->areTypeWithClassNamesRelated($unionedType, $nestedUnionedType)) {
continue 2;
}
}
return $unionedType->getClassName();
}
return null;
}
}

View File

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace Rector\PHPStanStaticTypeMapper\ValueObject;
final class UnionTypeAnalysis
{
/**
* @var bool
*/
private $isNullableType = false;
/**
* @var bool
*/
private $hasIterable = false;
public function __construct(bool $isNullableType, bool $hasIterable)
{
$this->isNullableType = $isNullableType;
$this->hasIterable = $hasIterable;
}
public function isNullableType(): bool
{
return $this->isNullableType;
}
public function hasIterable(): bool
{
return $this->hasIterable;
}
}

View File

@ -270,6 +270,7 @@ final class DumpNodesCommand extends AbstractCommand
$node = new Function_('some_function');
} elseif ($nodeClass === ClassMethod::class) {
$node = new ClassMethod('someMethod');
$node->flags |= Class_::MODIFIER_PUBLIC;
} elseif ($nodeClass === Case_::class) {
$node = new Case_(new ConstFetch(new Name('true')));
} elseif ($nodeClass === Use_::class) {