Skip to content

RenameVariableToMatchMethodCallReturnTypeRector rename a variable only partialy #9872

Description

@jeff1326

Rector 2.6.4
PHP 8.5.6 (cli) (built: May 14 2026 16:01:51) (NTS)
Composer version 2.10.0 2026-05-28 11:22:08

Variable is renamed only at one place and forget where it is used.

Minimal PHP Code Causing Issue

app/Http/Controllers/Settings/PartMappingApiController.php

<?php

namespace App\Http\Controllers\Settings;

use App\Http\Controllers\Concerns\PaginatesJsonResponse;
use App\Models\PartMapping;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller;
use Spatie\QueryBuilder\AllowedSort;
use Spatie\QueryBuilder\QueryBuilder;

class PartMappingApiController extends Controller
{
    use AuthorizesRequests;
    use PaginatesJsonResponse;

    public function index(): JsonResponse
    {
        $query = QueryBuilder::for(PartMapping::class)
            ->allowedSorts(
                // The part_type sort keeps the legacy source_part_number secondary ordering so a
                // type groups its rows the same way it did before.
                AllowedSort::callback('part_type', fn(Builder $query, bool $descending): Builder => $query
                    ->orderBy('part_type', $descending ? 'desc' : 'asc')
                    ->orderBy('source_part_number')),
            )
            ->defaultSort('part_type');

        return $this->apiPaginatedResponse($query->jsonPaginate());
    }
}

rector.php

<?php

use Rector\CodeQuality\Rector\If_\ObjectExplicitBoolCompareRector;
use Rector\CodingStyle\Rector\PostInc\PostIncDecToPreIncDecRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector;
use Rector\Naming\Rector\ClassMethod\RenameParamToMatchTypeRector;
use Rector\PHPUnit\CodeQuality\Rector\StmtsAwareInterface\DeclareStrictTypesTestsRector;
use Rector\TypeDeclaration\Rector\StmtsAwareInterface\DeclareStrictTypesRector;
use Rector\TypeDeclaration\Rector\StmtsAwareInterface\SafeDeclareStrictTypesRector;

return RectorConfig::configure()
    ->withPaths([
        __DIR__ . '/app',
    ])
    ->withPhpSets()
    ->withPreparedSets(
        deadCode: true,
        codeQuality: true,
        codingStyle: true,
        typeDeclarations: true,
        typeDeclarationDocblocks: true,
        privatization: true,
        naming: true,
        instanceOf: true,
        if: true,
        earlyReturn: true,
        carbon: true,
        rectorPreset: true,
        phpunitCodeQuality: true,
        phpunitNarrowAsserts: true,
        phpunitMockToStub: true
    )
    ->withSkip([
        SafeDeclareStrictTypesRector::class,
        RenameParamToMatchTypeRector::class, // Some variable are named to explain their purpose. ex: `canDelegateTo(User $candidate): bool`
        ObjectExplicitBoolCompareRector::class,
        PostIncDecToPreIncDecRector::class, // I dont see the plus value
        DeclareStrictTypesRector::class,
        DeclareStrictTypesTestsRector::class,
        RemoveUnusedVariableAssignRector::class,
    ]);

Weirdly, the demo gives // no change
https://getrector.com/demo/9d2db485-3248-47a3-8f38-474f1eff02bf

Result

2) app/Http/Controllers/Settings/PartMappingApiController.php:21

    ---------- begin diff ----------
@@ Line 21 @@
     {
         $this->authorize('viewAny', PartMapping::class);

-        $query = QueryBuilder::for(PartMapping::class)
+        $queryBuilder = QueryBuilder::for(PartMapping::class)
             ->allowedFilters(
                 AllowedFilter::groupOr('search', [AllowedFilter::partial('source_part_number'), AllowedFilter::partial('destination_part_number'), AllowedFilter::partial('destination_manufacturer'), AllowedFilter::partial('notes')]),
                 AllowedFilter::exact('part_type'),
    ----------- end diff -----------

Applied rules:
 * RenameVariableToMatchMethodCallReturnTypeRector

Expected Behaviour

$queryBuilder = QueryBuilder::for(PartMapping::class) like proposed but changing this line return $this->apiPaginatedResponse($query->jsonPaginate()); to return $this->apiPaginatedResponse($queryBuilder->jsonPaginate()); too

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions