2020-01-15 02:16:22 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\PHPStanStaticTypeMapper\TypeMapper;
|
2020-01-15 02:16:22 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
|
|
|
|
use PHPStan\Type\Generic\GenericClassStringType;
|
|
|
|
use PHPStan\Type\IntersectionType;
|
|
|
|
use PHPStan\Type\Type;
|
|
|
|
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareIntersectionTypeNode;
|
|
|
|
use Rector\Core\Exception\ShouldNotHappenException;
|
|
|
|
use Rector\Core\Php\PhpVersionProvider;
|
|
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
|
|
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
|
|
|
|
use Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper;
|
2022-06-07 06:40:10 +00:00
|
|
|
use RectorPrefix20220607\Symfony\Contracts\Service\Attribute\Required;
|
2021-06-30 11:36:37 +00:00
|
|
|
/**
|
|
|
|
* @implements TypeMapperInterface<IntersectionType>
|
|
|
|
*/
|
2022-06-06 17:12:56 +00:00
|
|
|
final class IntersectionTypeMapper implements \Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface
|
2020-01-15 02:16:22 +00:00
|
|
|
{
|
2022-02-19 18:54:29 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private const STRING = 'string';
|
2020-01-15 02:16:22 +00:00
|
|
|
/**
|
2021-05-12 13:15:45 +00:00
|
|
|
* @var \Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper
|
2020-01-15 02:16:22 +00:00
|
|
|
*/
|
|
|
|
private $phpStanStaticTypeMapper;
|
2021-11-14 12:07:09 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-11-14 12:07:09 +00:00
|
|
|
* @var \Rector\Core\Php\PhpVersionProvider
|
|
|
|
*/
|
|
|
|
private $phpVersionProvider;
|
2022-06-06 17:12:56 +00:00
|
|
|
public function __construct(\Rector\Core\Php\PhpVersionProvider $phpVersionProvider)
|
2021-11-14 12:07:09 +00:00
|
|
|
{
|
|
|
|
$this->phpVersionProvider = $phpVersionProvider;
|
|
|
|
}
|
2020-01-15 02:16:22 +00:00
|
|
|
/**
|
|
|
|
* @required
|
|
|
|
*/
|
2022-06-06 17:12:56 +00:00
|
|
|
public function autowire(\Rector\PHPStanStaticTypeMapper\PHPStanStaticTypeMapper $phpStanStaticTypeMapper) : void
|
2020-01-15 02:16:22 +00:00
|
|
|
{
|
|
|
|
$this->phpStanStaticTypeMapper = $phpStanStaticTypeMapper;
|
|
|
|
}
|
2021-03-06 21:16:18 +00:00
|
|
|
/**
|
|
|
|
* @return class-string<Type>
|
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeClass() : string
|
2020-01-15 02:16:22 +00:00
|
|
|
{
|
2022-06-06 17:12:56 +00:00
|
|
|
return \PHPStan\Type\IntersectionType::class;
|
2020-01-15 02:16:22 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param IntersectionType $type
|
2020-01-15 02:16:22 +00:00
|
|
|
*/
|
2022-06-06 17:12:56 +00:00
|
|
|
public function mapToPHPStanPhpDocTypeNode(\PHPStan\Type\Type $type, string $typeKind) : \PHPStan\PhpDocParser\Ast\Type\TypeNode
|
2020-01-15 02:16:22 +00:00
|
|
|
{
|
|
|
|
$intersectionTypesNodes = [];
|
2020-03-24 21:26:12 +00:00
|
|
|
foreach ($type->getTypes() as $intersectionedType) {
|
2021-07-04 13:13:54 +00:00
|
|
|
$intersectionTypesNodes[] = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($intersectionedType, $typeKind);
|
2020-01-15 02:16:22 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
$intersectionTypesNodes = \array_unique($intersectionTypesNodes);
|
2021-11-14 12:07:09 +00:00
|
|
|
if (\count($intersectionTypesNodes) === 1) {
|
|
|
|
return $intersectionTypesNodes[0];
|
|
|
|
}
|
2022-06-06 17:12:56 +00:00
|
|
|
return new \Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareIntersectionTypeNode($intersectionTypesNodes);
|
2020-01-15 02:16:22 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param IntersectionType $type
|
2020-01-15 02:16:22 +00:00
|
|
|
*/
|
2022-06-06 17:12:56 +00:00
|
|
|
public function mapToPhpParserNode(\PHPStan\Type\Type $type, string $typeKind) : ?\PhpParser\Node
|
2020-01-15 02:16:22 +00:00
|
|
|
{
|
2022-06-06 17:12:56 +00:00
|
|
|
if (!$this->phpVersionProvider->isAtLeastPhpVersion(\Rector\Core\ValueObject\PhpVersionFeature::INTERSECTION_TYPES)) {
|
2021-11-14 12:07:09 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$intersectionedTypeNodes = [];
|
|
|
|
foreach ($type->getTypes() as $intersectionedType) {
|
|
|
|
$resolvedType = $this->phpStanStaticTypeMapper->mapToPhpParserNode($intersectionedType, $typeKind);
|
2022-06-06 17:12:56 +00:00
|
|
|
if ($intersectionedType instanceof \PHPStan\Type\Generic\GenericClassStringType) {
|
2022-02-19 18:54:29 +00:00
|
|
|
$resolvedTypeName = self::STRING;
|
2022-06-06 17:12:56 +00:00
|
|
|
$resolvedType = new \PhpParser\Node\Name(self::STRING);
|
|
|
|
} elseif (!$resolvedType instanceof \PhpParser\Node\Name) {
|
|
|
|
throw new \Rector\Core\Exception\ShouldNotHappenException();
|
2022-02-19 18:54:29 +00:00
|
|
|
} else {
|
|
|
|
$resolvedTypeName = (string) $resolvedType;
|
2021-11-14 12:07:09 +00:00
|
|
|
}
|
2022-02-19 18:54:29 +00:00
|
|
|
if (\in_array($resolvedTypeName, [self::STRING, 'object'], \true)) {
|
2021-12-29 23:57:29 +00:00
|
|
|
return $resolvedType;
|
|
|
|
}
|
2021-11-14 12:07:09 +00:00
|
|
|
$intersectionedTypeNodes[] = $resolvedType;
|
|
|
|
}
|
2022-06-06 17:12:56 +00:00
|
|
|
return new \PhpParser\Node\IntersectionType($intersectionedTypeNodes);
|
2020-01-15 02:16:22 +00:00
|
|
|
}
|
|
|
|
}
|