Skip to content

Commit

Permalink
Add AvoidNegatedCollectionContainsOrDoesntContainRector (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia authored Jan 9, 2025
1 parent fc3c4d6 commit cf0086c
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/sets/laravel-collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use RectorLaravel\Rector\BooleanNot\AvoidNegatedCollectionContainsOrDoesntContainRector;
use RectorLaravel\Rector\MethodCall\AvoidNegatedCollectionFilterOrRejectRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../config.php');
$rectorConfig->rule(AvoidNegatedCollectionContainsOrDoesntContainRector::class);
$rectorConfig->rule(AvoidNegatedCollectionFilterOrRejectRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace RectorLaravel\Rector\BooleanNot;

use PhpParser\Node;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PHPStan\Type\ObjectType;
use RectorLaravel\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \RectorLaravel\Tests\Rector\BooleanNot\AvoidNegatedCollectionContainsOrDoesntContainRector\AvoidNegatedCollectionContainsOrDoesntContainRectorTest
*/
final class AvoidNegatedCollectionContainsOrDoesntContainRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Convert negated calls to `contains` to `doesntContain`, or vice versa.',
[
new CodeSample(
<<<'CODE_SAMPLE'
use Illuminate\Support\Collection;
$collection = new Collection([0, 1, null, -1]);
! $collection->contains(fn (?int $number): bool => is_null($number));
! $collection->doesntContain(fn (?int $number) => $number > 0);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Illuminate\Support\Collection;
$collection = new Collection([0, 1, null, -1]);
$collection->doesntContain(fn (?int $number): bool => is_null($number));
$collection->contains(fn (?int $number) => $number > 0);
CODE_SAMPLE
),
]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [BooleanNot::class];
}

/**
* @param BooleanNot $node
*/
public function refactor(Node $node): ?Node
{
return $this->updateBooleanNot($node);
}

private function updateBooleanNot(BooleanNot $booleanNot): ?MethodCall
{
$expr = $booleanNot->expr;
if (! $expr instanceof MethodCall) {
return null;
}

if (! $this->isObjectType($expr->var, new ObjectType('Illuminate\Support\Enumerable'))) {
return null;
}

$name = $expr->name;
if ($this->isName($name, 'contains')) {
$replacement = 'doesntContain';
} elseif ($this->isName($name, 'doesntContain')) {
$replacement = 'contains';
} else {
return null;
}

$expr->name = new Identifier($replacement);

return $expr;
}
}
20 changes: 20 additions & 0 deletions stubs/Illuminate/Support/Enumerable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,26 @@
*/
interface Enumerable
{
/**
* Determine if an item exists in the enumerable.
*
* @param (callable(TValue, TKey): bool)|TValue|string $key
* @param mixed $operator
* @param mixed $value
* @return bool
*/
public function contains($key, $operator = null, $value = null);

/**
* Determine if an item is not contained in the collection.
*
* @param mixed $key
* @param mixed $operator
* @param mixed $value
* @return bool
*/
public function doesntContain($key, $operator = null, $value = null);

/**
* Run a filter over each of the items.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace RectorLaravel\Tests\Rector\BooleanNot\AvoidNegatedCollectionContainsOrDoesntContainRector;

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

final class AvoidNegatedCollectionContainsOrDoesntContainRectorTest extends AbstractRectorTestCase
{
public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

/**
* @test
*/
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

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

namespace RectorLaravel\Tests\Rector\BooleanNot\AvoidNegatedCollectionContainsOrDoesntContainRector\Fixture;

use Illuminate\Support\Collection;

$collection = new Collection([0, 1, null, -1]);
$collection->contains(fn (?int $number): bool => is_null($number));
! $collection->contains(fn (?int $number): bool => is_null($number));
! $collection->contains(function (?int $number): bool {
return is_null($number);
});
! $collection->contains(1);

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\BooleanNot\AvoidNegatedCollectionContainsOrDoesntContainRector\Fixture;

use Illuminate\Support\Collection;

$collection = new Collection([0, 1, null, -1]);
$collection->contains(fn (?int $number): bool => is_null($number));
$collection->doesntContain(fn (?int $number): bool => is_null($number));
$collection->doesntContain(function (?int $number): bool {
return is_null($number);
});
$collection->doesntContain(1);

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

namespace RectorLaravel\Tests\Rector\BooleanNot\AvoidNegatedCollectionContainsOrDoesntContainRector\Fixture;

use Illuminate\Support\Collection;

$collection = new Collection([0, 1, null, -1]);
$collection->doesntContain(fn (?int $number): bool => is_null($number));
! $collection->doesntContain(fn (?int $number): bool => is_null($number));
! $collection->doesntContain(function (?int $number): bool {
return is_null($number);
});
! $collection->doesntContain(1);

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\BooleanNot\AvoidNegatedCollectionContainsOrDoesntContainRector\Fixture;

use Illuminate\Support\Collection;

$collection = new Collection([0, 1, null, -1]);
$collection->doesntContain(fn (?int $number): bool => is_null($number));
$collection->contains(fn (?int $number): bool => is_null($number));
$collection->contains(function (?int $number): bool {
return is_null($number);
});
$collection->contains(1);

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

declare(strict_types=1);

use Rector\Config\RectorConfig;
use RectorLaravel\Rector\BooleanNot\AvoidNegatedCollectionContainsOrDoesntContainRector;

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

0 comments on commit cf0086c

Please sign in to comment.