Skip to content

Commit

Permalink
Add sleep rector rule (#118)
Browse files Browse the repository at this point in the history
* Add sleep rector rule
* Fix up rule for Rector 0.17
  • Loading branch information
peterfox authored Aug 1, 2023
1 parent ea9ebaa commit 29af037
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 5 deletions.
25 changes: 20 additions & 5 deletions docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 34 Rules Overview
# 36 Rules Overview

## AddArgumentDefaultValueRector

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

<br>

## 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);
```

<br>

## 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
}
```

<br>
Expand Down
60 changes: 60 additions & 0 deletions src/Rector/FuncCall/SleepFuncToSleepStaticCallRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace RectorLaravel\Rector\FuncCall;

use PhpParser\Node;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \RectorLaravel\Tests\Rector\FuncCall\SleepFuncToSleepStaticCallRector\SleepFuncToSleepStaticCallRectorTest
*/
class SleepFuncToSleepStaticCallRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Use Sleep::sleep() and Sleep::usleep() instead of the sleep() and usleep() function.',
[
new CodeSample(
<<<'CODE_SAMPLE'
sleep(5);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
\Illuminate\Support\Sleep::sleep(5);
CODE_SAMPLE
,
),
]
);
}

public function getNodeTypes(): array
{
return [Node\Stmt\Expression::class];
}

/**
* @param Node\Stmt\Expression $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace RectorLaravel\Tests\Rector\FuncCall\SleepFuncToSleepStaticCallRector\Fixture;

class Fixture
{
public function store()
{
sleep(5);
usleep(5);
return true;
}
}

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\FuncCall\SleepFuncToSleepStaticCallRector\Fixture;

class Fixture
{
public function store()
{
\Illuminate\Support\Sleep::sleep(5);
\Illuminate\Support\Sleep::usleep(5);
return true;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace RectorLaravel\Tests\Rector\FuncCall\SleepFuncToSleepStaticCallRector\Fixture;

class SkipAsUsingReturn
{
public function run()
{
$var = sleep(5);

if (sleep(5)) {
return true;
}

while(sleep(5)) {
return true;
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace RectorLaravel\Tests\Rector\FuncCall\SleepFuncToSleepStaticCallRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class SleepFuncToSleepStaticCallRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

use RectorLaravel\Rector\FuncCall\SleepFuncToSleepStaticCallRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');

$rectorConfig->rule(SleepFuncToSleepStaticCallRector::class);
};

0 comments on commit 29af037

Please sign in to comment.