PHP 8.2.31
Preview: RemoveNullPropertyInitializationRector.php Size: 1.91 KB
//opt/cpanel/ea-wappspector/vendor/rector/rector/rules/DeadCode/Rector/PropertyProperty/RemoveNullPropertyInitializationRector.php

<?php

declare (strict_types=1);
namespace Rector\DeadCode\Rector\PropertyProperty;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Stmt\Property;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use function strtolower;
/**
 * @see \Rector\Tests\DeadCode\Rector\PropertyProperty\RemoveNullPropertyInitializationRector\RemoveNullPropertyInitializationRectorTest
 */
final class RemoveNullPropertyInitializationRector extends AbstractRector
{
    public function getRuleDefinition() : RuleDefinition
    {
        return new RuleDefinition('Remove initialization with null value from property declarations', [new CodeSample(<<<'CODE_SAMPLE'
class SunshineCommand extends ParentClassWithNewConstructor
{
    private $myVar = null;
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SunshineCommand extends ParentClassWithNewConstructor
{
    private $myVar;
}
CODE_SAMPLE
)]);
    }
    /**
     * @return array<class-string<Node>>
     */
    public function getNodeTypes() : array
    {
        return [Property::class];
    }
    /**
     * @param Property $node
     */
    public function refactor(Node $node) : ?Node
    {
        if ($node->type instanceof Node) {
            return null;
        }
        $hasChanged = \false;
        foreach ($node->props as $prop) {
            $defaultValueNode = $prop->default;
            if (!$defaultValueNode instanceof Expr) {
                continue;
            }
            if (!$defaultValueNode instanceof ConstFetch) {
                continue;
            }
            if (strtolower((string) $defaultValueNode->name) !== 'null') {
                continue;
            }
            $prop->default = null;
            $hasChanged = \true;
        }
        if ($hasChanged) {
            return $node;
        }
        return null;
    }
}

Directory Contents

Dirs: 0 × Files: 1

Name Size Perms Modified Actions
1.91 KB lrw-r--r-- 2024-11-08 13:59:10
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).