Completed
Push — master ( ab8175...d69783 )
by Kamil
15s
created

MongoDBPurgerListener::configureOptionsNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\FixturesBundle\Listener;
13
14
use Doctrine\Common\DataFixtures\Purger\MongoDBPurger;
15
use Doctrine\Common\Persistence\ManagerRegistry;
16
use Doctrine\ODM\MongoDB\DocumentManager;
17
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
18
19
/**
20
 * @author Kamil Kokot <[email protected]>
21
 */
22
final class MongoDBPurgerListener extends AbstractListener implements BeforeSuiteListenerInterface
23
{
24
    /**
25
     * @var ManagerRegistry
26
     */
27
    private $managerRegistry;
28
29
    /**
30
     * @param ManagerRegistry $managerRegistry
31
     */
32
    public function __construct(ManagerRegistry $managerRegistry)
33
    {
34
        $this->managerRegistry = $managerRegistry;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function beforeSuite(SuiteEvent $suiteEvent, array $options)
41
    {
42
        foreach ($options['managers'] as $managerName) {
43
            /** @var DocumentManager $manager */
44
            $manager = $this->managerRegistry->getManager($managerName);
45
46
            $purger = new MongoDBPurger($manager);
47
            $purger->purge();
48
        }
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getName()
55
    {
56
        return 'mongodb_purger';
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    protected function configureOptionsNode(ArrayNodeDefinition $optionsNode)
63
    {
64
        $optionsNode
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method prototype() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\ArrayNodeDefinition. 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...
65
            ->children()
66
                ->arrayNode('managers')
67
                    ->defaultValue([null])
68
                    ->prototype('scalar')
69
        ;
70
    }
71
}
72