-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
194 additions
and
2 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
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,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RectorLaravel\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
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\MethodCall\UnaliasCollectionMethodsRector\UnaliasCollectionMethodsRectorTest | ||
*/ | ||
final class UnaliasCollectionMethodsRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Use the base collection methods instead of their aliases.', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use Illuminate\Support\Collection; | ||
$collection = new Collection([0, 1, null, -1]); | ||
$collection->average(); | ||
$collection->some(fn (?int $number): bool => is_null($number)); | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
use Illuminate\Support\Collection; | ||
$collection = new Collection([0, 1, null, -1]); | ||
$collection->avg(); | ||
$collection->contains(fn (?int $number): bool => is_null($number)); | ||
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->updateMethodCall($node); | ||
} | ||
|
||
private function updateMethodCall(MethodCall $methodCall): ?MethodCall | ||
{ | ||
if (! $this->isObjectType($methodCall->var, new ObjectType('Illuminate\Support\Enumerable'))) { | ||
return null; | ||
} | ||
|
||
$name = $methodCall->name; | ||
if ($this->isName($name, 'some')) { | ||
$replacement = 'contains'; | ||
} elseif ($this->isName($name, 'average')) { | ||
$replacement = 'avg'; | ||
} else { | ||
return null; | ||
} | ||
|
||
$methodCall->name = new Identifier($replacement); | ||
|
||
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
23 changes: 23 additions & 0 deletions
23
tests/Rector/MethodCall/UnaliasCollectionMethodsRector/Fixture/fixture.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,23 @@ | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\MethodCall\UnaliasCollectionMethodsRector\Fixture; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
$collection = new Collection([0, 1, null, -1]); | ||
$collection->average(); | ||
$collection->some(fn (?int $number): bool => is_null($number)); | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace RectorLaravel\Tests\Rector\MethodCall\UnaliasCollectionMethodsRector\Fixture; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
$collection = new Collection([0, 1, null, -1]); | ||
$collection->avg(); | ||
$collection->contains(fn (?int $number): bool => is_null($number)); | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...s/Rector/MethodCall/UnaliasCollectionMethodsRector/UnaliasCollectionMethodsRectorTest.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\UnaliasCollectionMethodsRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class UnaliasCollectionMethodsRectorTest 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'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/Rector/MethodCall/UnaliasCollectionMethodsRector/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\MethodCall\UnaliasCollectionMethodsRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php'); | ||
$rectorConfig->rule(UnaliasCollectionMethodsRector::class); | ||
}; |