mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 00:10:51 +00:00
28c719d1fb
8961d20f56
[DX] Localize multi instance checker and privates accessor from Symplify + bump to PHPStan 1.8.3 (#2883)
39 lines
1.3 KiB
PHP
39 lines
1.3 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\NodeTypeResolver\NodeTypeCorrector;
|
|
|
|
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
|
|
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
|
|
use PHPStan\Type\IntersectionType;
|
|
use PHPStan\Type\Type;
|
|
final class AccessoryNonEmptyStringTypeCorrector
|
|
{
|
|
/**
|
|
* @return \PHPStan\Type\Type|\PHPStan\Type\IntersectionType
|
|
*/
|
|
public function correct(Type $mainType)
|
|
{
|
|
if (!$mainType instanceof IntersectionType) {
|
|
return $mainType;
|
|
}
|
|
if (!$mainType->isSubTypeOf(new AccessoryNonEmptyStringType())->yes() && !$mainType->isSubTypeOf(new AccessoryNonFalsyStringType())->yes()) {
|
|
return $mainType;
|
|
}
|
|
$clearIntersectionedTypes = [];
|
|
foreach ($mainType->getTypes() as $intersectionedType) {
|
|
if ($intersectionedType instanceof AccessoryNonEmptyStringType) {
|
|
continue;
|
|
}
|
|
if ($intersectionedType instanceof AccessoryNonFalsyStringType) {
|
|
continue;
|
|
}
|
|
$clearIntersectionedTypes[] = $intersectionedType;
|
|
}
|
|
if (\count($clearIntersectionedTypes) === 1) {
|
|
return $clearIntersectionedTypes[0];
|
|
}
|
|
return new IntersectionType($clearIntersectionedTypes);
|
|
}
|
|
}
|