Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TypeDeclaration] Skip ArrayShapeFromConstantArrayReturnRector on class name as key #1911

Merged
merged 3 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeItemNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayShapeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantStringType;
Expand All @@ -18,7 +19,8 @@
final class ArrayShapeTypeMapper
{
public function __construct(
private readonly PHPStanStaticTypeMapper $phpStanStaticTypeMapper
private readonly PHPStanStaticTypeMapper $phpStanStaticTypeMapper,
private readonly ReflectionProvider $reflectionProvider
) {
}

Expand All @@ -37,7 +39,13 @@ public function mapConstantArrayType(ConstantArrayType $constantArrayType): Arra
return null;
}

$keyDocTypeNode = new IdentifierTypeNode($keyType->getValue());
$keyValue = $keyType->getValue();

if ($this->reflectionProvider->hasClass($keyValue)) {
return null;
}

$keyDocTypeNode = new IdentifierTypeNode($keyValue);
$valueType = $constantArrayType->getValueTypes()[$index];

$valueDocTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode(
Expand Down
5 changes: 0 additions & 5 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
Expand Down Expand Up @@ -74,10 +73,6 @@
MyCLabsClassToEnumRector::class,
SpatieEnumClassToEnumRector::class,

ArrayShapeFromConstantArrayReturnRector::class => [
__DIR__ . '/rules/Transform/Rector/ClassMethod/ReturnTypeWillChangeRector.php',
],

// test paths
'*/tests/**/Fixture/*',
'*/rules-tests/**/Fixture/*',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ArrayShapeFromConstantArrayReturnRector\Fixture;

final class SkipClassNameAsKey
{
public function run(string $name)
{
return ['ArrayAccess' => $name];
}
}