-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/livewire-computed-property
- Loading branch information
Showing
13 changed files
with
335 additions
and
13 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
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
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\MethodCall\AvoidNegatedCollectionFilterOrRejectRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../config.php'); | ||
$rectorConfig->rule(AvoidNegatedCollectionFilterOrRejectRector::class); | ||
}; |
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
111 changes: 111 additions & 0 deletions
111
src/Rector/MethodCall/AvoidNegatedCollectionFilterOrRejectRector.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,111 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RectorLaravel\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ArrowFunction; | ||
use PhpParser\Node\Expr\BooleanNot; | ||
use PhpParser\Node\Expr\Cast\Bool_; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Identifier; | ||
use PHPStan\Type\ObjectType; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \RectorLaravel\Tests\Rector\MethodCall\AvoidNegatedCollectionFilterOrRejectRector\AvoidNegatedCollectionFilterOrRejectRectorTest | ||
*/ | ||
final class AvoidNegatedCollectionFilterOrRejectRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Avoid negated conditionals in `filter()` by using `reject()`, or vice versa.', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use Illuminate\Support\Collection; | ||
$collection = new Collection([0, 1, null, -1]); | ||
$collection->filter(fn (?int $number): bool => ! is_null($number)); | ||
$collection->filter(fn (?int $number): bool => ! $number); | ||
$collection->reject(fn (?int $number) => ! $number > 0); | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
use Illuminate\Support\Collection; | ||
$collection = new Collection([0, 1, null, -1]); | ||
$collection->reject(fn (?int $number): bool => is_null($number)); // Avoid negation | ||
$collection->reject(fn (?int $number): bool => (bool) $number); // Explicitly cast | ||
$collection->filter(fn (?int $number): bool => $number > 0); // Adds return type | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
return $this->updateFilterOrRejectCall($node); | ||
} | ||
|
||
private function updateFilterOrRejectCall(MethodCall $methodCall): ?MethodCall | ||
{ | ||
if (! $this->isObjectType($methodCall->var, new ObjectType('Illuminate\Support\Enumerable'))) { | ||
return null; | ||
} | ||
|
||
if (! $this->isNames($methodCall->name, ['filter', 'reject'])) { | ||
return null; | ||
} | ||
|
||
$args = $methodCall->getArgs(); | ||
if (count($args) !== 1) { | ||
return null; | ||
} | ||
|
||
$arg = $args[0]; | ||
$argValue = $arg->value; | ||
|
||
if (! $argValue instanceof ArrowFunction) { | ||
return null; | ||
} | ||
|
||
$return = $argValue->expr; | ||
if (! $return instanceof BooleanNot) { | ||
return null; | ||
} | ||
|
||
$methodCall->name = new Identifier( | ||
$this->isName($methodCall->name, 'filter') | ||
? 'reject' | ||
: 'filter' | ||
); | ||
|
||
// Since negation implicitly casts to boolean, we need to replace it with an | ||
// explicit cast - unless the value is already a boolean. | ||
$returnExpr = $return->expr; | ||
$argValue->expr = $this->getType($returnExpr)->isBoolean()->yes() | ||
? $returnExpr | ||
: new Bool_($returnExpr); | ||
|
||
$argValue->returnType = new Identifier('bool'); | ||
|
||
return $methodCall; | ||
} | ||
} |
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
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Illuminate\Support; | ||
|
||
if (class_exists('Illuminate\Support\Collection')) { | ||
return; | ||
} | ||
|
||
/** | ||
* @template TKey of array-key | ||
* | ||
* @template-covariant TValue | ||
* | ||
* @implements \Illuminate\Support\Enumerable<TKey, TValue> | ||
*/ | ||
class Collection implements Enumerable | ||
{ | ||
} |
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
...dNegatedCollectionFilterOrRejectRector/AvoidNegatedCollectionFilterOrRejectRectorTest.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\MethodCall\AvoidNegatedCollectionFilterOrRejectRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class AvoidNegatedCollectionFilterOrRejectRectorTest 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'; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...Rector/MethodCall/AvoidNegatedCollectionFilterOrRejectRector/Fixture/fixture_cast.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,27 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\MethodCall\AvoidNegatedCollectionFilterOrRejectRector\Fixture; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
(new Collection([0, 1, null, -1])) | ||
->filter(fn (?int $number): bool => ! $number); | ||
|
||
(new Collection([0, 1, null, -1])) | ||
->reject(fn (?int $number): bool => ! $number); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\MethodCall\AvoidNegatedCollectionFilterOrRejectRector\Fixture; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
(new Collection([0, 1, null, -1])) | ||
->reject(fn (?int $number): bool => (bool) $number); | ||
|
||
(new Collection([0, 1, null, -1])) | ||
->filter(fn (?int $number): bool => (bool) $number); | ||
|
||
?> |
27 changes: 27 additions & 0 deletions
27
...MethodCall/AvoidNegatedCollectionFilterOrRejectRector/Fixture/fixture_return_type.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,27 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\MethodCall\AvoidNegatedCollectionFilterOrRejectRector\Fixture; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
(new Collection([0, 1, null, -1])) | ||
->filter(fn (?int $number) => ! is_null($number)); | ||
|
||
(new Collection([0, 1, null, -1])) | ||
->reject(fn (?int $number) => ! is_null($number)); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\MethodCall\AvoidNegatedCollectionFilterOrRejectRector\Fixture; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
(new Collection([0, 1, null, -1])) | ||
->reject(fn (?int $number): bool => is_null($number)); | ||
|
||
(new Collection([0, 1, null, -1])) | ||
->filter(fn (?int $number): bool => is_null($number)); | ||
|
||
?> |
Oops, something went wrong.