jbboehr/phpstan-laravel-validation 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jbboehr/phpstan-laravel-validation

Composer 安装命令:

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

包简介

Laravel Validation extension for PHPStan

README 文档

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.

统计信息

  • 总下载量: 9.57k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: AGPL-3.0-or-later
  • 更新时间: 2022-12-29