Conditions | 6 |
Paths | 24 |
Total Lines | 94 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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]; |
||
|
|||
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']); |
||
122 | |||
123 | $manager->persist($tva); |
||
124 | |||
125 | $order = $key + 1; |
||
126 | $this->addReference('tva'.$order, $tva); |
||
127 | } |
||
128 | |||
129 | $manager->flush(); |
||
130 | } |
||
131 | |||
142 |
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:
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
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: