Skip to content

Commit 8b44b1d

Browse files
[CodingStyle] Skip by ref params on BinaryOpStandaloneAssignsToDirectRector (#7471)
* [CodingStyle] Skip by ref params on BinaryOpStandaloneAssignsToDirectRector * [ci-review] Rector Rectify * fix --------- Co-authored-by: GitHub Action <[email protected]>
1 parent 5697100 commit 8b44b1d

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\BinaryOpStandaloneAssignsToDirectRector\Fixture;
4+
5+
final class SkipByRefFromParamVariable
6+
{
7+
public function run(&$first, &$second)
8+
{
9+
$first = 100;
10+
$second = 200;
11+
12+
return $first <=> $second;
13+
}
14+
}

rules/CodingStyle/Rector/ClassMethod/BinaryOpStandaloneAssignsToDirectRector.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ public function refactor(Node $node): ?Node
105105
return null;
106106
}
107107

108+
$resolveParamByRefVariables = $this->resolveParamByRefVariables($node);
109+
if ($this->isNames($binaryOp->left, $resolveParamByRefVariables)) {
110+
return null;
111+
}
112+
113+
if ($this->isNames($binaryOp->right, $resolveParamByRefVariables)) {
114+
return null;
115+
}
116+
108117
$binaryOp->left = $firstVariableAndExprAssign->getExpr();
109118
$binaryOp->right = $secondVariableAndExprAssign->getExpr();
110119

@@ -117,6 +126,32 @@ public function provideMinPhpVersion(): int
117126
return PhpVersionFeature::VARIADIC_PARAM;
118127
}
119128

129+
/**
130+
* @return string[]
131+
*/
132+
private function resolveParamByRefVariables(ClassMethod|Function_|Closure $node): array
133+
{
134+
$paramByRefVariables = [];
135+
foreach ($node->params as $param) {
136+
if (! $param->var instanceof Variable) {
137+
continue;
138+
}
139+
140+
if (! $param->byRef) {
141+
continue;
142+
}
143+
144+
$paramName = $this->getName($param->var);
145+
if ($paramName === null) {
146+
continue;
147+
}
148+
149+
$paramByRefVariables[] = $paramName;
150+
}
151+
152+
return $paramByRefVariables;
153+
}
154+
120155
private function matchToVariableAssignExpr(Stmt $stmt): ?VariableAndExprAssign
121156
{
122157
if (! $stmt instanceof Expression) {

0 commit comments

Comments
 (0)