Skip to content

Commit

Permalink
[PHP 8.0] Fix DowngradeNamedArgumentRector named arguments (#373)
Browse files Browse the repository at this point in the history
* Fix DowngradeNamedArgumentRector args

* make use of TypeKind enum

* fixup args

* make TypeKind required

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* require PR link

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Jul 4, 2021
1 parent 5a6c10c commit dfbbb02
Show file tree
Hide file tree
Showing 75 changed files with 593 additions and 358 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"rector/rector-phpunit": "^0.11.3",
"rector/rector-symfony": "^0.11.8",
"sebastian/diff": "^4.0.4",
"ssch/typo3-rector": "^0.11.20",
"ssch/typo3-rector": "dev-main#e75102b",
"symfony/console": "^5.3",
"symfony/dependency-injection": "^5.3",
"symfony/finder": "^5.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\IterableType;
use PHPStan\Type\MixedType;
use Rector\PHPStanStaticTypeMapper\ValueObject\TypeKind;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\Testing\PHPUnit\AbstractTestCase;

Expand Down Expand Up @@ -60,7 +61,10 @@ public function testMapPHPStanTypeToPHPStanPhpDocTypeNode(): void
{
$iterableType = new IterableType(new MixedType(), new ClassStringType());

$phpStanDocTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($iterableType);
$phpStanDocTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode(
$iterableType,
TypeKind::ANY()
);
$this->assertInstanceOf(ArrayTypeNode::class, $phpStanDocTypeNode);

/** @var ArrayTypeNode $phpStanDocTypeNode */
Expand All @@ -71,7 +75,10 @@ public function testMixed(): void
{
$mixedType = new MixedType();

$phpStanDocTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($mixedType);
$phpStanDocTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode(
$mixedType,
TypeKind::ANY()
);
$this->assertInstanceOf(IdentifierTypeNode::class, $phpStanDocTypeNode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Type\StringType;
use PHPStan\Type\UnionType;
use Rector\PHPStanStaticTypeMapper\TypeMapper\ArrayTypeMapper;
use Rector\PHPStanStaticTypeMapper\ValueObject\TypeKind;
use Rector\Testing\PHPUnit\AbstractTestCase;

final class ArrayTypeMapperTest extends AbstractTestCase
Expand All @@ -30,7 +31,7 @@ protected function setUp(): void
*/
public function testWithoutKeys(ArrayType $arrayType, string $expectedResult): void
{
$actualTypeNode = $this->arrayTypeMapper->mapToPHPStanPhpDocTypeNode($arrayType);
$actualTypeNode = $this->arrayTypeMapper->mapToPHPStanPhpDocTypeNode($arrayType, TypeKind::ANY());
$this->assertSame($expectedResult, (string) $actualTypeNode);
}

Expand All @@ -39,7 +40,7 @@ public function testWithoutKeys(ArrayType $arrayType, string $expectedResult): v
*/
public function testWithKeys(ArrayType $arrayType, string $expectedResult): void
{
$actualTypeNode = $this->arrayTypeMapper->mapToPHPStanPhpDocTypeNode($arrayType);
$actualTypeNode = $this->arrayTypeMapper->mapToPHPStanPhpDocTypeNode($arrayType, TypeKind::ANY());
$this->assertSame($expectedResult, (string) $actualTypeNode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ public function changeVarType(PhpDocInfo $phpDocInfo, Type $newType): void
}

// override existing type
$newPHPStanPhpDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($newType);
$newPHPStanPhpDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode(
$newType,
TypeKind::PROPERTY()
);

$currentVarTagValueNode = $phpDocInfo->getVarTagValueNode();
if ($currentVarTagValueNode !== null) {
Expand Down Expand Up @@ -72,7 +75,7 @@ public function changeReturnType(PhpDocInfo $phpDocInfo, Type $newType): void
// override existing type
$newPHPStanPhpDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode(
$newType,
TypeKind::KIND_RETURN
TypeKind::RETURN()
);
$currentReturnTagValueNode = $phpDocInfo->getReturnTagValue();

Expand All @@ -93,7 +96,7 @@ public function changeParamType(PhpDocInfo $phpDocInfo, Type $newType, Param $pa
return;
}

$phpDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($newType);
$phpDocType = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($newType, TypeKind::PARAM());
$paramTagValueNode = $phpDocInfo->getParamTagValueByName($paramName);

// override existing type
Expand Down
7 changes: 2 additions & 5 deletions packages/FamilyTree/Reflection/FamilyRelationsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,11 @@ public function getChildrenOfClassReflection(ClassReflection $desiredClassReflec
return $childrenClassReflections;
}

/**
* @param Name|NullableType|PhpParserUnionType|null $propertyTypeNode
*/
public function getPossibleUnionPropertyType(
Property $property,
Type $varType,
?Scope $scope,
?Node $propertyTypeNode
Name | NullableType | PhpParserUnionType | null $propertyTypeNode
): PropertyType {
if ($varType instanceof UnionType) {
return new PropertyType($varType, $propertyTypeNode);
Expand Down Expand Up @@ -114,7 +111,7 @@ public function getPossibleUnionPropertyType(
$varType = new UnionType([$varType, new NullType()]);
$propertyTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode(
$varType,
TypeKind::KIND_PROPERTY
TypeKind::PROPERTY()
);

return new PropertyType($varType, $propertyTypeNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Rector\Core\Configuration\CurrentNodeProvider;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\ValueObject\OldToNewType;
use Rector\PHPStanStaticTypeMapper\ValueObject\TypeKind;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType;
use Symplify\SimplePhpDocParser\PhpDocNodeVisitor\AbstractPhpDocNodeVisitor;
Expand Down Expand Up @@ -59,7 +60,10 @@ public function enterNode(Node $node): ?Node
continue;
}

$newTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($oldToNewType->getNewType());
$newTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode(
$oldToNewType->getNewType(),
TypeKind::ANY()
);

$parentType = $node->getAttribute(PhpDocAttributeKey::PARENT);
if ($parentType instanceof TypeNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,12 @@ public function getNodeClass(): string;

/**
* @param T $type
* @param TypeKind::*|null $kind
*/
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode;
public function mapToPHPStanPhpDocTypeNode(Type $type, TypeKind $typeKind): TypeNode;

This comment has been minimized.

Copy link
@clxmstaab

clxmstaab Jul 15, 2021

Contributor

changing this parameter to non-nullable is a BC break


/**
* @param T $type
* @param TypeKind::*|null $kind
*
* @return Name|NullableType|UnionType|null
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node;
public function mapToPhpParserNode(Type $type, TypeKind $typeTypeKind): ?Node;
}
14 changes: 4 additions & 10 deletions packages/PHPStanStaticTypeMapper/PHPStanStaticTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,27 @@ public function __construct(
) {
}

/**
* @param TypeKind::*|null $kind
*/
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
public function mapToPHPStanPhpDocTypeNode(Type $type, TypeKind $typeKind): TypeNode
{
foreach ($this->typeMappers as $typeMapper) {
if (! is_a($type, $typeMapper->getNodeClass(), true)) {
continue;
}

return $typeMapper->mapToPHPStanPhpDocTypeNode($type, $kind);
return $typeMapper->mapToPHPStanPhpDocTypeNode($type, $typeKind);
}

throw new NotImplementedYetException(__METHOD__ . ' for ' . $type::class);
}

/**
* @param TypeKind::*|null $kind
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): Name | NullableType | UnionType | null
public function mapToPhpParserNode(Type $type, TypeKind $typeKind): Name | NullableType | UnionType | null
{
foreach ($this->typeMappers as $typeMapper) {
if (! is_a($type, $typeMapper->getNodeClass(), true)) {
continue;
}

return $typeMapper->mapToPhpParserNode($type, $kind);
return $typeMapper->mapToPhpParserNode($type, $typeKind);
}

throw new NotImplementedYetException(__METHOD__ . ' for ' . $type::class);
Expand Down
59 changes: 39 additions & 20 deletions packages/PHPStanStaticTypeMapper/TypeMapper/ArrayTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeCommonTypeNarrower;
use Rector\PHPStanStaticTypeMapper\ValueObject\TypeKind;
use Symfony\Contracts\Service\Attribute\Required;

/**
Expand Down Expand Up @@ -69,45 +70,47 @@ public function getNodeClass(): string
/**
* @param ArrayType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
public function mapToPHPStanPhpDocTypeNode(Type $type, TypeKind $typeKind): TypeNode
{
$itemType = $type->getItemType();

if ($itemType instanceof UnionType && ! $type instanceof ConstantArrayType) {
return $this->createArrayTypeNodeFromUnionType($itemType);
return $this->createArrayTypeNodeFromUnionType($itemType, $typeKind);
}

if ($itemType instanceof ArrayType && $this->isGenericArrayCandidate($itemType)) {
return $this->createGenericArrayType($type, true);
return $this->createGenericArrayType($type, $typeKind, true);
}

if ($this->isGenericArrayCandidate($type)) {
return $this->createGenericArrayType($type, true);
return $this->createGenericArrayType($type, $typeKind, true);
}

$narrowedTypeNode = $this->narrowConstantArrayTypeOfUnionType($type, $itemType);
$narrowedTypeNode = $this->narrowConstantArrayTypeOfUnionType($type, $itemType, $typeKind);
if ($narrowedTypeNode instanceof TypeNode) {
return $narrowedTypeNode;
}

$itemTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($itemType);
$itemTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($itemType, $typeKind);

return new SpacingAwareArrayTypeNode($itemTypeNode);
}

/**
* @param ArrayType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
public function mapToPhpParserNode(Type $type, TypeKind $typeKind): ?Node
{
return new Name('array');
}

private function createArrayTypeNodeFromUnionType(UnionType $unionType): SpacingAwareArrayTypeNode
{
private function createArrayTypeNodeFromUnionType(
UnionType $unionType,
TypeKind $typeKind
): SpacingAwareArrayTypeNode {
$unionedArrayType = [];
foreach ($unionType->getTypes() as $unionedType) {
$typeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($unionedType);
$typeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($unionedType, $typeKind);
$unionedArrayType[(string) $typeNode] = $typeNode;
}

Expand Down Expand Up @@ -158,9 +161,15 @@ private function isGenericArrayCandidate(ArrayType $arrayType): bool
return false;
}

private function createGenericArrayType(ArrayType $arrayType, bool $withKey = false): GenericTypeNode
{
$itemTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($arrayType->getItemType());
private function createGenericArrayType(
ArrayType $arrayType,
TypeKind $typeKind,
bool $withKey = false
): GenericTypeNode {
$itemTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode(
$arrayType->getItemType(),
$typeKind
);
$identifierTypeNode = new IdentifierTypeNode('array');

// is class-string[] list only
Expand All @@ -169,7 +178,10 @@ private function createGenericArrayType(ArrayType $arrayType, bool $withKey = fa
}

if ($withKey) {
$keyTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($arrayType->getKeyType());
$keyTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode(
$arrayType->getKeyType(),
$typeKind
);
$genericTypes = [$keyTypeNode, $itemTypeNode];
} else {
$genericTypes = [$itemTypeNode];
Expand All @@ -195,33 +207,40 @@ private function isIntegerKeyAndNonNestedArray(ArrayType $arrayType): bool
return ! $arrayType->getItemType() instanceof ArrayType;
}

private function narrowConstantArrayTypeOfUnionType(ArrayType $arrayType, Type $itemType): ?TypeNode
{
private function narrowConstantArrayTypeOfUnionType(
ArrayType $arrayType,
Type $itemType,
TypeKind $typeKind
): ?TypeNode {
if ($arrayType instanceof ConstantArrayType && $itemType instanceof UnionType) {
$narrowedItemType = $this->unionTypeCommonTypeNarrower->narrowToSharedObjectType($itemType);
if ($narrowedItemType instanceof ObjectType) {
$itemTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($narrowedItemType);
$itemTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode(
$narrowedItemType,
$typeKind
);
return new SpacingAwareArrayTypeNode($itemTypeNode);
}

$narrowedItemType = $this->unionTypeCommonTypeNarrower->narrowToGenericClassStringType($itemType);
if ($narrowedItemType instanceof GenericClassStringType) {
return $this->createTypeNodeFromGenericClassStringType($narrowedItemType);
return $this->createTypeNodeFromGenericClassStringType($narrowedItemType, $typeKind);
}
}

return null;
}

private function createTypeNodeFromGenericClassStringType(
GenericClassStringType $genericClassStringType
GenericClassStringType $genericClassStringType,
TypeKind $typeKind
): TypeNode {
$genericType = $genericClassStringType->getGenericType();
if ($genericType instanceof ObjectType && ! $this->reflectionProvider->hasClass($genericType->getClassName())) {
return new IdentifierTypeNode($genericType->getClassName());
}

$itemTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($genericClassStringType);
$itemTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($genericClassStringType, $typeKind);

return new GenericTypeNode(new IdentifierTypeNode('array'), [$itemTypeNode]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\ValueObject\TypeKind;

/**
* @implements TypeMapperInterface<BooleanType>
Expand All @@ -36,7 +37,7 @@ public function getNodeClass(): string
/**
* @param BooleanType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
public function mapToPHPStanPhpDocTypeNode(Type $type, TypeKind $typeKind): TypeNode
{
if ($this->isFalseBooleanTypeWithUnion($type)) {
return new IdentifierTypeNode('false');
Expand All @@ -48,7 +49,7 @@ public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): Ty
/**
* @param BooleanType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
public function mapToPhpParserNode(Type $type, TypeKind $typeKind): ?Node
{
if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::SCALAR_TYPES)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ public function getNodeClass(): string
/**
* @param CallableType $type
*/
public function mapToPHPStanPhpDocTypeNode(Type $type, ?string $kind = null): TypeNode
public function mapToPHPStanPhpDocTypeNode(Type $type, TypeKind $typeKind): TypeNode
{
$returnTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($type->getReturnType());
$returnTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($type->getReturnType(), $typeKind);

return new SpacingAwareCallableTypeNode(new IdentifierTypeNode('callable'), [], $returnTypeNode);
}

/**
* @param CallableType|ClosureType $type
*/
public function mapToPhpParserNode(Type $type, ?string $kind = null): ?Node
public function mapToPhpParserNode(Type $type, TypeKind $typeKind): ?Node
{
if ($kind === TypeKind::KIND_PROPERTY) {
if ($typeKind->equals(TypeKind::PROPERTY())) {
return null;
}

Expand Down

0 comments on commit dfbbb02

Please sign in to comment.