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

[Php81] Skip increment/decrement on ReadOnlyPropertyRector #5267

Merged
merged 2 commits into from
Nov 21, 2023
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
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