mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-20 08:05:29 +01:00
Merge pull request #2508 from rectorphp/fix-param-type-union
Fix param type union
This commit is contained in:
commit
e380711dce
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
{
|
||||
}
|
||||
|
||||
?>
|
@ -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
|
||||
{
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user