Skip to content

Commit

Permalink
fix: add some tests with current object
Browse files Browse the repository at this point in the history
  • Loading branch information
guideloince committed Jan 30, 2025
1 parent 4a3c957 commit d052ec1
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 5 deletions.
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,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 Objects
{
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 @@ -136,14 +136,19 @@ private function resolveCurrentStmtVariableName(Stmt $stmt): ?string
}

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

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

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

Expand Down

0 comments on commit d052ec1

Please sign in to comment.