Skip to content

Commit

Permalink
[DowngradePhp70] Add class out of trait (#6385)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 7, 2021
1 parent 550d147 commit 3cdbd48
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ class ClassExists
}
}

?>
?>
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ trait ClassInTrait

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ class Fixture
}
}

?>
?>
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ function () {
return new AnonymousFor_NotInFunction();
};

?>
?>
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ function () {
return new AnonymousFor_NotInfunctionNoNamespace();
};

?>
?>
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ function inFunction()
return new AnonymousFor_inFunction();
}

?>
?>
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ class AnonymousFor_inFunctionNoNamespace
function inFunctionNoNamespace()
{
return new AnonymousFor_inFunctionNoNamespace();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ class NoNamespace
{
return new AnonymousFor_NoNamespace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ class AnonymousFor_NotInFunction
}
return new AnonymousFor_NotInFunction();

?>
?>
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ class AnonymousFor_NotInfunctionNoNamespace
{
}
}
return new AnonymousFor_NotInfunctionNoNamespace();
return new AnonymousFor_NotInfunctionNoNamespace();
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ class WithArgs
}
}

?>
?>
18 changes: 8 additions & 10 deletions rules/DowngradePhp70/Rector/New_/DowngradeAnonymousClassRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
Expand Down Expand Up @@ -106,9 +105,9 @@ public function refactor(Node $node): ?Node
return null;
}

$classNode = $this->betterNodeFinder->findParentType($node, Class_::class);
if ($classNode instanceof Class_) {
return $this->processMoveAnonymousClassInClass($node, $classNode);
$classLike = $this->betterNodeFinder->findParentType($node, ClassLike::class);
if ($classLike instanceof ClassLike) {
return $this->processMoveAnonymousClassInClass($node, $classLike);
}

$functionNode = $this->betterNodeFinder->findParentType($node, Function_::class);
Expand Down Expand Up @@ -141,19 +140,18 @@ private function getClassName(string $namespace, string $shortName): string
return ucfirst($className);
}

private function processMoveAnonymousClassInClass(New_ $new, Class_ $class): ?New_
private function processMoveAnonymousClassInClass(New_ $new, ClassLike $classLike): ?New_
{
$namespacedClassName = $this->getName($class->namespacedName);
/** @var Identifier $shortClassName */
$shortClassName = $class->name;
$shortClassName = (string) $this->getName($shortClassName);
$namespacedClassName = $this->getName($classLike->namespacedName);

$shortClassName = $this->nodeNameResolver->getShortName($classLike->name);

$namespace = $namespacedClassName === $shortClassName
? ''
: Strings::substring($namespacedClassName, 0, - strlen($shortClassName) - 1);
$className = $this->getClassName($namespace, $shortClassName);

return $this->processMove($new, $className, $class);
return $this->processMove($new, $className, $classLike);
}

private function processMoveAnonymousClassInFunction(New_ $new, Function_ $function): ?New_
Expand Down

0 comments on commit 3cdbd48

Please sign in to comment.