[TypeDeclaratoin] Add AccessoryLiteralStringType to static type mapper (#1558)

This commit is contained in:
Tomas Votruba 2021-12-24 02:13:43 +01:00 committed by GitHub
parent 6c8a69e956
commit 89c3483001
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 0 deletions

View File

@ -8,6 +8,7 @@ use PhpParser\Node\ComplexType;
use PhpParser\Node\Name;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\Accessory\AccessoryLiteralStringType;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Accessory\HasMethodType;
use PHPStan\Type\Type;
@ -43,6 +44,10 @@ final class PHPStanStaticTypeMapper
return new IdentifierTypeNode('object');
}
if ($type instanceof AccessoryLiteralStringType) {
return new IdentifierTypeNode('string');
}
throw new NotImplementedYetException(__METHOD__ . ' for ' . $type::class);
}

View File

@ -0,0 +1,38 @@
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector\Fixture;
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector\Source\ExternalList;
final class ForeachLiteral
{
public function run(): array
{
$map = [];
foreach (ExternalList::VALUES as $externalValue) {
$map[$externalValue] = 100;
}
return $map;
}
}
-----
<?php
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector\Fixture;
use Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector\Source\ExternalList;
final class ForeachLiteral
{
/**
* @return array<string, int>
*/
public function run(): array
{
$map = [];
foreach (ExternalList::VALUES as $externalValue) {
$map[$externalValue] = 100;
}
return $map;
}
}

View File

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddArrayReturnDocTypeRector\Source;
final class ExternalList
{
public const FIRST = 'first';
public const SECOND = 'second';
public const VALUES = [self::FIRST, self::SECOND];
}