Skip to content

Commit

Permalink
[StaticTypeMapper] Remove #[Required] on NullableTypeNodeMapper (#4690)
Browse files Browse the repository at this point in the history
* [StaticTypeMapper] Remove #[Required] on NullableTypeNodeMapper

* Fix

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Aug 7, 2023
1 parent 09148f1 commit 35a594b
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions packages/StaticTypeMapper/PhpParser/NullableTypeNodeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,27 @@
namespace Rector\StaticTypeMapper\PhpParser;

use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\NullableType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\StaticTypeMapper\Contract\PhpParser\PhpParserNodeMapperInterface;
use Rector\StaticTypeMapper\Mapper\PhpParserNodeMapper;
use Symfony\Contracts\Service\Attribute\Required;

/**
* @implements PhpParserNodeMapperInterface<NullableType>
*/
final class NullableTypeNodeMapper implements PhpParserNodeMapperInterface
{
private PhpParserNodeMapper $phpParserNodeMapper;

public function __construct(
private readonly TypeFactory $typeFactory
private readonly TypeFactory $typeFactory,
private readonly FullyQualifiedNodeMapper $fullyQualifiedNodeMapper,
private readonly NameNodeMapper $nameNodeMapper,
private readonly IdentifierNodeMapper $identifierNodeMapper
) {
}

#[Required]
public function autowire(PhpParserNodeMapper $phpParserNodeMapper): void
{
$this->phpParserNodeMapper = $phpParserNodeMapper;
}

public function getNodeType(): string
{
return NullableType::class;
Expand All @@ -41,7 +36,15 @@ public function getNodeType(): string
*/
public function mapToPHPStan(Node $node): Type
{
$types = [$this->phpParserNodeMapper->mapToPHPStanType($node->type), new NullType()];
if ($node->type instanceof FullyQualified) {
$type = $this->fullyQualifiedNodeMapper->mapToPHPStan($node->type);
} elseif ($node->type instanceof Name) {
$type = $this->nameNodeMapper->mapToPHPStan($node->type);
} else {
$type = $this->identifierNodeMapper->mapToPHPStan($node->type);
}

$types = [$type, new NullType()];

return $this->typeFactory->createMixedPassedOrUnionType($types);
}
Expand Down

0 comments on commit 35a594b

Please sign in to comment.