Skip to content

Commit

Permalink
[Php81] Allow used as assign expr on ReadOnlyPropertyRector (#6331)
Browse files Browse the repository at this point in the history
* [Php81] Allow used as assign expr on ReadOnlyPropertyRector

* clean up
  • Loading branch information
samsonasik authored Sep 27, 2024
1 parent 834c9ff commit e2d9647
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;

/**
* @see https://3v4l.org/e6q3d used by ref readonly is got fatal error
*/
class SkipUsedAsAssignByRef
{
public function __construct(private string $foo)
{}

public function run()
{
$data['data'] = &$this->foo;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;

class UsedAsAssignExpr
{
public function __construct(private string $foo)
{}

public function run()
{
$data['data'] = $this->foo;
}
}

?>
-----
<?php

namespace Rector\Tests\Php81\Rector\Property\ReadOnlyPropertyRector\Fixture;

class UsedAsAssignExpr
{
public function __construct(private readonly string $foo)
{}

public function run()
{
$data['data'] = $this->foo;
}
}

?>
6 changes: 5 additions & 1 deletion src/NodeManipulator/AssignManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ public function isLeftPartOfAssign(Node $node): bool
return true;
}

return $node->getAttribute(AttributeKey::IS_ASSIGNED_TO) === true;
if ($node->getAttribute(AttributeKey::IS_ASSIGN_REF_EXPR) === true) {
return true;
}

return $node->getAttribute(AttributeKey::IS_ASSIGN_OP_VAR) === true;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/NodeTypeResolver/Node/AttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ final class AttributeKey
*/
public const IS_BEING_ASSIGNED = 'is_being_assigned';

/**
* @var string
*/
public const IS_ASSIGN_OP_VAR = 'is_assign_op_var';

/**
* @var string
*/
public const IS_ASSIGN_REF_EXPR = 'is_assign_ref_expr';

/**
* @var string
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignOp;
use PhpParser\Node\Expr\AssignRef;
use PhpParser\NodeVisitorAbstract;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface;
Expand All @@ -19,7 +20,12 @@ final class AssignedToNodeVisitor extends NodeVisitorAbstract implements ScopeRe
public function enterNode(Node $node): ?Node
{
if ($node instanceof AssignOp) {
$node->var->setAttribute(AttributeKey::IS_ASSIGNED_TO, true);
$node->var->setAttribute(AttributeKey::IS_ASSIGN_OP_VAR, true);
return null;
}

if ($node instanceof AssignRef) {
$node->expr->setAttribute(AttributeKey::IS_ASSIGN_REF_EXPR, true);
return null;
}

Expand Down

0 comments on commit e2d9647

Please sign in to comment.