Skip to content

Commit

Permalink
add test fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 19, 2024
1 parent 6d7c4b5 commit f0ca44e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
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();
}
}

?>
15 changes: 5 additions & 10 deletions rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,21 @@ public function refactor(Node $node): ?Node
return null;
}

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

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

if ($node->args === []) {
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

0 comments on commit f0ca44e

Please sign in to comment.