-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Swaps use of negative blank/filled with alternating helper * Add Docs
- Loading branch information
Showing
5 changed files
with
149 additions
and
0 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
62 changes: 62 additions & 0 deletions
62
src/Rector/FuncCall/NotFilledBlankFuncCallToBlankFilledFuncCallRector.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,62 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Rector\FuncCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\BooleanNot; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \RectorLaravel\Tests\Rector\FuncCall\NotFilledBlankFuncCallToBlankFilledFuncCallRector\NotFilledBlankFuncCallToBlankFilledFuncCallRectorTest | ||
*/ | ||
class NotFilledBlankFuncCallToBlankFilledFuncCallRector extends AbstractRector | ||
{ | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Swap the use of NotBooleans used with filled() and blank() to the correct helper.', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
!filled([]); | ||
!blank([]); | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
blank([]); | ||
filled([]); | ||
CODE_SAMPLE | ||
), | ||
|
||
] | ||
); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [BooleanNot::class]; | ||
} | ||
|
||
/** | ||
* @param BooleanNot $node | ||
*/ | ||
public function refactor(Node $node): ?Node\Expr\FuncCall | ||
{ | ||
if (! $node->expr instanceof Node\Expr\FuncCall) { | ||
return null; | ||
} | ||
|
||
if ( | ||
! $this->isName($node->expr->name, 'filled') && | ||
! $this->isName($node->expr->name, 'blank') | ||
) { | ||
return null; | ||
} | ||
|
||
$method = $this->isName($node->expr->name, 'filled') ? 'blank' : 'filled'; | ||
return $this->nodeFactory->createFuncCall($method, $node->expr->args); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...Rector/FuncCall/NotFilledBlankFuncCallToBlankFilledFuncCallRector/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,29 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\FuncCall\NotFilledBlankFuncCallToBlankFilledFuncCallRector\Fixture; | ||
|
||
class Fixture | ||
{ | ||
public function run() | ||
{ | ||
!filled([]); | ||
!blank([]); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\FuncCall\NotFilledBlankFuncCallToBlankFilledFuncCallRector\Fixture; | ||
|
||
class Fixture | ||
{ | ||
public function run() | ||
{ | ||
blank([]); | ||
filled([]); | ||
} | ||
} | ||
|
||
?> |
28 changes: 28 additions & 0 deletions
28
...CallToBlankFilledFuncCallRector/NotFilledBlankFuncCallToBlankFilledFuncCallRectorTest.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\NotFilledBlankFuncCallToBlankFilledFuncCallRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class NotFilledBlankFuncCallToBlankFilledFuncCallRectorTest 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'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...tor/FuncCall/NotFilledBlankFuncCallToBlankFilledFuncCallRector/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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php'); | ||
|
||
$rectorConfig->rule(\RectorLaravel\Rector\FuncCall\NotFilledBlankFuncCallToBlankFilledFuncCallRector::class); | ||
}; |