Completed
Push — Recipes ( c1a0bd...9ec83f )
by Laurent
24:17
created

LoadDiverseData::load()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 94

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 94
rs 7.5086
c 0
b 0
f 0
cc 6
nc 24
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * LoadDiverseData Données de configuration de l'application GLSR.
4
 *
5
 * PHP Version 5
6
 *
7
 * @author    Quétier Laurent <[email protected]>
8
 * @copyright 2014 Dev-Int GLSR
9
 * @license   http://opensource.org/licenses/gpl-license.php GNU Public License
10
 *
11
 * @version GIT: <git_id>
12
 *
13
 * @link https://github.com/Dev-Int/glsr
14
 */
15
namespace AppBundle\DataFixtures\ORM;
16
17
use Doctrine\Common\DataFixtures\AbstractFixture;
18
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
19
use Doctrine\Common\Persistence\ObjectManager;
20
use AppBundle\Entity\Settings\Diverse\FamilyLog;
21
use AppBundle\Entity\Settings\Diverse\ZoneStorage;
22
use AppBundle\Entity\Settings\Diverse\Unit;
23
use AppBundle\Entity\Settings\Diverse\Tva;
24
25
/**
26
 * LoadDiverse Data.
27
 *
28
 * @category DataFixtures
29
 */
30
class LoadDiverseData extends AbstractFixture implements OrderedFixtureInterface
31
{
32
    /**
33
     * Load data fixtures with the passed EntityManager
34
     *
35
     * @param ObjectManager $manager
36
     */
37
    public function load(ObjectManager $manager)
38
    {
39
        // Load FamilyLog
40
        $familyArray = [
41
            ['name' => "Alimentaire"],
42
            ['name' => "Non alimentaire"],
43
            ['name' => "Surgelé", 'parent' => 1],
44
            ['name' => "Frais", 'parent' => 1],
45
            ['name' => "Sec", 'parent' => 1],
46
            ['name' => "Boissons", 'parent' => 1],
47
            ['name' => "Fruits&Légumes", 'parent' => 3],
48
            ['name' => "Patisseries", 'parent' => 3],
49
            ['name' => "Viandes", 'parent' => 3],
50
            ['name' => "Fruits&Légumes", 'parent' => 4],
51
            ['name' => "Patisseries", 'parent' => 4],
52
            ['name' => "Viandes", 'parent' => 4],
53
            ['name' => "Fruits&Légumes", 'parent' => 5],
54
            ['name' => "Patisseries", 'parent' => 5],
55
            ['name' => "Bières", 'parent' => 6],
56
            ['name' => "Vins", 'parent' => 6],
57
        ];
58
        foreach ($familyArray as $key => $family) {
59
            $familyLog = new FamilyLog();
60
            $familyLog->setName($family['name']);
61
            if (isset($family['parent'])) {
62
                $parent = $familyLogs[$family['parent'] - 1];
0 ignored issues
show
Bug introduced by
The variable $familyLogs does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
63
                $familyLog->setParent($parent);
64
            }
65
            $familyLogs[$key] = $familyLog;
66
67
            $manager->persist($familyLog);
68
69
            $order = $key + 1;
70
            $this->addReference('family-log'.$order, $familyLog);
71
        }
72
73
        // Load ZoneStorage
74
        $zoneArray = [
75
            ['name' => 'Chambre négative'],
76
            ['name' => 'Chambre posistive'],
77
            ['name' => 'Réserve sèche'],
78
            ['name' => 'Réserve boissons'],
79
            ['name' => 'Armoire à boissons'],
80
            ['name' => 'Caisse'],
81
        ];
82
        foreach ($zoneArray as $key => $zone) {
83
            $zoneStorage = new ZoneStorage();
84
            $zoneStorage->setName($zone['name']);
85
86
            $manager->persist($zoneStorage);
87
88
            $order = $key + 1;
89
            $this->addReference('zoneStorage'.$order, $zoneStorage);
90
        }
91
92
        // Load Unit
93
        $unitArray = [
94
            ['name' => 'Boite', 'abbr' => 'BOI'],
95
            ['name' => 'Bouteille', 'abbr' => 'BTE'],
96
            ['name' => 'Carton', 'abbr' => 'CAR'],
97
            ['name' => 'Colis', 'abbr' => 'CLS'],
98
            ['name' => 'Kilogramme', 'abbr' => 'KG'],
99
            ['name' => 'Litre', 'abbr' => 'L'],
100
            ['name' => 'Pièce', 'abbr' => 'PIE'],
101
        ];
102
        foreach ($unitArray as $key => $unit) {
103
            $unitStorage = new Unit();
104
            $unitStorage->setName($unit['name'])
105
                ->setAbbr($unit['abbr']);
106
107
            $manager->persist($unitStorage);
108
109
            $order = $key + 1;
110
            $this->addReference('unit'.$order, $unitStorage);
111
        }
112
113
        // Load Tva
114
        $tvaArray = [
115
            ['rate' => '0.055'],
116
            ['rate' => '0.1'],
117
            ['rate' => '0.2'],
118
        ];
119
        foreach ($tvaArray as $key => $tvaRate) {
120
            $tva = new Tva();
121
            $tva->setRate($tvaRate['rate']);
0 ignored issues
show
Documentation introduced by
$tvaRate['rate'] is of type string, but the function expects a object<AppBundle\Entity\Settings\Diverse\decimal>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
122
123
            $manager->persist($tva);
124
125
            $order = $key + 1;
126
            $this->addReference('tva'.$order, $tva);
127
        }
128
129
        $manager->flush();
130
    }
131
132
    /**
133
     * Get the order of this fixture
134
     *
135
     * @return integer
136
     */
137
    public function getOrder()
138
    {
139
        return 4;
140
    }
141
}
142