Skip to content

Commit

Permalink
fix: NewlineBeforeNewAssignSetRector variable as same name like prope…
Browse files Browse the repository at this point in the history
…rty (#6705)

* fix: NewlineBeforeNewAssignSetRector variable as same name like property

* Add fixture

* Fix class name

* Remove override

* fix: add some tests with current object
  • Loading branch information
guideloince authored Jan 31, 2025
1 parent 756c244 commit c781983
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector\Fixture;

final class Objects
{
private ?Value $foo = null;
private ?Value $bar = null;
public function run()
{
$this->foo = new Value;
$this->foo->bar = 1;
$this->bar = new Value;
$this->bar->bar = 1;
}
}

?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector\Fixture;

final class Objects
{
private ?Value $foo = null;
private ?Value $bar = null;
public function run()
{
$this->foo = new Value;
$this->foo->bar = 1;

$this->bar = new Value;
$this->bar->bar = 1;
}
}

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

namespace Rector\Tests\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector\Fixture;

final class Properties
{
public function run()
{
$foo = new Value;
$foo->bar = new Value;
$foo->bar->bar = 5;
$bar = new Value;
$bar->foo = 1;
}
}

?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector\Fixture;

final class Properties
{
public function run()
{
$foo = new Value;
$foo->bar = new Value;
$foo->bar->bar = 5;

$bar = new Value;
$bar->foo = 1;
}
}

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

namespace Rector\Tests\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector\Fixture;

final class Statics
{
protected static ?Value $fooBar = null;
protected static ?Value $barFoo = null;
public function run()
{
self::$fooBar = new Value;
self::$fooBar->bar = 1;
self::$barFoo = new Value;
self::$barFoo->bar = 1;
}
}

?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector\Fixture;

final class Statics
{
protected static ?Value $fooBar = null;
protected static ?Value $barFoo = null;
public function run()
{
self::$fooBar = new Value;
self::$fooBar->bar = 1;

self::$barFoo = new Value;
self::$barFoo->bar = 1;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public function run()
$value->setValue(5);
$value2 = new Value;
$value2->setValue(1);
$foo = new Value;
$foo->bar = 5;
$bar = new Value;
$bar->foo = 1;
}
}
CODE_SAMPLE
Expand All @@ -56,6 +60,12 @@ public function run()
$value2 = new Value;
$value2->setValue(1);
$foo = new Value;
$foo->bar = 5;
$bar = new Value;
$bar->foo = 1;
}
}
CODE_SAMPLE
Expand Down Expand Up @@ -126,7 +136,19 @@ private function resolveCurrentStmtVariableName(Stmt $stmt): ?string
}

if (! $stmtExpr->var instanceof MethodCall && ! $stmtExpr->var instanceof StaticCall) {
return $this->getName($stmtExpr->var);
$nodeVar = $stmtExpr->var;

if ($nodeVar instanceof Node\Expr\PropertyFetch) {
do {
$previous = $nodeVar;
$nodeVar = $nodeVar->var;
} while ($nodeVar instanceof Node\Expr\PropertyFetch);

if ($this->getName($nodeVar) === 'this') {
$nodeVar = $previous;
}
}
return $this->getName($nodeVar);
}
}

Expand Down

0 comments on commit c781983

Please sign in to comment.