Skip to content

Commit

Permalink
[TypeDeclaration] Skip unset by trait on TypedPropertyFromStrictConst…
Browse files Browse the repository at this point in the history
…ructorRector
  • Loading branch information
samsonasik committed Jan 10, 2025
1 parent 8da1bfe commit 3c4f14e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

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

use Doctrine\ORM\EntityManagerInterface;
use Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector\Source\TraitWithUnsetProperty;

final class SkipUnsetByTrait
{
use TraitWithUnsetProperty;

/**
* @var EntityManagerInterface
*/
private $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
}

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

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

trait TraitWithUnsetProperty
{
public function reset()
{
unset($this->entityManager);
}
}
17 changes: 14 additions & 3 deletions src/NodeAnalyzer/PropertyFetchAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
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;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PhpParser\AstResolver;
use Rector\PhpParser\Node\BetterNodeFinder;
Expand All @@ -41,7 +43,9 @@ public function __construct(
private BetterNodeFinder $betterNodeFinder,
private AstResolver $astResolver,
private NodeTypeResolver $nodeTypeResolver,
private ReflectionResolver $reflectionResolver
private ReflectionResolver $reflectionResolver,
private ContextAnalyzer $contextAnalyzer,
private PropertyWriteonlyAnalyzer $propertyWriteonlyAnalyzer
) {
}

Expand Down Expand Up @@ -113,11 +117,18 @@ public function containsWrittenPropertyFetchName(Trait_ $trait, string $property
return (bool) $this->betterNodeFinder->findFirst(
$trait,
function (Node $node) use ($propertyName): bool {
if (! $node instanceof Assign) {
if (! $this->isLocalPropertyFetchName($node, $propertyName)) {
return false;
}

return $this->isLocalPropertyFetchName($node->var, $propertyName);
/**
* @var PropertyFetch|StaticPropertyFetch|NullsafePropertyFetch $node
*/
if ($this->contextAnalyzer->isChangeableContext($node)) {
return true;
}

return $this->propertyWriteonlyAnalyzer->arePropertyFetchesExclusivelyBeingAssignedTo([$node]);
}
);
}
Expand Down
23 changes: 4 additions & 19 deletions src/NodeManipulator/PropertyManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeNestingScope\ContextAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
Expand Down Expand Up @@ -59,7 +60,8 @@ public function __construct(
private PromotedPropertyResolver $promotedPropertyResolver,
private ConstructorAssignDetector $constructorAssignDetector,
private AstResolver $astResolver,
private PropertyFetchAnalyzer $propertyFetchAnalyzer
private PropertyFetchAnalyzer $propertyFetchAnalyzer,
private ContextAnalyzer $contextAnalyzer
) {
}

Expand All @@ -78,7 +80,7 @@ public function isPropertyChangeableExceptConstructor(
$classMethod = $class->getMethod(MethodName::CONSTRUCT);

foreach ($propertyFetches as $propertyFetch) {
if ($this->isChangeableContext($propertyFetch)) {
if ($this->contextAnalyzer->isChangeableContext($propertyFetch)) {
return true;
}

Expand Down Expand Up @@ -189,23 +191,6 @@ private function isPropertyAssignedOnlyInConstructor(
return $this->constructorAssignDetector->isPropertyAssigned($class, $propertyName);
}

private function isChangeableContext(PropertyFetch | StaticPropertyFetch $propertyFetch): bool
{
if ($propertyFetch->getAttribute(AttributeKey::IS_UNSET_VAR, false)) {
return true;
}

if ($propertyFetch->getAttribute(AttributeKey::INSIDE_ARRAY_DIM_FETCH, false)) {
return true;
}

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

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

private function hasAllowedNotReadonlyAnnotationOrAttribute(PhpDocInfo $phpDocInfo, Class_ $class): bool
{
if ($phpDocInfo->hasByAnnotationClasses(self::ALLOWED_NOT_READONLY_CLASS_ANNOTATIONS)) {
Expand Down
22 changes: 22 additions & 0 deletions src/NodeNestingScope/ContextAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
namespace Rector\NodeNestingScope;

use PhpParser\Node;
use PhpParser\Node\Expr\NullsafePropertyFetch;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use Rector\DeadCode\NodeAnalyzer\PropertyWriteonlyAnalyzer;
use Rector\NodeManipulator\AssignManipulator;
use Rector\NodeTypeResolver\Node\AttributeKey;

final class ContextAnalyzer
Expand All @@ -24,4 +29,21 @@ public function isInIf(Node $node): bool
{
return $node->getAttribute(AttributeKey::IS_IN_IF) === true;
}

public function isChangeableContext(PropertyFetch | StaticPropertyFetch | NullsafePropertyFetch $propertyFetch): bool
{
if ($propertyFetch->getAttribute(AttributeKey::IS_UNSET_VAR, false)) {
return true;
}

if ($propertyFetch->getAttribute(AttributeKey::INSIDE_ARRAY_DIM_FETCH, false)) {
return true;
}

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

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

0 comments on commit 3c4f14e

Please sign in to comment.