Skip to content

Commit

Permalink
[Php81] Skip increment/decrement on ReadOnlyPropertyRector (#5267)
Browse files Browse the repository at this point in the history
* [Php81] Skip increment/decrement on ReadOnlyPropertyRector

* [Php81] Skip increment/decrement on ReadOnlyPropertyRector
  • Loading branch information
samsonasik committed Nov 21, 2023
1 parent 9fa5013 commit b5f7a09
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
7 changes: 6 additions & 1 deletion packages/NodeTypeResolver/Node/AttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,12 @@ final class AttributeKey
/**
* @var string
*/
public const IS_PARAM_VAR = 'IS_PARAM_VAR';
public const IS_PARAM_VAR = 'is_param_var';

/**
* @var string
*/
public const IS_INCREMENT_OR_DECREMENT = 'is_increment_or_decrement';

/**
* @var string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\PostDec;
use PhpParser\Node\Expr\PostInc;
use PhpParser\Node\Expr\PreDec;
use PhpParser\Node\Expr\PreInc;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
Expand Down Expand Up @@ -77,6 +81,10 @@ public function enterNode(Node $node): ?Node
return null;
}

if ($node instanceof PostDec || $node instanceof PostInc || $node instanceof PreDec || $node instanceof PreInc) {
$node->var->setAttribute(AttributeKey::IS_INCREMENT_OR_DECREMENT, true);
}

$this->processContextInClass($node);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

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

final class SkipIncrement
{
public function __construct(private int $maxRetriesLeft)
{
}

private function retry(): void
{
$this->maxRetriesLeft--;
}
}


6 changes: 5 additions & 1 deletion src/NodeManipulator/PropertyManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ private function isChangeableContext(PropertyFetch | StaticPropertyFetch $proper
return true;
}

return $propertyFetch->getAttribute(AttributeKey::IS_USED_AS_ARG_BY_REF_VALUE, false) === 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
Expand Down

0 comments on commit b5f7a09

Please sign in to comment.