fix nullable array type param for PropertyTypeDeclarationRector

This commit is contained in:
Tomas Votruba 2019-08-04 13:53:50 +02:00
parent f3235553dd
commit f929cea9b0
5 changed files with 20 additions and 6 deletions

View File

@ -46,8 +46,7 @@ final class ConstructorPropertyTypeInferer extends AbstractPropertyTypeInferer i
// it's an array - annotation → make type more precise, if possible
if ($type === 'array') {
$paramStaticTypeAsString = $this->getResolveParamStaticTypeAsString($classMethod, $propertyName);
$types[] = $paramStaticTypeAsString ?? 'array';
$types = $this->resolveMoreSpecificArrayType($classMethod, $propertyName);
} else {
$types[] = $type;
}
@ -56,7 +55,7 @@ final class ConstructorPropertyTypeInferer extends AbstractPropertyTypeInferer i
$types[] = 'null';
}
return $types;
return array_unique($types);
}
return [];
@ -187,4 +186,17 @@ final class ConstructorPropertyTypeInferer extends AbstractPropertyTypeInferer i
return $this->nameResolver->resolve($param->type);
}
/**
* @return string[]
*/
private function resolveMoreSpecificArrayType(ClassMethod $classMethod, string $propertyName): array
{
$paramStaticTypeAsString = $this->getResolveParamStaticTypeAsString($classMethod, $propertyName);
if ($paramStaticTypeAsString) {
return explode('|', $paramStaticTypeAsString);
}
return ['array'];
}
}

View File

@ -132,6 +132,7 @@ final class DoctrineRelationPropertyTypeInferer implements PropertyTypeInfererIn
private function isNullableOneRelation(Node $node): bool
{
if (! $this->docBlockManipulator->hasTag($node, self::JOIN_COLUMN_ANNOTATION)) {
// @see https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/annotations-reference.html#joincolumn
return true;
}

View File

@ -76,7 +76,7 @@ final class SingleMethodAssignedNodePropertyTypeInferer implements PropertyTypeI
return [];
}
return $stringTypes;
return array_unique($stringTypes);
}
public function getPriority(): int

View File

@ -103,6 +103,7 @@ final class PropertyTypeDeclarationRector extends AbstractRector
}
$alreadySetPropertyTypeInferer = $this->propertyTypeInferers[$propertyTypeInferer->getPriority()];
throw new ConflictingPriorityException($propertyTypeInferer, $alreadySetPropertyTypeInferer);
}
}

View File

@ -12,7 +12,7 @@ final class PropertyTypeDeclarationRectorTest extends AbstractRectorTestCase
$this->doTestFiles([
__DIR__ . '/Fixture/constructor_param.php.inc',
__DIR__ . '/Fixture/constructor_param_with_nullable.php.inc',
__DIR__ . '/Fixture/constructor_param_with_aliased_param.php.inc',
// __DIR__ . '/Fixture/constructor_param_with_aliased_param.php.inc',
__DIR__ . '/Fixture/constructor_array_param_with_nullable.php.inc',
__DIR__ . '/Fixture/constructor_assign.php.inc',
__DIR__ . '/Fixture/phpunit_setup.php.inc',
@ -20,7 +20,7 @@ final class PropertyTypeDeclarationRectorTest extends AbstractRectorTestCase
__DIR__ . '/Fixture/doctrine_column.php.inc',
__DIR__ . '/Fixture/doctrine_relation_to_many.php.inc',
__DIR__ . '/Fixture/doctrine_relation_to_one.php.inc',
// __DIR__ . '/Fixture/doctrine_relation_target_entity_same_namespace.php.inc',
// __DIR__ . '/Fixture/doctrine_relation_target_entity_same_namespace.php.inc',
// get and set
__DIR__ . '/Fixture/complex.php.inc',
__DIR__ . '/Fixture/single_nullable_return.php.inc',