calebdw/fakerstan
最新稳定版本:v1.2.0
Composer 安装命令:
composer require --dev calebdw/fakerstan
包简介
PHPStan extension for Faker
README 文档
README
A PHPStan extension for Faker.
Install
composer require calebdw/fakerstan --dev
If you have the PHPStan extension installer installed then nothing more is needed, otherwise you will need to manually include the extension in the phpstan.neon(.dist) configuration file:
includes: - ./vendor/calebdw/fakerstan/extension.neon
Features
Custom Providers
FakerStan will automatically look for unknown methods on the custom providers on the Faker instance and infer the return type based on the method signature, even generics are supported:
<?php use Faker\Generator; use Faker\Provider\Base; class CustomProvider extends Base { public function customMethod(): string { return 'custom'; } /** * @template T * @param T $object * @return class-string<T> */ public function classFromObject(object $object): string { return $object::class } } $faker = new Generator(); $faker->addProvider(new CustomProvider($faker)); $faker->customMethod(); // string $faker->classFromObject(new User); // class-string<User>
Configuration
In order for FakerStan to correctly detect the custom providers, it needs the actual Faker instance used in the project (or at least an instance with the custom providers added to it).
If using Laravel, FakerStan will automatically resolve the faker instance from
the container using the fake() helper function.
If not using Laravel, but you are using a framework or environment that has a PSR-11 compliant DI container (and assuming this container is configured via a PHP file), you can use the PsrContainerFakerProviderFactory.
parameters: fakerstan: fakerProviderFactory: CalebDW\Fakerstan\PsrContainerFakerProviderFactory psrProvider: phpContainerPath: /path/to/container.php ...
The parameters that can be set are:
phpContainerPath: the path to the PHP file that configures the container.setsVariable: if the file that configures the container assigns it to a global variable, this parameter is used to indicate the name of that variable. If the container file returns the container itself, set this parameter is null (which is the default).containerFakerId: the ID that the container uses for retrieving the Faker Generator. By default, this parameter is the Generator's class name (ie.Faker\Generator).
If using Symfony, for example, you could use something like:
parameters: fakerstan: fakerProviderFactory: CalebDW\Fakerstan\PsrContainerFakerProviderFactory psrProvider: phpContainerPath: /opt/project/var/cache/dev/App_KernelDevDebugContainer.php
to use the PsrContainerFakerProviderFactory and set the path to the Symfony container. The default values for the additional parameters are correct for Symfony.
If not using Laravel or some other PSR-11 compliant container, you can specify a custom
factory class in the phpstan.neon(.dist) configuration file:
parameters: fakerstan: fakerProviderFactory: App\CustomFakerProviderFactory services: - class: App\CustomFakerProviderFactory
where App\CustomFakerProviderFactory is a class that creates an implementation of
the CalebDW\FakerStan\FakerProvider interface:
<?php use Faker\Generator; use CalebDW\FakerStan\FakerProvider; class CustomFakerProviderFactory { public static function create(): FakerProvider { return new CustomFakerProvider(); } } class CustomFakerProvider implements FakerProvider { public function getFaker(): Generator { // or from a DI container $faker = new Generator(); $faker->addProvider(new CustomProvider($faker)); return $faker; } }
Contributing
Thank you for considering contributing! You can read the contribution guide here.
License
FakerStan is open-sourced software licensed under the MIT license.
统计信息
- 总下载量: 38.59k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-19
