use @required

This commit is contained in:
TomasVotruba 2020-01-15 03:11:11 +01:00
parent 54c92ebfec
commit a4e90efb7c
6 changed files with 49 additions and 26 deletions

View File

@ -203,7 +203,7 @@ class SomeClass
### Filter Rectors
If you have a configuration file for Rector including many sets and Rectors, you might want at times to run only a single Rector from them. The `--only` argument allows that, for example :
If you have a configuration file for Rector including many sets and Rectors, you might want at times to run only a single Rector from them. The `--only` argument allows that, for example :
```bash
vendor/bin/rector process --set solid --only "Rector\SOLID\Rector\Class_\FinalizeClassesWithoutChildrenRector" src/

View File

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

View File

@ -22,4 +22,6 @@ interface TypeMapperInterface
* @return Identifier|Name|NullableType|UnionType|null
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node;
public function mapToDocString(Type $phpStanType, ?Type $parentType = null): string;
}

View File

@ -43,7 +43,6 @@ use Rector\PHPStan\Type\AliasedObjectType;
use Rector\PHPStan\Type\FullyQualifiedObjectType;
use Rector\PHPStan\Type\SelfObjectType;
use Rector\PHPStan\Type\ShortenedObjectType;
use Rector\PHPStanStaticTypeMapper\Contract\PHPStanStaticTypeMapperAwareInterface;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\ValueObject\PhpVersionFeature;
@ -70,9 +69,8 @@ final class PHPStanStaticTypeMapper
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
if ($type instanceof ArrayType || $type instanceof IterableType) {
if ($type instanceof ArrayType) {
$itemTypeNode = $this->mapToPHPStanPhpDocTypeNode($type->getItemType());
if ($itemTypeNode instanceof UnionTypeNode) {
return $this->convertUnionArrayTypeNodesToArrayTypeOfUnionTypeNodes($itemTypeNode);
}
@ -85,11 +83,6 @@ final class PHPStanStaticTypeMapper
continue;
}
// prevents circular dependency
if ($typeMapper instanceof PHPStanStaticTypeMapperAwareInterface) {
$typeMapper->setPHPStanStaticTypeMapper($this);
}
return $typeMapper->mapToPHPStanPhpDocTypeNode($type);
}

View File

@ -6,14 +6,30 @@ namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use PHPStan\Type\IterableType;
use PHPStan\Type\Type;
use Rector\Exception\NotImplementedException;
use Rector\AttributeAwarePhpDoc\Ast\Type\AttributeAwareUnionTypeNode;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
final class IterableTypeMapper implements TypeMapperInterface
{
/**
* @var PHPStanStaticTypeMapper
*/
private $phpStanStaticTypeMapper;
/**
* @required
*/
public function autowire(PHPStanStaticTypeMapper $phpStanStaticTypeMapper): void
{
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
}
public function getNodeClass(): string
{
return IterableType::class;
@ -24,7 +40,12 @@ final class IterableTypeMapper implements TypeMapperInterface
*/
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
{
throw new NotImplementedException();
$itemTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($type->getItemType());
if ($itemTypeNode instanceof UnionTypeNode) {
return $this->convertUnionArrayTypeNodesToArrayTypeOfUnionTypeNodes($itemTypeNode);
}
return new ArrayTypeNode($itemTypeNode);
}
/**
@ -34,4 +55,24 @@ final class IterableTypeMapper implements TypeMapperInterface
{
return new Identifier('iterable');
}
private function convertUnionArrayTypeNodesToArrayTypeOfUnionTypeNodes(
UnionTypeNode $unionTypeNode
): AttributeAwareUnionTypeNode {
$unionedArrayType = [];
foreach ($unionTypeNode->types as $unionedType) {
if ($unionedType instanceof UnionTypeNode) {
foreach ($unionedType->types as $key => $subUnionedType) {
$unionedType->types[$key] = new ArrayTypeNode($subUnionedType);
}
$unionedArrayType[] = $unionedType;
continue;
}
$unionedArrayType[] = new ArrayTypeNode($unionedType);
}
return new AttributeAwareUnionTypeNode($unionedArrayType);
}
}

View File

@ -18,13 +18,12 @@ 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
final class UnionTypeMapper implements TypeMapperInterface
{
/**
* @var PHPStanStaticTypeMapper
@ -71,7 +70,7 @@ final class UnionTypeMapper implements TypeMapperInterface, PHPStanStaticTypeMap
/**
* @required
*/
public function setPHPStanStaticTypeMapper(PHPStanStaticTypeMapper $phpStanStaticTypeMapper): void
public function autowire(PHPStanStaticTypeMapper $phpStanStaticTypeMapper): void
{
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
}