Completed
Push — Recipes ( 630f49...c8afb0 )
by Laurent
12:15 queued 03:48
created

LoadSupplierData::load()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 8.829
c 0
b 0
f 0
cc 2
nc 2
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
 * LoadSupplierData Datas of the suppliers of the GLSR application.
4
 *
5
 * PHP Version 7
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
 * @see https://github.com/Dev-Int/glsr
14
 */
15
16
namespace App\DataFixtures;
17
18
use App\Entity\Settings\Supplier;
19
use Doctrine\Common\Persistence\ObjectManager;
20
use Doctrine\Bundle\FixturesBundle\Fixture;
21
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
22
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
23
24
/**
25
 * Load Supplier Data.
26
 *
27
 * @category DataFixtures
28
 */
29
class LoadSupplierData extends Fixture implements DependentFixtureInterface, FixtureGroupInterface
30
{
31
    /**
32
     * Load data fixtures with the passed EntityManager.
33
     *
34
     * @param ObjectManager $manager
35
     */
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'])
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class App\Entity\Contact as the method setFamilyLog() does only exist in the following sub-classes of App\Entity\Contact: App\Entity\Settings\Supplier. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
79
                ->setAddress($data['address'])
80
                ->setZipCode($data['zipCode'])
81
                ->setTown($data['town'])
82
                ->setPhone($data['phone'])
0 ignored issues
show
Documentation introduced by
$data['phone'] is of type object<libphonenumber\PhoneNumber>, but the function expects a object<App\Entity\phone_number>.

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...
83
                ->setFax($data['fax'])
0 ignored issues
show
Documentation introduced by
$data['fax'] is of type object<libphonenumber\PhoneNumber>, but the function expects a object<App\Entity\phone_number>.

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...
84
                ->setMail($data['mail'])
85
                ->setContact($data['contact'])
86
                ->setGsm($data['gsm'])
0 ignored issues
show
Documentation introduced by
$data['gsm'] is of type object<libphonenumber\PhoneNumber>, but the function expects a object<App\Entity\phone_number>.

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...
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
99
    public function getDependencies()
100
    {
101
        return [LoadDiverseData::class];
102
    }
103
104
    public static function getGroups(): array
105
    {
106
        return ['settingsGroup'];
107
    }
108
}
109