2020-03-24 22:35:10 +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\Php74\Rector\Property;
|
2020-03-24 22:35:10 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2023-03-23 23:21:34 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\Property;
|
2023-11-11 17:10:37 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +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
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RestoreDefaultNullToNullableTypePropertyRector extends AbstractRector implements MinPhpVersionInterface
|
2020-03-24 22:35:10 +00:00
|
|
|
{
|
2022-04-11 11:30:16 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2022-04-11 11:30:16 +00:00
|
|
|
* @var \Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector
|
|
|
|
*/
|
|
|
|
private $constructorAssignDetector;
|
2023-11-09 22:08:11 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
|
|
|
|
*/
|
|
|
|
private $phpDocInfoFactory;
|
|
|
|
public function __construct(ConstructorAssignDetector $constructorAssignDetector, PhpDocInfoFactory $phpDocInfoFactory)
|
2022-04-11 11:30:16 +00:00
|
|
|
{
|
|
|
|
$this->constructorAssignDetector = $constructorAssignDetector;
|
2023-11-09 22:08:11 +00:00
|
|
|
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
2022-04-11 11:30:16 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-03-24 22:35:10 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +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
|
2021-05-09 20:15:43 +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-09 20:15:43 +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
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-03-24 22:35:10 +00:00
|
|
|
{
|
2023-06-09 17:42:49 +00:00
|
|
|
return [Class_::class];
|
2020-03-24 22:35:10 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-06-09 17:42:49 +00:00
|
|
|
* @param Class_ $node
|
2020-03-24 22:35:10 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2020-03-24 22:35:10 +00:00
|
|
|
{
|
2024-04-03 11:21:35 +00:00
|
|
|
if ($this->isReadonlyClass($node)) {
|
2023-07-11 22:02:27 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-06-09 17:42:49 +00:00
|
|
|
$hasChanged = \false;
|
|
|
|
foreach ($node->getProperties() as $property) {
|
|
|
|
if ($this->shouldSkip($property, $node)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$onlyProperty = $property->props[0];
|
|
|
|
$onlyProperty->default = $this->nodeFactory->createNull();
|
|
|
|
$hasChanged = \true;
|
2020-03-24 22:35:10 +00:00
|
|
|
}
|
2023-06-09 17:42:49 +00:00
|
|
|
if ($hasChanged) {
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
return null;
|
2020-03-24 22:35:10 +00:00
|
|
|
}
|
2021-07-21 09:35:57 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::TYPED_PROPERTIES;
|
2021-07-21 09:35:57 +00:00
|
|
|
}
|
2023-06-09 17:42:49 +00:00
|
|
|
private function shouldSkip(Property $property, Class_ $class) : bool
|
2020-03-24 22:35:10 +00:00
|
|
|
{
|
2022-04-13 16:41:54 +00:00
|
|
|
if ($property->type === null) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-03-24 22:35:10 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($property->props) > 1) {
|
|
|
|
return \true;
|
2020-03-24 22:35:10 +00:00
|
|
|
}
|
|
|
|
$onlyProperty = $property->props[0];
|
2023-03-23 23:21:34 +00:00
|
|
|
if ($onlyProperty->default instanceof Expr) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-07-06 19:57:48 +00:00
|
|
|
}
|
2024-04-03 11:21:35 +00:00
|
|
|
if ($this->isReadonlyProperty($property)) {
|
2022-01-22 09:45:55 +00:00
|
|
|
return \true;
|
|
|
|
}
|
2022-04-18 22:25:27 +00:00
|
|
|
if (!$this->nodeTypeResolver->isNullableType($property)) {
|
|
|
|
return \true;
|
|
|
|
}
|
2020-07-06 19:57:48 +00:00
|
|
|
// is variable assigned in constructor
|
|
|
|
$propertyName = $this->getName($property);
|
2023-06-09 17:42:49 +00:00
|
|
|
return $this->constructorAssignDetector->isPropertyAssigned($class, $propertyName);
|
2020-03-24 22:35:10 +00:00
|
|
|
}
|
2024-04-03 11:21:35 +00:00
|
|
|
private function isReadonlyProperty(Property $property) : bool
|
2023-11-08 12:07:04 +00:00
|
|
|
{
|
|
|
|
// native readonly
|
|
|
|
if ($property->isReadonly()) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
// @readonly annotation
|
|
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
|
2024-04-03 12:05:35 +00:00
|
|
|
return $phpDocInfo->hasByName('@readonly');
|
2023-11-08 12:07:04 +00:00
|
|
|
}
|
2024-04-03 11:21:35 +00:00
|
|
|
private function isReadonlyClass(Class_ $class) : bool
|
|
|
|
{
|
|
|
|
// native readonly
|
|
|
|
if ($class->isReadonly()) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
// @immutable annotation
|
|
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($class);
|
2024-04-03 12:05:35 +00:00
|
|
|
return $phpDocInfo->hasByName('@immutable');
|
2024-04-03 11:21:35 +00:00
|
|
|
}
|
2020-03-24 22:35:10 +00:00
|
|
|
}
|