Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Override Factory base Generator class without touching create-method #1988

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

mingalevme
Copy link

@mingalevme mingalevme commented Apr 27, 2020

Added possibility to override Factory base Generator class without rewriting the whole create-method.

In my Laravel project I have custom Generator like this:

<?php

namespace App\Faker;

/**
 * @see PictureProvider
 * @method string pictureUrl(int $width = 640, int $height = 480, $randomize = true, $gray = false)
 *
 * @see UniversityProvider
 * @method string gender()
 * @method string cyrillicFirstName(string $gender = null)
 * @method string cyrillicLastName(string $gender = null)
 * @method string facultyName()
 * @method string programName()
 * @method string groupName()
 * @method string subject()
 */
class Generator extends \Faker\Generator
{
    public function __construct()
    {
        $this->addProvider(new PictureProvider($this));
        $this->addProvider(new UniversityProvider($this));
    }
}

The class provides a single place to manage Generator providers and more over - I have IDE suggestions because of class annotations.

So to replace the base Generator-class everywhere in the project i should override Generator-instantiating (Laravel-level code):

$this->app->singleton(FakerGenerator::class, function (Application $app, array $parameters) {
    return FakerFactory::create($parameters['locale'] ?? $app['config']->get('app.faker_locale', 'en_US'));
});

But I don't want to replace the logic of setting up the generator (because it can be changed in the future):

$generator = new Generator();
foreach (static::$defaultProviders as $provider) {
    $providerClassName = self::getProviderClassname($provider, $locale);
    $generator->addProvider(new $providerClassName($generator));
}

return $generator;

So moving creating the new Generator instance in the separate protected-method do the trick:

$generator = static::createGeneratorInstance();
...
protected static function createGeneratorInstance()
{
    return new Generator();
}

What do you think?

Faker\Provider\ru_RU\Person uses suffix "a" to make a female last name from male, but used symbol was a latin "a" (ASCII fzaninotto#97) and not cyrillic "a" (ASCII fzaninotto#208), so the final last name was "some cyrillic symbols" + "latin a", now female last name forms correctly: cyrillic male last name + cyrillic "a".
@codecov-commenter
Copy link

codecov-commenter commented Sep 29, 2020

Codecov Report

Merging #1988 into master will decrease coverage by 0.12%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #1988      +/-   ##
============================================
- Coverage     56.56%   56.44%   -0.13%     
- Complexity     2068     2069       +1     
============================================
  Files           306      306              
  Lines          4849     4851       +2     
============================================
- Hits           2743     2738       -5     
- Misses         2106     2113       +7     
Impacted Files Coverage Δ Complexity Δ
src/Faker/Factory.php 95.00% <100.00%> (+0.55%) 10.00 <1.00> (+1.00)
src/Faker/Provider/ru_RU/Person.php 47.36% <100.00%> (ø) 8.00 <0.00> (ø)
src/Faker/Provider/de_AT/Person.php 0.00% <0.00%> (-100.00%) 1.00% <0.00%> (ø%)
src/Faker/Provider/hr_HR/Company.php 0.00% <0.00%> (-100.00%) 1.00% <0.00%> (ø%)
src/Faker/Provider/ne_NP/Person.php 0.00% <0.00%> (-50.00%) 2.00% <0.00%> (ø%)
src/Faker/Provider/sr_RS/Address.php 66.66% <0.00%> (-33.34%) 3.00% <0.00%> (ø%)
src/Faker/Provider/uk_UA/Company.php 28.57% <0.00%> (-28.58%) 3.00% <0.00%> (ø%)
src/Faker/Provider/fa_IR/Address.php 73.33% <0.00%> (-26.67%) 7.00% <0.00%> (ø%)
src/Faker/Provider/en_ZA/Address.php 50.00% <0.00%> (-25.00%) 4.00% <0.00%> (ø%)
src/Faker/Provider/es_AR/Address.php 50.00% <0.00%> (-25.00%) 4.00% <0.00%> (ø%)
... and 36 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5337ce5...2a78e16. Read the comment docs.

jlekowski and others added 4 commits September 29, 2020 15:29
Faker\Provider\ru_RU\Person uses suffix "a" to make a female last name from male, but used symbol was a latin "a" (ASCII fzaninotto#97) and not cyrillic "a" (ASCII fzaninotto#208), so the final last name was "some cyrillic symbols" + "latin a", now female last name forms correctly: cyrillic male last name + cyrillic "a".
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants