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

Adds EloquentWhereRelationTypeHintingParameterRector rule #138

Merged
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
32 changes: 26 additions & 6 deletions docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 40 Rules Overview
# 41 Rules Overview

## AddArgumentDefaultValueRector

Expand Down Expand Up @@ -460,15 +460,35 @@ Convert DB Expression `__toString()` calls to `getValue()` method calls.

## EloquentMagicMethodToQueryBuilderRector

Transform certain magic method calls on Eloquent Models into corresponding Query Builder method calls.
The EloquentMagicMethodToQueryBuilderRule is designed to automatically transform certain magic method calls on Eloquent Models into corresponding Query Builder method calls.

- class: [`RectorLaravel\Rector\StaticCall\EloquentMagicMethodToQueryBuilderRector`](../src/Rector/StaticCall/EloquentMagicMethodToQueryBuilderRector.php)

```diff
-User::find(1);
-User::where('email', 'test@test.com')->first();
+User::query()->find(1);
+User::query()->where('email', 'test@test.com')->first();
use App\Models\User;

-$user = User::find(1);
+$user = User::query()->find(1);
```

<br>

## EloquentWhereRelationTypeHintingParameterRector

Add type hinting to where relation has methods e.g. `whereHas`, `orWhereHas`, `whereDoesntHave`, `orWhereDoesntHave`, `whereHasMorph`, `orWhereHasMorph`, `whereDoesntHaveMorph`, `orWhereDoesntHaveMorph`

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

```diff
-User::whereHas('posts', function ($query) {
+User::whereHas('posts', function (\Illuminate\Contracts\Database\Query\Builder $query) {
$query->where('is_published', true);
});

-$query->whereHas('posts', function ($query) {
+$query->whereHas('posts', function (\Illuminate\Contracts\Database\Query\Builder $query) {
$query->where('is_published', true);
});
```

<br>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

declare(strict_types=1);

namespace RectorLaravel\Rector\MethodCall;

use PhpParser\Node;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \RectorLaravel\Tests\Rector\MethodCall\EloquentWhereRelationTypeHintingParameterRector\EloquentWhereRelationTypeHintingParameterRectorTest
*/
class EloquentWhereRelationTypeHintingParameterRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Add type hinting to where relation has methods e.g. `whereHas`, `orWhereHas`, `whereDoesntHave`, `orWhereDoesntHave`, `whereHasMorph`, `orWhereHasMorph`, `whereDoesntHaveMorph`, `orWhereDoesntHaveMorph`',
[
new CodeSample(
<<<'CODE_SAMPLE'
User::whereHas('posts', function ($query) {
$query->where('is_published', true);
});

$query->whereHas('posts', function ($query) {
$query->where('is_published', true);
});
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
User::whereHas('posts', function (\Illuminate\Contracts\Database\Query\Builder $query) {
$query->where('is_published', true);
});

$query->whereHas('posts', function (\Illuminate\Contracts\Database\Query\Builder $query) {
$query->where('is_published', true);
});
CODE_SAMPLE
),
]
);
}

public function getNodeTypes(): array
{
return [Node\Expr\MethodCall::class, Node\Expr\StaticCall::class];
}

public function refactor(Node $node): ?Node
{
if (! $node instanceof Node\Expr\MethodCall && ! $node instanceof Node\Expr\StaticCall) {
return null;
}

if ($this->isWhereRelationMethodWithClosureOrArrowFunction($node)) {
$this->changeClosureParamType($node);

return $node;
}

return null;
}

private function isWhereRelationMethodWithClosureOrArrowFunction(
Node\Expr\MethodCall|Node\Expr\StaticCall $node
): bool {
if (! $this->expectedObjectTypeAndMethodCall($node)) {
return false;
}

// Morph methods have the closure in the 3rd position, others use the 2nd.
$position = $this->isNames(
$node->name,
['whereHasMorph', 'orWhereHasMorph', 'whereDoesntHaveMorph', 'orWhereDoesntHaveMorph']
) ? 2 : 1;

if (
! ($node->getArgs()[$position]->value ?? null) instanceof Node\Expr\Closure &&
! ($node->getArgs()[$position]->value ?? null) instanceof Node\Expr\ArrowFunction
) {
return false;
}

return true;
}

private function changeClosureParamType(Node\Expr\MethodCall|Node\Expr\StaticCall $node): void
{
// Morph methods have the closure in the 3rd position, others use the 2nd.
$position = $this->isNames(
$node->name,
['whereHasMorph', 'orWhereHasMorph', 'whereDoesntHaveMorph', 'orWhereDoesntHaveMorph']
) ? 2 : 1;

/** @var Node\Expr\ArrowFunction|Node\Expr\Closure $closure */
$closure = $node->getArgs()[$position]
->value;

if (! isset($closure->getParams()[0])) {
return;
}

$param = $closure->getParams()[0];

if ($param->type instanceof Node\Name) {
return;
}

$param->type = new Node\Name\FullyQualified('Illuminate\Contracts\Database\Query\Builder');
}

private function expectedObjectTypeAndMethodCall(Node\Expr\MethodCall|Node\Expr\StaticCall $node): bool
{
return match (true) {
$node instanceof Node\Expr\MethodCall && $this->isObjectType(
$node->var,
new ObjectType('Illuminate\Contracts\Database\Query\Builder')
) => true,
$node instanceof Node\Expr\StaticCall && $this->isObjectType(
$node->class,
new ObjectType('Illuminate\Database\Eloquent\Model')
) => true,
default => false,
} && $this->isNames(
$node->name,
[
'whereHas',
'orWhereHas',
'whereDoesntHave',
'orWhereDoesntHave',
'whereHasMorph',
'orWhereHasMorph',
'whereDoesntHaveMorph',
'orWhereDoesntHaveMorph',
]
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace RectorLaravel\Tests\Rector\MethodCall\EloquentWhereRelationTypeHintingParameterRector;

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

final class EloquentWhereRelationTypeHintingParameterRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

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

namespace RectorLaravel\Tests\Rector\MethodCall\EloquentWhereRelationTypeHintingParameterRector\Fixture;

/** @var \Illuminate\Contracts\Database\Query\Builder $query */
$query->whereHas('posts', function ($query) {
$query->where('is_published', true);
});

$query->orWhereHas('posts', function ($query) {
$query->where('is_published', true);
});

$query->whereDoesntHave('posts', function ($query) {
$query->where('is_published', true);
});

$query->orWhereDoesntHave('posts', function ($query) {
$query->where('is_published', true);
});

$query->whereHasMorph('posts', [], function ($query) {
$query->where('is_published', true);
});

$query->orWhereHasMorph('posts', [], function ($query) {
$query->where('is_published', true);
});

$query->whereDoesntHaveMorph('posts', [], function ($query) {
$query->where('is_published', true);
});

$query->orWhereDoesntHaveMorph('posts', [], function ($query) {
$query->where('is_published', true);
});

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\MethodCall\EloquentWhereRelationTypeHintingParameterRector\Fixture;

/** @var \Illuminate\Contracts\Database\Query\Builder $query */
$query->whereHas('posts', function (\Illuminate\Contracts\Database\Query\Builder $query) {
$query->where('is_published', true);
});

$query->orWhereHas('posts', function (\Illuminate\Contracts\Database\Query\Builder $query) {
$query->where('is_published', true);
});

$query->whereDoesntHave('posts', function (\Illuminate\Contracts\Database\Query\Builder $query) {
$query->where('is_published', true);
});

$query->orWhereDoesntHave('posts', function (\Illuminate\Contracts\Database\Query\Builder $query) {
$query->where('is_published', true);
});

$query->whereHasMorph('posts', [], function (\Illuminate\Contracts\Database\Query\Builder $query) {
$query->where('is_published', true);
});

$query->orWhereHasMorph('posts', [], function (\Illuminate\Contracts\Database\Query\Builder $query) {
$query->where('is_published', true);
});

$query->whereDoesntHaveMorph('posts', [], function (\Illuminate\Contracts\Database\Query\Builder $query) {
$query->where('is_published', true);
});

$query->orWhereDoesntHaveMorph('posts', [], function (\Illuminate\Contracts\Database\Query\Builder $query) {
$query->where('is_published', true);
});

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

namespace RectorLaravel\Tests\Rector\MethodCall\EloquentWhereRelationTypeHintingParameterRector\Fixture;

/** @var \Illuminate\Contracts\Database\Query\Builder $query */
$query->whereHas('posts', fn ($query) =>
$query->where('is_published', true)
);

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\MethodCall\EloquentWhereRelationTypeHintingParameterRector\Fixture;

/** @var \Illuminate\Contracts\Database\Query\Builder $query */
$query->whereHas('posts', fn (\Illuminate\Contracts\Database\Query\Builder $query) =>
$query->where('is_published', true)
);

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

namespace RectorLaravel\Tests\Rector\MethodCall\EloquentWhereRelationTypeHintingParameterRector\Fixture;

class User extends \Illuminate\Database\Eloquent\Model
{

}

User::whereHas('posts', function ($query) {
$query->where('is_published', true);
});

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\MethodCall\EloquentWhereRelationTypeHintingParameterRector\Fixture;

class User extends \Illuminate\Database\Eloquent\Model
{

}

User::whereHas('posts', function (\Illuminate\Contracts\Database\Query\Builder $query) {
$query->where('is_published', true);
});

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

namespace RectorLaravel\Tests\Rector\MethodCall\EloquentWhereRelationTypeHintingParameterRector\Fixture;

/** @var \Illuminate\Contracts\Database\Query\Builder $query */
$query->whereHas('posts');
$query->whereHas('posts', null);
$query->whereHasMorph('posts', '', null);

class User extends \Illuminate\Database\Eloquent\Model
{

}

User::whereHas('posts');
User::whereHas('posts', null);
User::whereHasMorph('posts', '');
User::whereHasMorph('posts', '', null);

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

namespace RectorLaravel\Tests\Rector\MethodCall\EloquentWhereRelationTypeHintingParameterRector\Fixture;

$obj->whereHas('posts', fn ($query) =>
$query->where('is_published', true)
);

RandomClass::whereHas('posts', fn ($query) =>
$query->where('is_published', true)
);

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

declare(strict_types=1);

use Rector\Config\RectorConfig;
use RectorLaravel\Rector\MethodCall\EloquentWhereRelationTypeHintingParameterRector;

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

$rectorConfig->rule(EloquentWhereRelationTypeHintingParameterRector::class);
};