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

Convert DateTimeImmutable to CarbonImmutable #5895

Merged
merged 3 commits into from
May 19, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Carbon\Rector\New_\DateTimeInstanceToCarbonRector\Fixture;

final class IncludeDateTimeImmutable
{
public function run()
{
$date = new \DateTimeImmutable('now');
}
}

?>
-----
<?php

namespace Rector\Tests\Carbon\Rector\New_\DateTimeInstanceToCarbonRector\Fixture;

final class IncludeDateTimeImmutable
{
public function run()
{
$date = \Carbon\CarbonImmutable::now();
}
}

?>
25 changes: 18 additions & 7 deletions rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\Carbon\Rector\New_;

use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
Expand Down Expand Up @@ -56,22 +57,32 @@ 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');
}

if ($node->isFirstClassCallable()) {
return null;
}

public function refactorWithClass(New_ $new, string $className): MethodCall|StaticCall|null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have the full picture probably but here I don't get why it is public (for who to call outside).

Else, thanks for the fix and test, I think next time, I'd be able to make a complete contribution at once 👌

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, this should be private 👍

{
if ($new->isFirstClassCallable()) {
return null;
}

// no arg? ::now()
$carbonFullyQualified = new FullyQualified('Carbon\Carbon');
if ($node->args === []) {
$carbonFullyQualified = new FullyQualified($className);

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

if (count($node->getArgs()) === 1) {
$firstArg = $node->getArgs()[0];
if (count($new->getArgs()) === 1) {
$firstArg = $new->getArgs()[0];

if ($firstArg->value instanceof String_) {
return $this->carbonCallFactory->createFromDateTimeString($carbonFullyQualified, $firstArg->value);
Expand Down
Loading