Skip to content

RequestStaticValidateToInjectRector incorrectly rewrites FormRequest::failOnUnknownFields() to $request->failOnUnknownFields() #496

@ManuelLeiner

Description

@ManuelLeiner

Problem

RequestStaticValidateToInjectRector appears to be too broad.

It currently rewrites a static call on Illuminate\Foundation\Http\FormRequest into an instance call on an injected Illuminate\Http\Request, even though failOnUnknownFields() is a static API on FormRequest and does not exist on the base Request class.

This produces invalid code.

Minimal reproduction

Rector config

<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use RectorLaravel\Rector\StaticCall\RequestStaticValidateToInjectRector;

return RectorConfig::configure()
    ->withPaths([__DIR__ . '/app'])
    ->withRules([
        RequestStaticValidateToInjectRector::class,
    ]);

Input

<?php

declare(strict_types=1);

namespace App\Providers;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\ServiceProvider;

final class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        FormRequest::failOnUnknownFields();
    }
}

Actual output

<?php

declare(strict_types=1);

namespace App\Providers;

use Illuminate\Http\Request;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\ServiceProvider;

final class AppServiceProvider extends ServiceProvider
{
    public function boot(Request $request): void
    {
        $request->failOnUnknownFields();
    }
}

Expected output

No change.

FormRequest::failOnUnknownFields() is a valid static call and should remain untouched.

Why this is a bug

failOnUnknownFields() exists on Illuminate\Foundation\Http\FormRequest, not on Illuminate\Http\Request.

So the transformation changes:

  • a valid static class-level configuration call
  • into an instance method call on the wrong type

which makes the result invalid.

Notes

The rule name and docs suggest it is intended for Request::validate() style rewrites, but the current implementation seems to rewrite arbitrary static calls on Request-like types.

Environment

  • rector/rector: 2.4.1
  • driftingly/rector-laravel: 2.3.0
  • laravel/framework: 13.4.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions