|
| 1 | +<?php |
| 2 | + |
| 3 | +declare (strict_types=1); |
| 4 | +namespace Rector\Php84\Rector\Param; |
| 5 | + |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\Expr\ConstFetch; |
| 8 | +use PhpParser\Node\Param; |
| 9 | +use PHPStan\Type\NullType; |
| 10 | +use PHPStan\Type\TypeCombinator; |
| 11 | +use Rector\PhpParser\Node\Value\ValueResolver; |
| 12 | +use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; |
| 13 | +use Rector\Rector\AbstractRector; |
| 14 | +use Rector\StaticTypeMapper\StaticTypeMapper; |
| 15 | +use Rector\ValueObject\PhpVersionFeature; |
| 16 | +use Rector\VersionBonding\Contract\MinPhpVersionInterface; |
| 17 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 18 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 19 | +/** |
| 20 | + * @see \Rector\Tests\Php84\Rector\Param\ExplicitNullableParamTypeRector\ExplicitNullableParamTypeRectorTest |
| 21 | + */ |
| 22 | +final class ExplicitNullableParamTypeRector extends AbstractRector implements MinPhpVersionInterface |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @readonly |
| 26 | + * @var \Rector\PhpParser\Node\Value\ValueResolver |
| 27 | + */ |
| 28 | + private $valueResolver; |
| 29 | + /** |
| 30 | + * @readonly |
| 31 | + * @var \Rector\StaticTypeMapper\StaticTypeMapper |
| 32 | + */ |
| 33 | + private $staticTypeMapper; |
| 34 | + public function __construct(ValueResolver $valueResolver, StaticTypeMapper $staticTypeMapper) |
| 35 | + { |
| 36 | + $this->valueResolver = $valueResolver; |
| 37 | + $this->staticTypeMapper = $staticTypeMapper; |
| 38 | + } |
| 39 | + public function getRuleDefinition() : RuleDefinition |
| 40 | + { |
| 41 | + return new RuleDefinition('Make implicit nullable param to explicit', [new CodeSample(<<<'CODE_SAMPLE' |
| 42 | +function foo(string $param = null) {} |
| 43 | +CODE_SAMPLE |
| 44 | +, <<<'CODE_SAMPLE' |
| 45 | +function foo(?string $param = null) {} |
| 46 | +CODE_SAMPLE |
| 47 | +)]); |
| 48 | + } |
| 49 | + public function getNodeTypes() : array |
| 50 | + { |
| 51 | + return [Param::class]; |
| 52 | + } |
| 53 | + /** |
| 54 | + * @param Param $node |
| 55 | + */ |
| 56 | + public function refactor(Node $node) : ?Param |
| 57 | + { |
| 58 | + if (!$node->type instanceof Node) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + if (!$node->default instanceof ConstFetch || !$this->valueResolver->isNull($node->default)) { |
| 62 | + return null; |
| 63 | + } |
| 64 | + $nodeType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($node->type); |
| 65 | + if ($nodeType instanceof NullType) { |
| 66 | + return null; |
| 67 | + } |
| 68 | + $removedNullNodeType = TypeCombinator::removeNull($nodeType); |
| 69 | + if (!$nodeType->equals($removedNullNodeType)) { |
| 70 | + return null; |
| 71 | + } |
| 72 | + $newNodeType = TypeCombinator::addNull($nodeType); |
| 73 | + $node->type = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($newNodeType, TypeKind::PARAM); |
| 74 | + return $node; |
| 75 | + } |
| 76 | + public function provideMinPhpVersion() : int |
| 77 | + { |
| 78 | + return PhpVersionFeature::DEPRECATE_IMPLICIT_NULLABLE_PARAM_TYPE; |
| 79 | + } |
| 80 | +} |
0 commit comments