Skip to content

Commit

Permalink
EloquentWhereTypeHintClosureParameterRector refactor (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterfox authored Jan 11, 2025
1 parent 03c1396 commit 87ba7c3
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 39 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/downgraded_release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ jobs:
# copy PHP 7.2 composer
- run: cp build/composer-php-72.json composer.json

# run the tests against the downgraded rules
- run: vendor/bin/phpunit tests

# clear the dev files
- run: rm -rf build .github tests stubs phpstan.neon phpunit.xml

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private function changeClosureParamType(MethodCall|StaticCall $node): void

private function expectedObjectTypeAndMethodCall(MethodCall|StaticCall $node): bool
{
return match (true) {
$isMatchingClass = match (true) {
$node instanceof MethodCall && $this->isObjectType(
$node->var,
new ObjectType('Illuminate\Contracts\Database\Query\Builder')
Expand All @@ -123,7 +123,9 @@ private function expectedObjectTypeAndMethodCall(MethodCall|StaticCall $node): b
new ObjectType('Illuminate\Database\Eloquent\Model')
) => true,
default => false,
} && $this->isNames(
};

$isMatchingMethod = $this->isNames(
$node->name,
[
'whereHas',
Expand All @@ -136,5 +138,7 @@ private function expectedObjectTypeAndMethodCall(MethodCall|StaticCall $node): b
'orWhereDoesntHaveMorph',
]
);

return $isMatchingClass && $isMatchingMethod;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private function changeClosureParamType(MethodCall|StaticCall $node): void

private function expectedObjectTypeAndMethodCall(MethodCall|StaticCall $node): bool
{
return match (true) {
$isMatchingClass = match (true) {
$node instanceof MethodCall && $this->isObjectType(
$node->var,
new ObjectType('Illuminate\Contracts\Database\Query\Builder')
Expand All @@ -105,6 +105,10 @@ private function expectedObjectTypeAndMethodCall(MethodCall|StaticCall $node): b
new ObjectType('Illuminate\Database\Eloquent\Model')
) => true,
default => false,
} && $this->isNames($node->name, ['where', 'orWhere']);
};

$isMatchingMethod = $this->isNames($node->name, ['where', 'orWhere']);

return $isMatchingClass && $isMatchingMethod;
}
}
3 changes: 3 additions & 0 deletions stubs/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
return;
}

/**
* @method static creating(\Closure $closure)
*/
abstract class Model
{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

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

class User extends \Illuminate\Database\Eloquent\Model
{
use RectorLaravel\Tests\Rector\MethodCall\EloquentWhereRelationTypeHintingParameterRector\Source\FooModel;

}

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

Expand All @@ -17,12 +14,9 @@ User::whereHas('posts', function ($query) {

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

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

}
use RectorLaravel\Tests\Rector\MethodCall\EloquentWhereRelationTypeHintingParameterRector\Source\FooModel;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@

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

use RectorLaravel\Tests\Rector\MethodCall\EloquentWhereRelationTypeHintingParameterRector\Source\FooModel;

/** @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);
FooModel::whereHas('posts');
FooModel::whereHas('posts', null);
FooModel::whereHasMorph('posts', '');
FooModel::whereHasMorph('posts', '', null);

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

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

use RectorLaravel\Tests\Rector\MethodCall\EloquentWhereTypeHintClosureParameterRector\Source\FooModel;

FooModel::creating('foo', function ($model) {
dump($model);
});

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

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

use Illuminate\Database\Eloquent\Model;

class FooModel extends Model {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

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

class User extends \Illuminate\Database\Eloquent\Model
{
use RectorLaravel\Tests\Rector\MethodCall\EloquentWhereTypeHintClosureParameterRector\Source\FooModel;

}

User::where(function ($query) {
FooModel::where(function ($query) {
$query->where('id', 1)
->orWhere('id', 2);
});
Expand All @@ -18,12 +15,9 @@ User::where(function ($query) {

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

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

}
use RectorLaravel\Tests\Rector\MethodCall\EloquentWhereTypeHintClosureParameterRector\Source\FooModel;

User::where(function (\Illuminate\Contracts\Database\Query\Builder $query) {
FooModel::where(function (\Illuminate\Contracts\Database\Query\Builder $query) {
$query->where('id', 1)
->orWhere('id', 2);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
<?php

use RectorLaravel\Tests\Rector\MethodCall\EloquentWhereTypeHintClosureParameterRector\Source\FooModel;

/** @var \Illuminate\Contracts\Database\Query\Builder $query */
$query->where('name', 'a');

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

}

User::where('name', 'a');
FooModel::where('name', 'a');

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

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

use RectorLaravel\Tests\Rector\MethodCall\EloquentWhereTypeHintClosureParameterRector\Source\FooModel;

FooModel::creating(function ($model) {
dump($model);
});

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

namespace RectorLaravel\Tests\Rector\MethodCall\EloquentWhereTypeHintClosureParameterRector\Source;

use Illuminate\Database\Eloquent\Model;

class FooModel extends Model {}

0 comments on commit 87ba7c3

Please sign in to comment.