mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-28 15:00:50 +00:00
503a6059f8
a8922f7431
skip temporarily match + throws downagrade in symfony/console, very unlikely to run
46 lines
1.7 KiB
PHP
46 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\NodeTypeResolver\TypeComparator;
|
|
|
|
use PHPStan\Type\ArrayType;
|
|
use PHPStan\Type\Constant\ConstantArrayType;
|
|
use PHPStan\Type\MixedType;
|
|
use PHPStan\Type\UnionType;
|
|
use Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeCommonTypeNarrower;
|
|
/**
|
|
* @see \Rector\Tests\NodeTypeResolver\TypeComparator\ArrayTypeComparatorTest
|
|
*/
|
|
final class ArrayTypeComparator
|
|
{
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\PHPStanStaticTypeMapper\TypeAnalyzer\UnionTypeCommonTypeNarrower
|
|
*/
|
|
private $unionTypeCommonTypeNarrower;
|
|
public function __construct(UnionTypeCommonTypeNarrower $unionTypeCommonTypeNarrower)
|
|
{
|
|
$this->unionTypeCommonTypeNarrower = $unionTypeCommonTypeNarrower;
|
|
}
|
|
public function isSubtype(ArrayType $checkedType, ArrayType $mainType) : bool
|
|
{
|
|
if (!$checkedType instanceof ConstantArrayType && !$mainType instanceof ConstantArrayType) {
|
|
return $mainType->isSuperTypeOf($checkedType)->yes();
|
|
}
|
|
$checkedKeyType = $checkedType->getKeyType();
|
|
$mainKeyType = $mainType->getKeyType();
|
|
if (!$mainKeyType instanceof MixedType && $mainKeyType->isSuperTypeOf($checkedKeyType)->yes()) {
|
|
return \true;
|
|
}
|
|
$checkedItemType = $checkedType->getItemType();
|
|
if ($checkedItemType instanceof UnionType) {
|
|
$checkedItemType = $this->unionTypeCommonTypeNarrower->narrowToGenericClassStringType($checkedItemType);
|
|
}
|
|
$mainItemType = $mainType->getItemType();
|
|
if ($mainItemType instanceof UnionType) {
|
|
$mainItemType = $this->unionTypeCommonTypeNarrower->narrowToGenericClassStringType($mainItemType);
|
|
}
|
|
return $checkedItemType->isSuperTypeOf($mainItemType)->yes();
|
|
}
|
|
}
|