-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AvoidNegatedCollectionContainsOrDoesntContainRector (#288)
- Loading branch information
Showing
7 changed files
with
213 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
87 changes: 87 additions & 0 deletions
87
src/Rector/BooleanNot/AvoidNegatedCollectionContainsOrDoesntContainRector.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,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; | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
...ContainsOrDoesntContainRector/AvoidNegatedCollectionContainsOrDoesntContainRectorTest.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,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'; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...nNot/AvoidNegatedCollectionContainsOrDoesntContainRector/Fixture/fixture_contains.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\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); | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...AvoidNegatedCollectionContainsOrDoesntContainRector/Fixture/fixture_doesntContain.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\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); | ||
|
||
?> |
11 changes: 11 additions & 0 deletions
11
...BooleanNot/AvoidNegatedCollectionContainsOrDoesntContainRector/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; | ||
use RectorLaravel\Rector\BooleanNot\AvoidNegatedCollectionContainsOrDoesntContainRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php'); | ||
$rectorConfig->rule(AvoidNegatedCollectionContainsOrDoesntContainRector::class); | ||
}; |