[TypeDeclaration] Add nested key support (#3941)

* [TypeDeclaration] Add nested key support

* [rector] [TypeDeclaration] Add nested key support

* [cs] [TypeDeclaration] Add nested key support

Co-authored-by: rector-bot <tomas@getrector.org>
This commit is contained in:
Tomas Votruba 2020-08-11 13:58:03 +02:00 committed by GitHub
parent 1c4b510513
commit 7a603dde2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 157 additions and 14 deletions

View File

@ -7,11 +7,18 @@ namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
use PhpParser\Node;
use PhpParser\Node\Name;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use Rector\AttributeAwarePhpDoc\Ast\Type\AttributeAwareGenericTypeNode;
use Rector\AttributeAwarePhpDoc\Ast\Type\AttributeAwareIdentifierTypeNode;
use Rector\AttributeAwarePhpDoc\Ast\Type\AttributeAwareUnionTypeNode;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
@ -56,6 +63,10 @@ final class ArrayTypeMapper implements TypeMapperInterface
return $this->convertUnionArrayTypeNodesToArrayTypeOfUnionTypeNodes($itemTypeNode);
}
if ($this->isGenericArrayCandidate($type)) {
return $this->createGenericArrayType($type->getKeyType(), $itemTypeNode);
}
return new ArrayTypeNode($itemTypeNode);
}
@ -102,6 +113,47 @@ final class ArrayTypeMapper implements TypeMapperInterface
return new AttributeAwareUnionTypeNode($unionedArrayType);
}
private function isGenericArrayCandidate(ArrayType $arrayType): bool
{
if ($arrayType->getKeyType() instanceof MixedType) {
return false;
}
if ($arrayType->getKeyType() instanceof NeverType) {
return false;
}
// make sure the integer key type is not natural/implicit array int keys
$keysArrayType = $arrayType->getKeysArray();
if (! $keysArrayType instanceof ConstantArrayType) {
return true;
}
foreach ($keysArrayType->getValueTypes() as $key => $keyType) {
if (! $keyType instanceof ConstantIntegerType) {
return true;
}
if ($key !== $keyType->getValue()) {
return true;
}
}
return false;
}
private function createGenericArrayType(Type $keyType, TypeNode $itemTypeNode): AttributeAwareGenericTypeNode
{
/** @var IdentifierTypeNode $keyTypeNode */
$keyTypeNode = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($keyType);
$attributeAwareIdentifierTypeNode = new AttributeAwareIdentifierTypeNode('array');
// @see https://github.com/phpstan/phpdoc-parser/blob/98a088b17966bdf6ee25c8a4b634df313d8aa531/tests/PHPStan/Parser/PhpDocParserTest.php#L2692-L2696
$genericTypes = [$keyTypeNode, $itemTypeNode];
return new AttributeAwareGenericTypeNode($attributeAwareIdentifierTypeNode, $genericTypes);
}
private function mapArrayUnionTypeToDocString(ArrayType $arrayType, UnionType $unionType): string
{
$unionedTypesAsString = [];

View File

@ -49,7 +49,7 @@ final class ConsistentPregDelimiterRector extends AbstractRector implements Conf
/**
* All with pattern as 2st argument
* @var int[][]
* @var array<string, array<string, int>>
*/
private const STATIC_METHODS_WITH_REGEX_PATTERN = [
'Nette\Utils\Strings' => [

View File

@ -0,0 +1,39 @@
<?php
namespace Rector\CodingStyle\Tests\Rector\ClassConst\VarConstantCommentRector\Fixture;
final class ExplicitKeyArray
{
const VALUES_WITH_KEYS = [
100 => 'hi'
];
const VALUES_WITHOUT_KEYS = [
'hi'
];
}
?>
-----
<?php
namespace Rector\CodingStyle\Tests\Rector\ClassConst\VarConstantCommentRector\Fixture;
final class ExplicitKeyArray
{
/**
* @var array<int, string>
*/
const VALUES_WITH_KEYS = [
100 => 'hi'
];
/**
* @var string[]
*/
const VALUES_WITHOUT_KEYS = [
'hi'
];
}
?>

View File

@ -40,7 +40,7 @@ namespace Rector\CodingStyle\Tests\Rector\ClassConst\VarConstantCommentRector;
class UnionNestedArrayScalars
{
/**
* @var string[]
* @var array<string, string>
*/
public const STRING_ONLY = [
'key' => 'value',

View File

@ -17,7 +17,7 @@ use Rector\Core\RectorDefinition\RectorDefinition;
final class SubstrStrlenFunctionToNetteUtilsStringsRector extends AbstractRector
{
/**
* @var string[]
* @var array<string, string>
*/
private const FUNCTION_TO_STATIC_METHOD = [
'substr' => 'substring',

View File

@ -111,10 +111,12 @@ PHP
/** @var PhpDocInfo|null $phpDocInfo */
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
if ($phpDocInfo instanceof PhpDocInfo) {
$phpDocInfo->changeReturnType($inferedType);
if ($phpDocInfo === null) {
return null;
}
$phpDocInfo->changeReturnType($inferedType);
return $node;
}

View File

@ -47,9 +47,13 @@ final class ReturnTypeInferer extends AbstractPriorityAwareTypeInferer
continue;
}
$type = $returnTypeInferer->inferFunctionLike($functionLike);
$originalType = $returnTypeInferer->inferFunctionLike($functionLike);
if ($originalType instanceof MixedType) {
continue;
}
$type = $this->typeNormalizer->normalizeArrayTypeAndArrayNever($originalType);
$type = $this->typeNormalizer->normalizeArrayTypeAndArrayNever($type);
$type = $this->typeNormalizer->uniqueateConstantArrayType($type);
$type = $this->typeNormalizer->normalizeArrayOfUnionToUnionArray($type);

View File

@ -6,7 +6,6 @@ namespace Rector\TypeDeclaration;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
@ -66,7 +65,8 @@ final class TypeNormalizer
} else {
$this->collectedNestedArrayTypes[] = new NestedArrayTypeValueObject(
$type->getItemType(),
$arrayNesting
$arrayNesting,
$type->getKeyType()
);
}
@ -161,7 +161,7 @@ final class TypeNormalizer
foreach ($collectedNestedArrayTypes as $collectedNestedArrayType) {
$arrayType = $collectedNestedArrayType->getType();
for ($i = 0; $i < $collectedNestedArrayType->getArrayNestingLevel(); ++$i) {
$arrayType = new ArrayType(new MixedType(), $arrayType);
$arrayType = new ArrayType($collectedNestedArrayType->getKeyType(), $arrayType);
}
/** @var ArrayType $arrayType */

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Rector\TypeDeclaration\ValueObject;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
final class NestedArrayTypeValueObject
@ -18,10 +19,16 @@ final class NestedArrayTypeValueObject
*/
private $type;
public function __construct(Type $type, int $arrayNestingLevel)
/**
* @var Type|null
*/
private $keyType;
public function __construct(Type $valueType, int $arrayNestingLevel, ?Type $keyType = null)
{
$this->type = $type;
$this->type = $valueType;
$this->arrayNestingLevel = $arrayNestingLevel;
$this->keyType = $keyType;
}
public function getType(): Type
@ -33,4 +40,9 @@ final class NestedArrayTypeValueObject
{
return $this->arrayNestingLevel;
}
public function getKeyType(): Type
{
return $this->keyType ?: new MixedType();
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\AddArrayReturnDocTypeRector\Fixture;
final class NestedKeyType
{
public function getValues(): array
{
return [
'string' => 1000
];
}
}
?>
-----
<?php
namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\AddArrayReturnDocTypeRector\Fixture;
final class NestedKeyType
{
/**
* @return array<string, int>
*/
public function getValues(): array
{
return [
'string' => 1000
];
}
}
?>

View File

@ -41,7 +41,7 @@ final class ReturnUuid
private $amenityBuildings = [];
/**
* @return \Ramsey\Uuid\UuidInterface[]
* @return array<int, \Ramsey\Uuid\UuidInterface>
*/
public function getBuildingIds(): array
{

View File

@ -36,7 +36,7 @@ final class RegexPatternArgumentManipulator
];
/**
* @var int[][]
* @var array<string, array<string, int>>
*/
private const STATIC_METHODS_WITH_PATTERNS_TO_ARGUMENT_POSITION = [
Strings::class => [