Skip to content

Commit 701a2a6

Browse files
authored
Change use of !blank() and !filled() to use the correct func (#113)
* Swaps use of negative blank/filled with alternating helper * Add Docs
1 parent 355abcf commit 701a2a6

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed

docs/rector_rules_overview.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,25 @@ Change minutes argument to seconds in `Illuminate\Contracts\Cache\Store` and Ill
403403

404404
<br>
405405

406+
## NotFilledBlankFuncCallToBlankFilledFuncCallRector
407+
408+
Change `!blank()` func calls to `filled()` func calls and vice versa.
409+
410+
- class: [`RectorLaravel\Rector\FuncCall\NotFilledBlankFuncCallToBlankFilledFuncCallRector`](../src/Rector/FuncCall/NotFilledBlankFuncCallToBlankFilledFuncCallRector.php)
411+
412+
```diff
413+
class SomeClass
414+
{
415+
public function run()
416+
{
417+
- return !blank($value);
418+
+ return filled($value);
419+
}
420+
}
421+
```
422+
423+
<br>
424+
406425
## NowFuncWithStartOfDayMethodCallToTodayFuncRector
407426

408427
Changes the user of `now()->startOfDay()` to be replaced with `today()`.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace RectorLaravel\Rector\FuncCall;
4+
5+
use PhpParser\Node;
6+
use PhpParser\Node\Expr\BooleanNot;
7+
use Rector\Core\Rector\AbstractRector;
8+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
9+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
10+
11+
/**
12+
* @see \RectorLaravel\Tests\Rector\FuncCall\NotFilledBlankFuncCallToBlankFilledFuncCallRector\NotFilledBlankFuncCallToBlankFilledFuncCallRectorTest
13+
*/
14+
class NotFilledBlankFuncCallToBlankFilledFuncCallRector extends AbstractRector
15+
{
16+
17+
public function getRuleDefinition(): RuleDefinition
18+
{
19+
return new RuleDefinition(
20+
'Swap the use of NotBooleans used with filled() and blank() to the correct helper.',
21+
[
22+
new CodeSample(
23+
<<<'CODE_SAMPLE'
24+
!filled([]);
25+
!blank([]);
26+
CODE_SAMPLE
27+
,
28+
<<<'CODE_SAMPLE'
29+
blank([]);
30+
filled([]);
31+
CODE_SAMPLE
32+
),
33+
34+
]
35+
);
36+
}
37+
38+
public function getNodeTypes(): array
39+
{
40+
return [BooleanNot::class];
41+
}
42+
43+
/**
44+
* @param BooleanNot $node
45+
*/
46+
public function refactor(Node $node): ?Node\Expr\FuncCall
47+
{
48+
if (! $node->expr instanceof Node\Expr\FuncCall) {
49+
return null;
50+
}
51+
52+
if (
53+
! $this->isName($node->expr->name, 'filled') &&
54+
! $this->isName($node->expr->name, 'blank')
55+
) {
56+
return null;
57+
}
58+
59+
$method = $this->isName($node->expr->name, 'filled') ? 'blank' : 'filled';
60+
return $this->nodeFactory->createFuncCall($method, $node->expr->args);
61+
}
62+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\FuncCall\NotFilledBlankFuncCallToBlankFilledFuncCallRector\Fixture;
4+
5+
class Fixture
6+
{
7+
public function run()
8+
{
9+
!filled([]);
10+
!blank([]);
11+
}
12+
}
13+
14+
?>
15+
-----
16+
<?php
17+
18+
namespace RectorLaravel\Tests\Rector\FuncCall\NotFilledBlankFuncCallToBlankFilledFuncCallRector\Fixture;
19+
20+
class Fixture
21+
{
22+
public function run()
23+
{
24+
blank([]);
25+
filled([]);
26+
}
27+
}
28+
29+
?>
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\NotFilledBlankFuncCallToBlankFilledFuncCallRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class NotFilledBlankFuncCallToBlankFilledFuncCallRectorTest 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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
7+
return static function (RectorConfig $rectorConfig): void {
8+
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');
9+
10+
$rectorConfig->rule(\RectorLaravel\Rector\FuncCall\NotFilledBlankFuncCallToBlankFilledFuncCallRector::class);
11+
};

0 commit comments

Comments
 (0)