Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 29af037

Browse files
authoredAug 1, 2023
Add sleep rector rule (#118)
* Add sleep rector rule * Fix up rule for Rector 0.17
1 parent ea9ebaa commit 29af037

File tree

6 files changed

+173
-5
lines changed

6 files changed

+173
-5
lines changed
 

‎docs/rector_rules_overview.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 34 Rules Overview
1+
# 36 Rules Overview
22

33
## AddArgumentDefaultValueRector
44

@@ -849,15 +849,30 @@ return static function (RectorConfig $rectorConfig): void {
849849

850850
<br>
851851

852+
## SleepFuncToSleepStaticCallRector
853+
854+
Use `Sleep::sleep()` and `Sleep::usleep()` instead of the `sleep()` and `usleep()` function.
855+
856+
- class: [`RectorLaravel\Rector\FuncCall\SleepFuncToSleepStaticCallRector`](../src/Rector/FuncCall/SleepFuncToSleepStaticCallRector.php)
857+
858+
```diff
859+
-sleep(5);
860+
+\Illuminate\Support\Sleep::sleep(5);
861+
```
862+
863+
<br>
864+
852865
## SubStrToStartsWithOrEndsWithStaticMethodCallRector
853866

854-
Change `substr()` to `startsWith()` or `endsWith()` static method call where applicable.
867+
Use `Str::startsWith()` or `Str::endsWith()` instead of `substr()` === `$str`
855868

856-
- class: [`RectorLaravel\Rector\FuncCall\SubStrToStartsWithOrEndsWithStaticMethodCallRector`](../src/Rector/Expr/SubStrToStartsWithOrEndsWithStaticMethodCallRector/SubStrToStartsWithOrEndsWithStaticMethodCallRector.php)
869+
- class: [`RectorLaravel\Rector\Expr\SubStrToStartsWithOrEndsWithStaticMethodCallRector\SubStrToStartsWithOrEndsWithStaticMethodCallRector`](../src/Rector/Expr/SubStrToStartsWithOrEndsWithStaticMethodCallRector/SubStrToStartsWithOrEndsWithStaticMethodCallRector.php)
857870

858871
```diff
859-
-$string = substr($string, 0, 5) === 'foo';
860-
+$string = Str::startsWith($string, 'foo');
872+
-if (substr($str, 0, 3) === 'foo') {
873+
+if (Str::startsWith($str, 'foo')) {
874+
// do something
875+
}
861876
```
862877

863878
<br>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RectorLaravel\Rector\FuncCall;
6+
7+
use PhpParser\Node;
8+
use Rector\Core\Rector\AbstractRector;
9+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
10+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
11+
12+
/**
13+
* @see \RectorLaravel\Tests\Rector\FuncCall\SleepFuncToSleepStaticCallRector\SleepFuncToSleepStaticCallRectorTest
14+
*/
15+
class SleepFuncToSleepStaticCallRector extends AbstractRector
16+
{
17+
public function getRuleDefinition(): RuleDefinition
18+
{
19+
return new RuleDefinition(
20+
'Use Sleep::sleep() and Sleep::usleep() instead of the sleep() and usleep() function.',
21+
[
22+
new CodeSample(
23+
<<<'CODE_SAMPLE'
24+
sleep(5);
25+
CODE_SAMPLE
26+
,
27+
<<<'CODE_SAMPLE'
28+
\Illuminate\Support\Sleep::sleep(5);
29+
CODE_SAMPLE
30+
,
31+
),
32+
]
33+
);
34+
}
35+
36+
public function getNodeTypes(): array
37+
{
38+
return [Node\Stmt\Expression::class];
39+
}
40+
41+
/**
42+
* @param Node\Stmt\Expression $node
43+
*/
44+
public function refactor(Node $node): ?Node
45+
{
46+
if (! $node->expr instanceof Node\Expr\FuncCall) {
47+
return null;
48+
}
49+
50+
if (! $this->isName($node->expr->name, 'sleep') && ! $this->isName($node->expr->name, 'usleep')) {
51+
return null;
52+
}
53+
54+
$method = $this->isName($node->expr->name, 'sleep') ? 'sleep' : 'usleep';
55+
56+
$node->expr = $this->nodeFactory->createStaticCall('Illuminate\Support\Sleep', $method, $node->expr->args);
57+
58+
return $node;
59+
}
60+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\FuncCall\SleepFuncToSleepStaticCallRector\Fixture;
4+
5+
class Fixture
6+
{
7+
public function store()
8+
{
9+
sleep(5);
10+
usleep(5);
11+
return true;
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace RectorLaravel\Tests\Rector\FuncCall\SleepFuncToSleepStaticCallRector\Fixture;
20+
21+
class Fixture
22+
{
23+
public function store()
24+
{
25+
\Illuminate\Support\Sleep::sleep(5);
26+
\Illuminate\Support\Sleep::usleep(5);
27+
return true;
28+
}
29+
}
30+
31+
?>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\FuncCall\SleepFuncToSleepStaticCallRector\Fixture;
4+
5+
class SkipAsUsingReturn
6+
{
7+
public function run()
8+
{
9+
$var = sleep(5);
10+
11+
if (sleep(5)) {
12+
return true;
13+
}
14+
15+
while(sleep(5)) {
16+
return true;
17+
}
18+
}
19+
}
20+
21+
?>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RectorLaravel\Tests\Rector\FuncCall\SleepFuncToSleepStaticCallRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class SleepFuncToSleepStaticCallRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
7+
use RectorLaravel\Rector\FuncCall\SleepFuncToSleepStaticCallRector;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');
11+
12+
$rectorConfig->rule(SleepFuncToSleepStaticCallRector::class);
13+
};

0 commit comments

Comments
 (0)
Please sign in to comment.