BaseFixture::getRandomInstance()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the vseth-semesterly-reports project.
5
 *
6
 * (c) Florian Moser <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace App\DataFixtures\Base;
13
14
use Doctrine\Bundle\FixturesBundle\Fixture;
15
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
16
use Doctrine\Persistence\ObjectManager;
17
use Faker\Factory;
18
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
19
use Symfony\Component\DependencyInjection\ContainerInterface;
20
21
abstract class BaseFixture extends Fixture implements OrderedFixtureInterface, ContainerAwareInterface
22
{
23
    /* @var ContainerInterface $container */
24
    private $container;
25
26
    public function setContainer(ContainerInterface $container = null)
27
    {
28
        $this->container = $container;
29
    }
30
31
    protected function getTranslator()
32
    {
33
        return $this->container->get('translator');
34
    }
35
36
    /**
37
     * @return \Faker\Generator
38
     */
39
    protected function getFaker()
40
    {
41
        return Factory::create('de_CH');
42
    }
43
44
    /**
45
     * create random instances.
46
     *
47
     * @param $count
48
     *
49
     * @throws \Exception
50
     */
51
    protected function loadSomeRandoms(ObjectManager $manager, $count = 5)
52
    {
53
        $instances = [];
54
        for ($i = 0; $i < $count; ++$i) {
55
            $instance = $this->getRandomInstance();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $instance is correct as $this->getRandomInstance() targeting App\DataFixtures\Base\Ba...re::getRandomInstance() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
56
            if ($instance === null) {
57
                throw new \Exception('you need to override getRandomInstance to return an instance before you can use this function');
58
            }
59
60
            $instances[] = $instance;
61
            $manager->persist($instance);
62
        }
63
64
        return $instances;
65
    }
66
67
    /**
68
     * create an instance with all random values.
69
     *
70
     * @return mixed
71
     */
72
    protected function getRandomInstance()
73
    {
74
        return null;
75
    }
76
}
77