2020-03-24 22:35:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\Php74\Rector\Property;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
2020-07-06 19:57:48 +00:00
|
|
|
use PhpParser\Node\Expr\Assign;
|
2020-03-24 22:35:10 +00:00
|
|
|
use PhpParser\Node\NullableType;
|
2020-07-06 19:57:48 +00:00
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
2020-03-24 22:35:10 +00:00
|
|
|
use PhpParser\Node\Stmt\Property;
|
2020-07-06 19:57:48 +00:00
|
|
|
use PhpParser\NodeTraverser;
|
2020-03-24 22:35:10 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2020-08-02 13:17:33 +00:00
|
|
|
use Rector\Core\ValueObject\MethodName;
|
2020-03-24 22:49:19 +00:00
|
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
2020-07-06 19:57:48 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-03-24 22:35:10 +00:00
|
|
|
|
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector\RestoreDefaultNullToNullableTypePropertyRectorTest
|
2020-03-24 22:35:10 +00:00
|
|
|
*/
|
|
|
|
final class RestoreDefaultNullToNullableTypePropertyRector extends AbstractRector
|
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2020-03-24 22:35:10 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition(
|
|
|
|
'Add null default to properties with PHP 7.4 property nullable type',
|
|
|
|
[
|
|
|
|
new CodeSample(
|
|
|
|
<<<'CODE_SAMPLE'
|
2020-03-24 22:35:10 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public ?string $name;
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2020-03-24 22:35:10 +00:00
|
|
|
,
|
2020-11-16 17:50:38 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2020-03-24 22:35:10 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public ?string $name = null;
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-06 18:51:25 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
);
|
2020-03-24 22:35:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-03-24 22:35:10 +00:00
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [Property::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Property $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
|
|
|
if ($this->shouldSkip($node)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$onlyProperty = $node->props[0];
|
2021-01-30 21:41:25 +00:00
|
|
|
$onlyProperty->default = $this->nodeFactory->createNull();
|
2020-03-24 22:35:10 +00:00
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function shouldSkip(Property $property): bool
|
|
|
|
{
|
2020-03-24 22:49:19 +00:00
|
|
|
if (! $this->isAtLeastPhpVersion(PhpVersionFeature::TYPED_PROPERTIES)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-24 22:35:10 +00:00
|
|
|
if (! $property->type instanceof NullableType) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-12-25 00:22:45 +00:00
|
|
|
if (count($property->props) > 1) {
|
2020-03-24 22:35:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$onlyProperty = $property->props[0];
|
2020-07-06 19:57:48 +00:00
|
|
|
if ($onlyProperty->default !== null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// is variable assigned in constructor
|
|
|
|
$propertyName = $this->getName($property);
|
|
|
|
|
2020-07-06 20:07:11 +00:00
|
|
|
return $this->isPropertyInitiatedInConstuctor($property, $propertyName);
|
2020-07-06 19:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function isPropertyInitiatedInConstuctor(Property $property, string $propertyName): bool
|
|
|
|
{
|
2020-07-19 18:52:42 +00:00
|
|
|
$classLike = $property->getAttribute(AttributeKey::CLASS_NODE);
|
|
|
|
if (! $classLike instanceof Class_) {
|
2020-07-06 19:57:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-02 13:17:33 +00:00
|
|
|
$constructClassMethod = $classLike->getMethod(MethodName::CONSTRUCT);
|
2020-07-06 19:57:48 +00:00
|
|
|
if (! $constructClassMethod instanceof ClassMethod) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$isPropertyInitiated = false;
|
|
|
|
$this->traverseNodesWithCallable((array) $constructClassMethod->stmts, function (Node $node) use (
|
|
|
|
$propertyName,
|
|
|
|
&$isPropertyInitiated
|
2020-09-01 17:56:30 +00:00
|
|
|
): ?int {
|
2020-07-06 19:57:48 +00:00
|
|
|
if (! $node instanceof Assign) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-02-09 01:52:35 +00:00
|
|
|
if (! $this->nodeNameResolver->isLocalPropertyFetchNamed($node->var, $propertyName)) {
|
2020-07-06 19:57:48 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$isPropertyInitiated = true;
|
|
|
|
|
|
|
|
return NodeTraverser::STOP_TRAVERSAL;
|
|
|
|
});
|
2020-03-24 22:35:10 +00:00
|
|
|
|
2020-07-06 19:57:48 +00:00
|
|
|
return $isPropertyInitiated;
|
2020-03-24 22:35:10 +00:00
|
|
|
}
|
|
|
|
}
|