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

LoadAppData   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 5
dl 0
loc 47
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 29 1
A getOrder() 0 4 1
1
<?php
2
/**
3
 * LoadAppData Données 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
21
use libphonenumber\PhoneNumber;
22
use AppBundle\Entity\Settings\Company;
23
use AppBundle\Entity\Settings\Settings;
24
25
/**
26
 * LoadApp Data.
27
 *
28
 * @category DataFixtures
29
 */
30
class LoadAppData 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
        $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
40
41
        // Load Company
42
        $app = new Company();
43
        $app->setName('Dev-Int')
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class AppBundle\Entity\Contact as the method setStatus() does only exist in the following sub-classes of AppBundle\Entity\Contact: AppBundle\Entity\Settings\Company. 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...
44
            ->setStatus('S.A.R.L.')
45
            ->setAddress('2, rue de la chèvre')
46
            ->setZipCode('75000')
47
            ->setTown('Paris')
48
            ->setPhone($phoneUtil->parse('0140000000', "FR"))
49
            ->setFax($phoneUtil->parse('0140000000', "FR"))
50
            ->setMail('[email protected]')
51
            ->setContact('Ursule')
52
            ->setGsm($phoneUtil->parse('0640000000', "FR"));
53
54
        $manager->persist($app);
55
56
        // Load Settings
57
        $settings = new Settings();
58
        $settings->setInventoryStyle("zonestorage")
59
            ->setCalculation("weighted")
60
            ->setCurrency("EUR");
61
62
        $manager->persist($settings);
63
64
        $manager->flush();
65
    }
66
67
    /**
68
     * Get the order of this fixture
69
     *
70
     * @return integer
71
     */
72
    public function getOrder()
73
    {
74
        return 3;
75
    }
76
}