Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Fakerstan Laravel Package

calebdw/fakerstan

View on GitHub
Deep Wiki
Context7

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.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity