Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TypeDeclaration] Skip changed by ref from trait on TypedPropertyFromStrictConstructorRector #6666

Merged
merged 2 commits into from
Jan 11, 2025
Merged
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,17 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector\Source\ByRefTrait;

final class SkipByRefAssignOnTrait
{
use ByRefTrait;

private $str;

public function __construct(string $str)
{
$this->str = $str;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector\Source;

trait ByRefTrait
{
public function run()
{
$str = &$this->str;
$str = null;
}
}
6 changes: 2 additions & 4 deletions src/NodeAnalyzer/PropertyFetchAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ThisType;
use Rector\DeadCode\NodeAnalyzer\PropertyWriteonlyAnalyzer;
use Rector\Enum\ObjectReference;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeNestingScope\ContextAnalyzer;
Expand All @@ -44,8 +43,7 @@ public function __construct(
private AstResolver $astResolver,
private NodeTypeResolver $nodeTypeResolver,
private ReflectionResolver $reflectionResolver,
private ContextAnalyzer $contextAnalyzer,
private PropertyWriteonlyAnalyzer $propertyWriteonlyAnalyzer
private ContextAnalyzer $contextAnalyzer
) {
}

Expand Down Expand Up @@ -128,7 +126,7 @@ function (Node $node) use ($propertyName): bool {
return true;
}

return $this->propertyWriteonlyAnalyzer->arePropertyFetchesExclusivelyBeingAssignedTo([$node]);
return $this->contextAnalyzer->isLeftPartOfAssign($node);
}
);
}
Expand Down
20 changes: 4 additions & 16 deletions src/NodeManipulator/AssignManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use PhpParser\Node\FunctionLike;
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeNestingScope\ContextAnalyzer;
use Rector\Php72\ValueObject\ListAndEach;
use Rector\PhpParser\Node\BetterNodeFinder;

Expand All @@ -23,7 +23,8 @@
public function __construct(
private NodeNameResolver $nodeNameResolver,
private BetterNodeFinder $betterNodeFinder,
private PropertyFetchAnalyzer $propertyFetchAnalyzer
private PropertyFetchAnalyzer $propertyFetchAnalyzer,
private ContextAnalyzer $contextAnalyzer
) {
}

Expand Down Expand Up @@ -61,19 +62,6 @@ public function matchListAndEach(Assign $assign): ?ListAndEach
return new ListAndEach($assign->var, $bareExpr);
}

public function isLeftPartOfAssign(Node $node): bool
{
if ($node->getAttribute(AttributeKey::IS_BEING_ASSIGNED) === true) {
return true;
}

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

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

/**
* @api doctrine
* @return array<PropertyFetch|StaticPropertyFetch>
Expand All @@ -85,7 +73,7 @@ public function resolveAssignsToLocalPropertyFetches(FunctionLike $functionLike)
return false;
}

return $this->isLeftPartOfAssign($node);
return $this->contextAnalyzer->isLeftPartOfAssign($node);
});
}
}
3 changes: 1 addition & 2 deletions src/NodeManipulator/PropertyManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
];

public function __construct(
private AssignManipulator $assignManipulator,
private BetterNodeFinder $betterNodeFinder,
private PhpDocInfoFactory $phpDocInfoFactory,
private PropertyFetchFinder $propertyFetchFinder,
Expand Down Expand Up @@ -91,7 +90,7 @@ public function isPropertyChangeableExceptConstructor(
continue;
}

if ($this->assignManipulator->isLeftPartOfAssign($propertyFetch)) {
if ($this->contextAnalyzer->isLeftPartOfAssign($propertyFetch)) {
return true;
}

Expand Down
13 changes: 13 additions & 0 deletions src/NodeNestingScope/ContextAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,17 @@ public function isChangeableContext(

return $propertyFetch->getAttribute(AttributeKey::IS_INCREMENT_OR_DECREMENT, false) === true;
}

public function isLeftPartOfAssign(Node $node): bool
{
if ($node->getAttribute(AttributeKey::IS_BEING_ASSIGNED) === true) {
return true;
}

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

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