Skip to content

Commit

Permalink
Convert DateTimeImmutable to CarbonImmutable
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed May 18, 2024
1 parent b2ec1f0 commit 6d7c4b5
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,31 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node->class, 'DateTime')) {
return null;
if ($this->isName($node->class, 'DateTime')) {
return $this->refactorWithClass($node, 'Carbon\\Carbon');
}

if ($this->isName($node->class, 'DateTimeImmutable')) {
return $this->refactorWithClass($node, 'Carbon\\CarbonImmutable');
}

return null;
}

/**
* @param New_ $node
* @param class-string<\Carbon\Carbon|\Carbon\CarbonImmutable> $className
*/
public function refactorWithClass(Node $node, string $className) : ?Node
{
if ($node->isFirstClassCallable()) {
return null;
}

// no arg? ::now()
$carbonFullyQualified = new FullyQualified('Carbon\Carbon');
$carbonFullyQualified = new FullyQualified('Carbon\\Carbon');
$carbonFullyQualified = new FullyQualified($className);

if ($node->args === []) {
return new StaticCall($carbonFullyQualified, new Identifier('now'));
}
Expand Down

0 comments on commit 6d7c4b5

Please sign in to comment.