diff --git a/packages/NodeTypeResolver/Node/AttributeKey.php b/packages/NodeTypeResolver/Node/AttributeKey.php index 4dc977a7bbe..dbf38d5a369 100644 --- a/packages/NodeTypeResolver/Node/AttributeKey.php +++ b/packages/NodeTypeResolver/Node/AttributeKey.php @@ -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 diff --git a/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php b/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php index 464cf9afb1c..5ce3b19de1c 100644 --- a/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php +++ b/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ContextNodeVisitor.php @@ -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; @@ -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; } diff --git a/rules-tests/Php81/Rector/Property/ReadOnlyPropertyRector/Fixture/skip_increment.php.inc b/rules-tests/Php81/Rector/Property/ReadOnlyPropertyRector/Fixture/skip_increment.php.inc new file mode 100644 index 00000000000..802b496e7d2 --- /dev/null +++ b/rules-tests/Php81/Rector/Property/ReadOnlyPropertyRector/Fixture/skip_increment.php.inc @@ -0,0 +1,17 @@ +maxRetriesLeft--; + } +} + + diff --git a/src/NodeManipulator/PropertyManipulator.php b/src/NodeManipulator/PropertyManipulator.php index 5218e609360..0b550a920bc 100644 --- a/src/NodeManipulator/PropertyManipulator.php +++ b/src/NodeManipulator/PropertyManipulator.php @@ -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