jbboehr/phpstan-laravel-validation

Laravel Validation extension for PHPStan

Installs: 8 959

Dependents: 0

Suggesters: 0

Security: 0

Stars: 4

Watchers: 2

Forks: 1

Open Issues: 10

Type:phpstan-extension

dev-master 2024-09-28 02:39 UTC

README

ci License: AGPL v3+ stability-experimental

Explanation

If the rules given to a laravel validator are a constant expression, then the shape of the array returned by \Illuminate\Validation\Validator::validated() is known at compile-time, and can be statically analyzed.

$request = new \Illuminate\Http\Request();

$data = \Illuminate\Support\Facades\Validator::make($request->all(), [
    'person.*.email' => 'required|email|unique:users',
    'person.*.first_name' => 'required|string',
    'person.*.age' => 'required|integer|string',
])->validated();

\PHPStan\dumpType($data);
// array{person: array<int|string, array{email: non-empty-string, first_name: string, age: numeric-string}>}

$data = $request->validate([
    'person.*.email' => 'required|email|unique:users',
    'person.*.first_name' => 'required|string',
    'person.*.age' => 'required|integer|string',
]);

\PHPStan\dumpType($data);
// array{person: array<int|string, array{email: non-empty-string, first_name: string, age: numeric-string}>}

If the input data does not match the rules array, an \Illuminate\Validation\ValidationException is thrown, thus preserving type safety.

Installation

To use this extension, require it in Composer:

composer require --dev jbboehr/phpstan-laravel-validation

If you also install phpstan/extension-installer then you're all set!

Manual installation

If you don't want to use phpstan/extension-installer, include extension.neon in your project's PHPStan config:

includes:
    - vendor/jbboehr/phpstan-laravel-validation/extension.neon

Caveats

  • Laravel's validation does not cast anything, so, for example, numeric produces the type union int|float|numeric-string. If you know it will always be a string, you can refine the type by using numeric|string and get a plain numeric-string.
  • Wildcards must be indexed by integer and can't be mixed with non-wildcard rules.
  • Custom validation rules, implicit rules, and enums are not currently supported.

License

This project is licensed under the AGPL v3+ License - see the LICENSE.md file for details.