BrandsAwareExampleFactoryTrait::setBrandField()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Loevgaard\SyliusBrandPlugin\Fixture\Factory;
6
7
use Loevgaard\SyliusBrandPlugin\Doctrine\ORM\BrandRepositoryInterface;
8
use Loevgaard\SyliusBrandPlugin\Model\BrandAwareInterface;
9
use Loevgaard\SyliusBrandPlugin\Model\BrandInterface;
10
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption;
11
use Symfony\Component\OptionsResolver\OptionsResolver;
12
use Webmozart\Assert\Assert;
13
14
trait BrandsAwareExampleFactoryTrait
15
{
16
    /** @var BrandRepositoryInterface */
17
    protected $brandRepository;
18
19
    /** @var \Faker\Generator */
20
    protected $faker;
21
22
    public function __construct(BrandRepositoryInterface $brandRepository)
23
    {
24
        $this->brandRepository = $brandRepository;
25
        if (null === $this->faker) {
26
            $this->faker = \Faker\Factory::create();
27
        }
28
    }
29
30
    protected function configureBrandsOptions(OptionsResolver $resolver, int $amount = 10): void
31
    {
32
        $resolver
33
            ->setDefault('brands', LazyOption::randomOnes($this->brandRepository, $amount))
34
            ->setAllowedTypes('brands', ['array'])
35
            ->setNormalizer('brands', LazyOption::findBy($this->brandRepository, 'code'))
36
        ;
37
    }
38
39
    protected function setBrandField(BrandAwareInterface $brandAware, array $resolvedOptions = []): void
40
    {
41
        $brand = $this->faker->randomElement($resolvedOptions['brands']);
0 ignored issues
show
Bug introduced by
The call to Faker\Generator::randomElement() has too few arguments starting with 'b'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        /** @scrutinizer ignore-call */ 
42
        $brand = $this->faker->randomElement($resolvedOptions['brands']);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
42
        Assert::isInstanceOf($brand, BrandInterface::class);
43
44
        $brandAware->setBrand($brand);
45
    }
46
}
47