Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UnaliasCollectionMethodsRector #291

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"scripts": {
"phpstan": "vendor/bin/phpstan analyse --ansi",
"test": "vendor/bin/phpunit tests",
"lint": "vendor/bin/duster lint",
"fix": "vendor/bin/duster fix",
"rector-dry-run": "vendor/bin/rector process --dry-run --ansi",
Expand Down
2 changes: 2 additions & 0 deletions config/sets/laravel-collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use Rector\Config\RectorConfig;
use RectorLaravel\Rector\BooleanNot\AvoidNegatedCollectionContainsOrDoesntContainRector;
use RectorLaravel\Rector\MethodCall\AvoidNegatedCollectionFilterOrRejectRector;
use RectorLaravel\Rector\MethodCall\UnaliasCollectionMethodsRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../config.php');
$rectorConfig->rule(AvoidNegatedCollectionContainsOrDoesntContainRector::class);
$rectorConfig->rule(AvoidNegatedCollectionFilterOrRejectRector::class);
$rectorConfig->rule(UnaliasCollectionMethodsRector::class);
};
20 changes: 19 additions & 1 deletion docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 76 Rules Overview
# 77 Rules Overview

## AbortIfRector

Expand Down Expand Up @@ -1445,6 +1445,24 @@ Automatically type hints your tappable closures

<br>

## UnaliasCollectionMethodsRector

Use the base collection methods instead of their aliases.

- class: [`RectorLaravel\Rector\MethodCall\UnaliasCollectionMethodsRector`](../src/Rector/MethodCall/UnaliasCollectionMethodsRector.php)

```diff
use Illuminate\Support\Collection;

$collection = new Collection([0, 1, null, -1]);
-$collection->average();
-$collection->some(fn (?int $number): bool => is_null($number));
+$collection->avg();
+$collection->contains(fn (?int $number): bool => is_null($number));
```

<br>

## UnifyModelDatesWithCastsRector

Unify Model `$dates` property with `$casts`
Expand Down
81 changes: 81 additions & 0 deletions src/Rector/MethodCall/UnaliasCollectionMethodsRector.php
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;
}
}
26 changes: 26 additions & 0 deletions stubs/Illuminate/Support/Enumerable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,32 @@
*/
interface Enumerable
{
/**
* Alias for the "avg" method.
*
* @param (callable(TValue): float|int)|string|null $callback
* @return float|int|null
*/
public function average($callback = null);

/**
* Get the average value of a given key.
*
* @param (callable(TValue): float|int)|string|null $callback
* @return float|int|null
*/
public function avg($callback = null);

/**
* Alias for the "contains" method.
*
* @param (callable(TValue, TKey): bool)|TValue|string $key
* @param mixed $operator
* @param mixed $value
* @return bool
*/
public function some($key, $operator = null, $value = null);

/**
* Determine if an item exists in the enumerable.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@

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

$rectorConfig->rule(AvoidNegatedCollectionFilterOrRejectRector::class);
};
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));

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