diff --git a/docs/rector_rules_overview.md b/docs/rector_rules_overview.md index ab27b068..45ca1f11 100644 --- a/docs/rector_rules_overview.md +++ b/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# 34 Rules Overview +# 36 Rules Overview ## AddArgumentDefaultValueRector @@ -849,15 +849,30 @@ return static function (RectorConfig $rectorConfig): void {
+## SleepFuncToSleepStaticCallRector + +Use `Sleep::sleep()` and `Sleep::usleep()` instead of the `sleep()` and `usleep()` function. + +- class: [`RectorLaravel\Rector\FuncCall\SleepFuncToSleepStaticCallRector`](../src/Rector/FuncCall/SleepFuncToSleepStaticCallRector.php) + +```diff +-sleep(5); ++\Illuminate\Support\Sleep::sleep(5); +``` + +
+ ## SubStrToStartsWithOrEndsWithStaticMethodCallRector -Change `substr()` to `startsWith()` or `endsWith()` static method call where applicable. +Use `Str::startsWith()` or `Str::endsWith()` instead of `substr()` === `$str` -- class: [`RectorLaravel\Rector\FuncCall\SubStrToStartsWithOrEndsWithStaticMethodCallRector`](../src/Rector/Expr/SubStrToStartsWithOrEndsWithStaticMethodCallRector/SubStrToStartsWithOrEndsWithStaticMethodCallRector.php) +- class: [`RectorLaravel\Rector\Expr\SubStrToStartsWithOrEndsWithStaticMethodCallRector\SubStrToStartsWithOrEndsWithStaticMethodCallRector`](../src/Rector/Expr/SubStrToStartsWithOrEndsWithStaticMethodCallRector/SubStrToStartsWithOrEndsWithStaticMethodCallRector.php) ```diff --$string = substr($string, 0, 5) === 'foo'; -+$string = Str::startsWith($string, 'foo'); +-if (substr($str, 0, 3) === 'foo') { ++if (Str::startsWith($str, 'foo')) { + // do something + } ```
diff --git a/src/Rector/FuncCall/SleepFuncToSleepStaticCallRector.php b/src/Rector/FuncCall/SleepFuncToSleepStaticCallRector.php new file mode 100644 index 00000000..a718acff --- /dev/null +++ b/src/Rector/FuncCall/SleepFuncToSleepStaticCallRector.php @@ -0,0 +1,60 @@ +expr instanceof Node\Expr\FuncCall) { + return null; + } + + if (! $this->isName($node->expr->name, 'sleep') && ! $this->isName($node->expr->name, 'usleep')) { + return null; + } + + $method = $this->isName($node->expr->name, 'sleep') ? 'sleep' : 'usleep'; + + $node->expr = $this->nodeFactory->createStaticCall('Illuminate\Support\Sleep', $method, $node->expr->args); + + return $node; + } +} diff --git a/tests/Rector/FuncCall/SleepFuncToSleepStaticCallRector/Fixture/fixture.php.inc b/tests/Rector/FuncCall/SleepFuncToSleepStaticCallRector/Fixture/fixture.php.inc new file mode 100644 index 00000000..3e6680df --- /dev/null +++ b/tests/Rector/FuncCall/SleepFuncToSleepStaticCallRector/Fixture/fixture.php.inc @@ -0,0 +1,31 @@ + +----- + diff --git a/tests/Rector/FuncCall/SleepFuncToSleepStaticCallRector/Fixture/skip_as_using_return.php.inc b/tests/Rector/FuncCall/SleepFuncToSleepStaticCallRector/Fixture/skip_as_using_return.php.inc new file mode 100644 index 00000000..300c5cd3 --- /dev/null +++ b/tests/Rector/FuncCall/SleepFuncToSleepStaticCallRector/Fixture/skip_as_using_return.php.inc @@ -0,0 +1,21 @@ + diff --git a/tests/Rector/FuncCall/SleepFuncToSleepStaticCallRector/SleepFuncToSleepStaticCallRectorTest.php b/tests/Rector/FuncCall/SleepFuncToSleepStaticCallRector/SleepFuncToSleepStaticCallRectorTest.php new file mode 100644 index 00000000..c0850ac5 --- /dev/null +++ b/tests/Rector/FuncCall/SleepFuncToSleepStaticCallRector/SleepFuncToSleepStaticCallRectorTest.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/SleepFuncToSleepStaticCallRector/config/configured_rule.php b/tests/Rector/FuncCall/SleepFuncToSleepStaticCallRector/config/configured_rule.php new file mode 100644 index 00000000..3d4ca175 --- /dev/null +++ b/tests/Rector/FuncCall/SleepFuncToSleepStaticCallRector/config/configured_rule.php @@ -0,0 +1,13 @@ +import(__DIR__ . '/../../../../../config/config.php'); + + $rectorConfig->rule(SleepFuncToSleepStaticCallRector::class); +};