Skip to content

Commit

Permalink
[DowngradePhp70] Add class out of trait
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 7, 2021
1 parent 534f30d commit c375193
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Rector\Tests\DowngradePhp70\Rector\New_\DowngradeAnonymousClassRector\Fixture;

trait ClassInTrait
{
public function run()
{
$message = 'error';
return new class($message) extends \InvalidArgumentException {};
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp70\Rector\New_\DowngradeAnonymousClassRector\Fixture;

class AnonymousFor_NotInFunction extends \InvalidArgumentException
{
}
trait ClassInTrait
{
public function run()
{
$message = 'error';
return new AnonymousFor_NotInFunction($message);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Namespace_;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
Expand Down Expand Up @@ -193,7 +194,13 @@ private function processMove(New_ $new, string $className, Node $node): ?New_
'attrGroups' => $new->class->attrGroups,
]
);
$this->addNodeBeforeNode($class, $node);

$currentClass = $node->getAttribute(AttributeKey::CLASS_NODE);
if ($currentClass instanceof ClassLike) {
$this->addNodeBeforeNode($class, $currentClass);
} else {
$this->addNodeBeforeNode($class, $node);
}

return new New_(new Name($className), $new->args);
}
Expand Down

0 comments on commit c375193

Please sign in to comment.