Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveDefaultValueFromAssignedPropertyRector\Fixture;

use RuntimeException;

final class AssignAfterThrowingBranch
{
private ?string $name = null;

public function __construct(string $name, bool $shouldFail)
{
if ($shouldFail) {
throw new RuntimeException('nope');
}

$this->name = $name;
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveDefaultValueFromAssignedPropertyRector\Fixture;

use RuntimeException;

final class AssignAfterThrowingBranch
{
private ?string $name;

public function __construct(string $name, bool $shouldFail)
{
if ($shouldFail) {
throw new RuntimeException('nope');
}

$this->name = $name;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveDefaultValueFromAssignedPropertyRector\Fixture;

final class AssignInIfElseIfElse
{
private ?string $name = null;

public function __construct(int $type)
{
if ($type === 1) {
$this->name = 'one';
} elseif ($type === 2) {
$this->name = 'two';
} else {
$this->name = 'many';
}
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveDefaultValueFromAssignedPropertyRector\Fixture;

final class AssignInIfElseIfElse
{
private ?string $name;

public function __construct(int $type)
{
if ($type === 1) {
$this->name = 'one';
} elseif ($type === 2) {
$this->name = 'two';
} else {
$this->name = 'many';
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveDefaultValueFromAssignedPropertyRector\Fixture;

final class AssignInSwitchWithDefault
{
private ?string $name = null;

public function __construct(int $type)
{
switch ($type) {
case 1:
$this->name = 'one';
break;
default:
$this->name = 'many';
}
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveDefaultValueFromAssignedPropertyRector\Fixture;

final class AssignInSwitchWithDefault
{
private ?string $name;

public function __construct(int $type)
{
switch ($type) {
case 1:
$this->name = 'one';
break;
default:
$this->name = 'many';
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveDefaultValueFromAssignedPropertyRector\Fixture;

final class ReturnAfterAssign
{
private ?string $name = null;

public function __construct(string $name, bool $shouldReturn = false)
{
$this->name = $name;

if ($shouldReturn) {
return;
}

$this->name = strtoupper($name);
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveDefaultValueFromAssignedPropertyRector\Fixture;

final class ReturnAfterAssign
{
private ?string $name;

public function __construct(string $name, bool $shouldReturn = false)
{
$this->name = $name;

if ($shouldReturn) {
return;
}

$this->name = strtoupper($name);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveDefaultValueFromAssignedPropertyRector\Fixture;

final class SkipAssignInForeach
{
private ?string $name = null;

/**
* @param string[] $names
*/
public function __construct(array $names)
{
foreach ($names as $name) {
$this->name = $name;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveDefaultValueFromAssignedPropertyRector\Fixture;

use Closure;

final class SkipAssignInsideClosure
{
private ?string $name = null;

public Closure $setName;

public function __construct(string $name)
{
$this->setName = function () use ($name): void {
$this->name = $name;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveDefaultValueFromAssignedPropertyRector\Fixture;

use Throwable;

final class SkipAssignOnlyInTry
{
private ?string $name = null;

public function __construct(string $name)
{
try {
$this->name = json_encode($name, JSON_THROW_ON_ERROR);
} catch (Throwable) {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveDefaultValueFromAssignedPropertyRector\Fixture;

final class SkipAssignReadsDefault
{
private int $count = 0;

public function __construct(int $extra)
{
$this->count = $this->count + $extra;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveDefaultValueFromAssignedPropertyRector\Fixture;

final class SkipAssignViaOffsetAccess
{
/**
* @var array<string, bool>
*/
private array $cachedByName = [];

public function __construct(string $name)
{
$this->cachedByName[$name] = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use Rector\NodeAnalyzer\AlwaysInitializedPropertyAnalyzer;
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
Expand All @@ -23,7 +26,9 @@ final class RemoveDefaultValueFromAssignedPropertyRector extends AbstractRector
{
public function __construct(
private readonly ConstructorAssignDetector $constructorAssignDetector,
private readonly BetterNodeFinder $betterNodeFinder
private readonly BetterNodeFinder $betterNodeFinder,
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer,
private readonly AlwaysInitializedPropertyAnalyzer $alwaysInitializedPropertyAnalyzer
) {
}

Expand Down Expand Up @@ -79,11 +84,6 @@ public function refactor(Node $node): ?Node
return null;
}

// early return can skip the assign, so the default value is still needed
if ($this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($constructClassMethod, Return_::class)) {
return null;
}

$hasChanged = false;

foreach ($node->getProperties() as $property) {
Expand All @@ -102,7 +102,23 @@ public function refactor(Node $node): ?Node
}

$propertyName = $this->getName($propertyProperty);
if (! $this->constructorAssignDetector->isPropertyAssigned($node, $propertyName)) {

// guards against an assign that reads the default value first, e.g. $this->value = $this->value + 1
if (! $this->constructorAssignDetector->isPropertyAssignedConditionally($node, $propertyName)) {
continue;
}

// the property must be initialized on every path out of the constructor,
// otherwise removing the default value leaves it uninitialized
if (! $this->alwaysInitializedPropertyAnalyzer->isAlwaysInitialized(
$constructClassMethod,
$propertyName
)) {
continue;
}

// $this->value['key'] = ... writes to the default value, it does not replace it
if ($this->isAssignedViaOffsetAccess($constructClassMethod, $propertyName)) {
continue;
}

Expand All @@ -117,4 +133,29 @@ public function refactor(Node $node): ?Node

return null;
}

private function isAssignedViaOffsetAccess(ClassMethod $constructClassMethod, string $propertyName): bool
{
$assign = $this->betterNodeFinder->findFirstInFunctionLikeScoped(
$constructClassMethod,
function (Node $node) use ($propertyName): bool {
if (! $node instanceof Assign) {
return false;
}

$assignVar = $node->var;
if (! $assignVar instanceof ArrayDimFetch) {
return false;
}

while ($assignVar instanceof ArrayDimFetch) {
$assignVar = $assignVar->var;
}

return $this->propertyFetchAnalyzer->isLocalPropertyFetchName($assignVar, $propertyName);
}
);

return $assign instanceof Assign;
}
}
Loading
Loading