Skip to content

Commit

Permalink
Change use of !blank() and !filled() to use the correct func (#113)
Browse files Browse the repository at this point in the history
* Swaps use of negative blank/filled with alternating helper

* Add Docs
  • Loading branch information
peterfox authored Jun 3, 2023
1 parent 355abcf commit 701a2a6
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
19 changes: 19 additions & 0 deletions docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,25 @@ Change minutes argument to seconds in `Illuminate\Contracts\Cache\Store` and Ill

<br>

## NotFilledBlankFuncCallToBlankFilledFuncCallRector

Change `!blank()` func calls to `filled()` func calls and vice versa.

- class: [`RectorLaravel\Rector\FuncCall\NotFilledBlankFuncCallToBlankFilledFuncCallRector`](../src/Rector/FuncCall/NotFilledBlankFuncCallToBlankFilledFuncCallRector.php)

```diff
class SomeClass
{
public function run()
{
- return !blank($value);
+ return filled($value);
}
}
```

<br>

## NowFuncWithStartOfDayMethodCallToTodayFuncRector

Changes the user of `now()->startOfDay()` to be replaced with `today()`.
Expand Down
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);
}
}
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([]);
}
}

?>
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';
}
}
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);
};

0 comments on commit 701a2a6

Please sign in to comment.