Conditions | 2 |
Paths | 2 |
Total Lines | 62 |
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 |
||
36 | public function load(ObjectManager $manager) |
||
37 | { |
||
38 | // Serialization of phone numbers |
||
39 | $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance(); |
||
40 | // References of logistics families |
||
41 | $surgele = $this->getReference('family-log3'); |
||
42 | $frais = $this->getReference('family-log4'); |
||
43 | $sec = $this->getReference('family-log5'); |
||
44 | $boissons = $this->getReference('family-log6'); |
||
45 | |||
46 | // Datas of suppliers |
||
47 | $datas = [ |
||
48 | ['name' => 'Davigel', 'address' => '12, rue du gel', 'zipCode' => 75001, |
||
49 | 'town' => 'Paris', 'phone' => $phoneUtil->parse('0140000001', 'FR'), |
||
50 | 'fax' => $phoneUtil->parse('0140000001', 'FR'), |
||
51 | 'mail' => '[email protected]', 'contact' => 'David Gel', |
||
52 | 'gsm' => $phoneUtil->parse('0160000001', 'FR'), 'family' => $surgele, |
||
53 | 'delayDeliv' => 2, 'orderDate' => [2, 5], ], |
||
54 | ['name' => 'Davifrais', 'address' => '12, rue du frais', |
||
55 | 'zipCode' => 75001, 'town' => 'Paris', |
||
56 | 'phone' => $phoneUtil->parse('0140000002', 'FR'), |
||
57 | 'fax' => $phoneUtil->parse('0140000002', 'FR'), |
||
58 | 'mail' => '[email protected]', 'contact' => 'David Frais', |
||
59 | 'gsm' => $phoneUtil->parse('0160000002', 'FR'), 'family' => $frais, |
||
60 | 'delayDeliv' => 2, 'orderDate' => [2, 5], ], |
||
61 | ['name' => 'Davisec', 'address' => '12, rue du sec', 'zipCode' => 75001, |
||
62 | 'town' => 'Paris', 'phone' => $phoneUtil->parse('0140000003', 'FR'), |
||
63 | 'fax' => $phoneUtil->parse('0140000003', 'FR'), |
||
64 | 'mail' => '[email protected]', 'contact' => 'David Sec', |
||
65 | 'gsm' => $phoneUtil->parse('0160000003', 'FR'), 'family' => $sec, |
||
66 | 'delayDeliv' => 3, 'orderDate' => [3], ], |
||
67 | ['name' => 'Loire Boissons', 'address' => '12, rue de la soif', |
||
68 | 'zipCode' => 75001, 'town' => 'Paris', |
||
69 | 'phone' => $phoneUtil->parse('0140000004', 'FR'), |
||
70 | 'fax' => $phoneUtil->parse('0140000004', 'FR'), |
||
71 | 'mail' => '[email protected]', 'contact' => 'David Sec', |
||
72 | 'gsm' => $phoneUtil->parse('0160000004', 'FR'), 'family' => $boissons, |
||
73 | 'delayDeliv' => 3, 'orderDate' => [5], ], |
||
74 | ]; |
||
75 | |||
76 | foreach ($datas as $key => $data) { |
||
77 | $supplier = new Supplier(); |
||
78 | $supplier->setName($data['name']) |
||
|
|||
79 | ->setAddress($data['address']) |
||
80 | ->setZipCode($data['zipCode']) |
||
81 | ->setTown($data['town']) |
||
82 | ->setPhone($data['phone']) |
||
83 | ->setFax($data['fax']) |
||
84 | ->setMail($data['mail']) |
||
85 | ->setContact($data['contact']) |
||
86 | ->setGsm($data['gsm']) |
||
87 | ->setFamilyLog($data['family']) |
||
88 | ->setDelaydeliv($data['delayDeliv']) |
||
89 | ->setOrderdate($data['orderDate']); |
||
90 | |||
91 | $manager->persist($supplier); |
||
92 | $order = $key + 1; |
||
93 | $this->addReference('supplier'.$order, $supplier); |
||
94 | } |
||
95 | |||
96 | $manager->flush(); |
||
97 | } |
||
98 | |||
109 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: