-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add sleep rector rule * Fix up rule for Rector 0.17
- Loading branch information
Showing
6 changed files
with
173 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
tests/Rector/FuncCall/SleepFuncToSleepStaticCallRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
?> |
21 changes: 21 additions & 0 deletions
21
tests/Rector/FuncCall/SleepFuncToSleepStaticCallRector/Fixture/skip_as_using_return.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
|
||
?> |
28 changes: 28 additions & 0 deletions
28
...Rector/FuncCall/SleepFuncToSleepStaticCallRector/SleepFuncToSleepStaticCallRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/Rector/FuncCall/SleepFuncToSleepStaticCallRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |