Skip to content

Commit

Permalink
[TypeDeclaration] Handle Recursive multiple class string array on Com…
Browse files Browse the repository at this point in the history
…pleteVarDocTypePropertyRector (#520)

* [TypeDeclaration] Skip Recursive multiple class string array on CompleteVarDocTypePropertyRector

* debug

* fix

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Jul 26, 2021
1 parent 834ea49 commit e5d95bd
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;

class InvokableA {}
class InvokableB {}
class InvokableC {}
class FactoryA {}
class FactoryB {}
class FactoryC {}

final class RecursiveMultipleClassStringArray
{
public $services = [
'invokables' => [
InvokableA::class,
InvokableB::class,
InvokableC::class,
],
'factories' => [
FactoryA::class,
FactoryB::class,
FactoryC::class,
],
];
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\CompleteVarDocTypePropertyRector\Fixture;

class InvokableA {}
class InvokableB {}
class InvokableC {}
class FactoryA {}
class FactoryB {}
class FactoryC {}

final class RecursiveMultipleClassStringArray
{
/**
* @var array<string, mixed[]>
*/
public $services = [
'invokables' => [
InvokableA::class,
InvokableB::class,
InvokableC::class,
],
'factories' => [
FactoryA::class,
FactoryB::class,
FactoryC::class,
],
];
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ArrayType;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Generic\GenericClassStringType;
Expand Down Expand Up @@ -54,7 +55,7 @@ public function normalize(Type $type): Type
return $type;
}

private function resolveClassStringInUnionType(UnionType $type): Type
private function resolveClassStringInUnionType(UnionType $type): UnionType | ArrayType
{
$unionTypes = $type->getTypes();

Expand All @@ -66,6 +67,11 @@ private function resolveClassStringInUnionType(UnionType $type): Type
$keyType = $unionType->getKeyType();
$itemType = $unionType->getItemType();

if ($itemType instanceof ConstantArrayType) {
$arrayType = new ArrayType(new MixedType(), new MixedType());
return new ArrayType($keyType, $arrayType);
}

if (! $keyType instanceof MixedType && ! $keyType instanceof ConstantIntegerType) {
return $type;
}
Expand Down

0 comments on commit e5d95bd

Please sign in to comment.