Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DowngradePhp70] Add class out of trait #6385

Merged
merged 1 commit into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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