Merge pull request #2508 from rectorphp/fix-param-type-union

Fix param type union
This commit is contained in:
Tomas Votruba 2019-12-27 16:30:12 +01:00 committed by GitHub
commit e380711dce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 93 additions and 6 deletions

View File

@ -722,18 +722,26 @@ final class StaticTypeMapper
return $phpParserUnionType;
}
// we need exactly one type
// do the type should be compatible with all other types, e.g. A extends B, B
foreach ($unionType->getTypes() as $unionedType) {
if (! $unionedType instanceof TypeWithClassName) {
return null;
}
foreach ($unionType->getTypes() as $nestedUnionedType) {
if (! $nestedUnionedType instanceof TypeWithClassName) {
return null;
}
if (! $this->areTypeWithClassNamesRelated($unionedType, $nestedUnionedType)) {
continue 2;
}
}
return new FullyQualified($unionedType->getClassName());
}
// @todo the type should be compatible with all other types, check with is_a()?
/** @var TypeWithClassName $firstObjectType */
$firstObjectType = $unionType->getTypes()[0];
return new FullyQualified($firstObjectType->getClassName());
return null;
}
private function mapScalarStringToType(string $scalarName): ?Type
@ -805,4 +813,13 @@ final class StaticTypeMapper
return new PhpParserUnionType($phpParserUnionedTypes);
}
private function areTypeWithClassNamesRelated(TypeWithClassName $firstType, TypeWithClassName $secondType): bool
{
if (is_a($firstType->getClassName(), $secondType->getClassName(), true)) {
return true;
}
return is_a($secondType->getClassName(), $firstType->getClassName(), true);
}
}

View File

@ -0,0 +1,47 @@
<?php
namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\ParamTypeDeclarationRector\Fixture;
class ChangeUnionTypeMutuallyChildren
{
/**
* @param CDataChild|CTypeParent $pointer
*/
public static function sizeof(&$pointer): int
{
}
}
class CDataChild extends CTypeParent
{
}
class CTypeParent
{
}
?>
-----
<?php
namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\ParamTypeDeclarationRector\Fixture;
class ChangeUnionTypeMutuallyChildren
{
/**
* @param CDataChild|CTypeParent $pointer
*/
public static function sizeof(\Rector\TypeDeclaration\Tests\Rector\ClassMethod\ParamTypeDeclarationRector\Fixture\CDataChild &$pointer): int
{
}
}
class CDataChild extends CTypeParent
{
}
class CTypeParent
{
}
?>

View File

@ -0,0 +1,23 @@
<?php
namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\ParamTypeDeclarationRector\Fixture;
class SkipUnionType
{
/**
* Returns size of C data type of the given FFI\CData or FFI\CType.
*
* @param CData|CType $pointer
*/
public static function sizeof(&$pointer): int
{
}
}
class CData
{
}
class CType
{
}