From 6d7c4b58d899edc28f3524144b049ac86299ca76 Mon Sep 17 00:00:00 2001 From: Bastien Miclo Date: Sat, 18 May 2024 19:16:00 +0200 Subject: [PATCH 1/3] Convert DateTimeImmutable to CarbonImmutable --- .../New_/DateTimeInstanceToCarbonRector.php | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php b/rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php index c7ba81cc734..b050a1cc4f2 100644 --- a/rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php +++ b/rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php @@ -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')); } From f0ca44ee081fc48e912a441a8196032d2eb47ac5 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 19 May 2024 10:59:21 +0900 Subject: [PATCH 2/3] add test fixture --- .../include_datetime_immutable.php.inc | 27 +++++++++++++++++++ .../New_/DateTimeInstanceToCarbonRector.php | 15 ++++------- 2 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 rules-tests/Carbon/Rector/New_/DateTimeInstanceToCarbonRector/Fixture/include_datetime_immutable.php.inc diff --git a/rules-tests/Carbon/Rector/New_/DateTimeInstanceToCarbonRector/Fixture/include_datetime_immutable.php.inc b/rules-tests/Carbon/Rector/New_/DateTimeInstanceToCarbonRector/Fixture/include_datetime_immutable.php.inc new file mode 100644 index 00000000000..4aa5fa52884 --- /dev/null +++ b/rules-tests/Carbon/Rector/New_/DateTimeInstanceToCarbonRector/Fixture/include_datetime_immutable.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php b/rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php index b050a1cc4f2..0b43e558085 100644 --- a/rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php +++ b/rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php @@ -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); From 6f8c28a21ef494fcc20ee164adeac82cb3717286 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Sun, 19 May 2024 02:05:08 +0000 Subject: [PATCH 3/3] [ci-review] Rector Rectify --- rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php b/rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php index 0b43e558085..28c71e8d5a3 100644 --- a/rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php +++ b/rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php @@ -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; @@ -67,7 +68,7 @@ public function refactor(Node $node): ?Node return null; } - public function refactorWithClass(New_ $new, string $className): Node\Expr\MethodCall|StaticCall|null + public function refactorWithClass(New_ $new, string $className): MethodCall|StaticCall|null { if ($new->isFirstClassCallable()) { return null;