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;
|
|
|
|
use Rector\Core\RectorDefinition\CodeSample;
|
|
|
|
use Rector\Core\RectorDefinition\RectorDefinition;
|
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-03-24 22:35:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see \Rector\Php74\Tests\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector\RestoreDefaultNullToNullableTypePropertyRectorTest
|
|
|
|
*/
|
|
|
|
final class RestoreDefaultNullToNullableTypePropertyRector extends AbstractRector
|
|
|
|
{
|
|
|
|
public function getDefinition(): RectorDefinition
|
|
|
|
{
|
|
|
|
return new RectorDefinition('Add null default to properties with PHP 7.4 property nullable type', [
|
|
|
|
new CodeSample(
|
|
|
|
<<<'PHP'
|
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public ?string $name;
|
|
|
|
}
|
|
|
|
PHP
|
|
|
|
,
|
|
|
|
<<<'PHP'
|
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public ?string $name = null;
|
|
|
|
}
|
|
|
|
PHP
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
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];
|
|
|
|
$onlyProperty->default = $this->createNull();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($property->props) > 1) {
|
|
|
|
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
|
|
|
|
) {
|
|
|
|
if (! $node instanceof Assign) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $this->isLocalPropertyFetchNamed($node->var, $propertyName)) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|