Completed
Branch index_loading_feature (a31c33)
by Alessandro
02:52
created

LoadFixturesCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 61
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
C execute() 0 33 7
A loadFixture() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\MongoDbBundle\Command;
6
7
use Facile\MongoDbBundle\Fixtures\MongoFixturesLoader;
8
use Facile\MongoDbBundle\Fixtures\MongoFixtureInterface;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
/**
13
 * Class LoadFixturesCommand.
14
 */
15
class LoadFixturesCommand extends AbstractCommand
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20 5
    protected function configure()
21
    {
22 5
        parent::configure();
23
        $this
24 5
            ->setName('mongodb:fixtures:load')
25 5
            ->setDescription('Load fixtures and applies them');
26
        ;
27 5
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function execute(InputInterface $input, OutputInterface $output)
33
    {
34
        $this->io->writeln('Loading index fixtures');
35
36
        $paths = array();
37
        foreach ($this->getApplication()->getKernel()->getBundles() as $bundle) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Application as the method getKernel() does only exist in the following sub-classes of Symfony\Component\Console\Application: Symfony\Bundle\FrameworkBundle\Console\Application. 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...
38
            $paths[] = $bundle->getPath().'/DataFixtures/Mongo';
39
        }
40
41
        $loader = new MongoFixturesLoader($this->getContainer());
42
43
        foreach ($paths as $path) {
44
            if (is_dir($path)) {
45
                $loader->loadFromDirectory($path);
46
            }
47
            if (is_file($path)) {
48
                $loader->loadFromFile($path);
49
            }
50
        }
51
52
        $fixtures = $loader->getLoadedClasses();
53
        if (!$fixtures) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fixtures of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
54
            throw new \InvalidArgumentException(
55
                sprintf('Could not find any class to load in: %s', "\n\n- ".implode("\n- ", $paths))
56
            );
57
        }
58
59
        foreach ($fixtures as $fixture){
60
            $this->loadFixture($fixture);
61
        }
62
63
        $this->io->writeln('Done.');
64
    }
65
66
    /**
67
     * @param MongoFixtureInterface $indexList
68
     */
69
    private function loadFixture(MongoFixtureInterface $indexList)
70
    {
71
        $indexList->loadData();
72
        $indexList->loadIndexes();
73
        $this->io->writeln('Loaded fixture: '. get_class($indexList));
74
    }
75
}
76