From 2b63db4846b41860764dc2b6ce9ce9a8e22f314b Mon Sep 17 00:00:00 2001 From: Peter Fox Date: Fri, 2 Jun 2023 21:16:46 +0100 Subject: [PATCH 1/3] Converts now()->startOfDay() to today() --- ...hStartOfDayMethodCallToTodayFuncRector.php | 50 +++++++++++++++++++ .../Fixture/fixture.php.inc | 29 +++++++++++ .../Fixture/skip_now_with_other_calls.php.inc | 27 ++++++++++ ...rtOfDayMethodCallToTodayFuncRectorTest.php | 28 +++++++++++ .../config/configured_rule.php | 13 +++++ 5 files changed, 147 insertions(+) create mode 100644 src/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector.php create mode 100644 tests/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector/Fixture/fixture.php.inc create mode 100644 tests/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector/Fixture/skip_now_with_other_calls.php.inc create mode 100644 tests/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector/NowFuncWithStartOfDayMethodCallToTodayFuncRectorTest.php create mode 100644 tests/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector/config/configured_rule.php diff --git a/src/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector.php b/src/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector.php new file mode 100644 index 00000000..7897c344 --- /dev/null +++ b/src/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector.php @@ -0,0 +1,50 @@ +startOfDay()', [ + new CodeSample( + <<<'CODE_SAMPLE' +$now = now()->startOfDay(); +CODE_SAMPLE, + <<<'CODE_SAMPLE' +$now = today(); +CODE_SAMPLE + ), + ]); + } + + public function getNodeTypes(): array + { + return [MethodCall::class]; + } + + /** + * @param MethodCall $node + */ + public function refactor(Node $node): ?Node + { + if (! $this->isName($node->name, 'startOfDay')) { + return null; + } + + if (! $this->isName($node->var, 'now')) { + return null; + } + + return $this->nodeFactory->createFuncCall('today'); + } +} diff --git a/tests/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector/Fixture/fixture.php.inc b/tests/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector/Fixture/fixture.php.inc new file mode 100644 index 00000000..78cc125f --- /dev/null +++ b/tests/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector/Fixture/fixture.php.inc @@ -0,0 +1,29 @@ +startOfDay(); + $tomorrow = now()->startOfDay()->addDay(); + } +} + +?> +----- +addDay(); + } +} + +?> diff --git a/tests/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector/Fixture/skip_now_with_other_calls.php.inc b/tests/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector/Fixture/skip_now_with_other_calls.php.inc new file mode 100644 index 00000000..9d4fd1b9 --- /dev/null +++ b/tests/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector/Fixture/skip_now_with_other_calls.php.inc @@ -0,0 +1,27 @@ +addDay()->startOfDay(); + } +} + +?> +----- +addDay()->startOfDay(); + } +} + +?> diff --git a/tests/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector/NowFuncWithStartOfDayMethodCallToTodayFuncRectorTest.php b/tests/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector/NowFuncWithStartOfDayMethodCallToTodayFuncRectorTest.php new file mode 100644 index 00000000..93962897 --- /dev/null +++ b/tests/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector/NowFuncWithStartOfDayMethodCallToTodayFuncRectorTest.php @@ -0,0 +1,28 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector/config/configured_rule.php b/tests/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector/config/configured_rule.php new file mode 100644 index 00000000..addb73a7 --- /dev/null +++ b/tests/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector/config/configured_rule.php @@ -0,0 +1,13 @@ +import(__DIR__ . '/../../../../../config/config.php'); + + $rectorConfig->rule(\RectorLaravel\Rector\FuncCall\NowFuncWithStartOfDayMethodCallToTodayFuncRector::class); +}; From aaeb80a870cec7a572be806da2aaa3f66a8d2cd3 Mon Sep 17 00:00:00 2001 From: Peter Fox Date: Sat, 3 Jun 2023 13:47:39 +0100 Subject: [PATCH 2/3] Add docs --- docs/rector_rules_overview.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/docs/rector_rules_overview.md b/docs/rector_rules_overview.md index 5d2de02a..cb49f6e2 100644 --- a/docs/rector_rules_overview.md +++ b/docs/rector_rules_overview.md @@ -403,6 +403,25 @@ Change minutes argument to seconds in `Illuminate\Contracts\Cache\Store` and Ill
+## NowFuncWithStartOfDayMethodCallToTodayFuncRector + +Changes the user of `now()->startOfDay()` to be replaced with `today()`. + +- class: [`RectorLaravel\Rector\FuncCall\NowFuncWithStartOfDayMethodCallToTodayFuncRector`](../src/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector.php) + +```diff +class SomeClass +{ + public function run() + { +- now()->startOfDay(); ++ today(); + } +} +``` + +
+ ## OptionalToNullsafeOperatorRector Convert simple calls to optional helper to use the nullsafe operator From 3edff38e187fd49406509f353df904679b230681 Mon Sep 17 00:00:00 2001 From: Peter Fox Date: Sat, 3 Jun 2023 13:59:26 +0100 Subject: [PATCH 3/3] improve return type --- .../NowFuncWithStartOfDayMethodCallToTodayFuncRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector.php b/src/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector.php index 7897c344..7e4ffc0e 100644 --- a/src/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector.php +++ b/src/Rector/FuncCall/NowFuncWithStartOfDayMethodCallToTodayFuncRector.php @@ -35,7 +35,7 @@ public function getNodeTypes(): array /** * @param MethodCall $node */ - public function refactor(Node $node): ?Node + public function refactor(Node $node): ?Node\Expr\FuncCall { if (! $this->isName($node->name, 'startOfDay')) { return null;