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

LoadAppData   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 29 1
A getGroups() 0 4 1
1
<?php
2
/**
3
 * LoadAppData GLSR application data.
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\Company;
19
use App\Entity\Settings\Settings;
20
use Doctrine\Common\Persistence\ObjectManager;
21
use Doctrine\Bundle\FixturesBundle\Fixture;
22
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
23
24
/**
25
 * LoadApp Data.
26
 *
27
 * @category DataFixtures
28
 */
29
class LoadAppData extends Fixture implements 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
        $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
39
40
        // Load Company
41
        $app = new Company();
42
        $app->setName('Dev-Int Création')
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 setStatus() does only exist in the following sub-classes of App\Entity\Contact: App\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...
43
            ->setStatus('S.A.R.L.')
44
            ->setAddress('2, rue de la chèvre')
45
            ->setZipCode('75000')
46
            ->setTown('Paris')
47
            ->setPhone($phoneUtil->parse('0140000000', 'FR'))
48
            ->setFax($phoneUtil->parse('0140000000', 'FR'))
49
            ->setMail('[email protected]')
50
            ->setContact('Ursule')
51
            ->setGsm($phoneUtil->parse('0640000000', 'FR'));
52
53
        $manager->persist($app);
54
55
        // Load Settings
56
        $settings = new Settings();
57
        $settings->setInventoryStyle('zonestorage')
58
            ->setCalculation('weighted')
59
            ->setCurrency('EUR');
60
61
        $manager->persist($settings);
62
63
        $manager->flush();
64
    }
65
66
    public static function getGroups(): array
67
    {
68
        return ['settingsGroup'];
69
    }
70
}
71