GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#249)
by Théo
34:01
created

DoctrineOrmLoader::loadFixtures()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.439
c 0
b 0
f 0
cc 6
eloc 19
nc 6
nop 6
1
<?php
2
3
namespace Hautelook\AliceBundle\Loader;
4
5
use Doctrine\DBAL\Sharding\PoolingShardConnection;
6
use Doctrine\ORM\EntityManagerInterface;
7
use Fidry\AliceDataFixtures\Bridge\Doctrine\Persister\ObjectManagerPersister;
8
use Fidry\AliceDataFixtures\Bridge\Doctrine\Purger\OrmPurger;
9
use Fidry\AliceDataFixtures\Loader\PurgerLoader;
10
use Fidry\AliceDataFixtures\LoaderInterface;
11
use Fidry\AliceDataFixtures\Persistence\PersisterAwareInterface;
12
use Hautelook\AliceBundle\BundleResolverInterface;
13
use Hautelook\AliceBundle\FixtureLocatorInterface;
14
use Hautelook\AliceBundle\LoaderInterface as AliceBundleLoaderInterface;
15
use Hautelook\AliceBundle\LoggerAwareInterface;
16
use Nelmio\Alice\NotClonableTrait;
17
use Psr\Log\LoggerInterface;
18
use Symfony\Bundle\FrameworkBundle\Console\Application;
19
20
final class DoctrineOrmLoader implements AliceBundleLoaderInterface, LoggerAwareInterface
21
{
22
    use NotClonableTrait;
23
24
    /**
25
     * @var BundleResolverInterface
26
     */
27
    private $bundleResolver;
28
29
    /**
30
     * @var FixtureLocatorInterface
31
     */
32
    private $fixtureLocator;
33
34
    /**
35
     * @var LoaderInterface|PersisterAwareInterface
36
     */
37
    private $loader;
38
39
    /**
40
     * @var LoggerInterface
41
     */
42
    private $logger;
43
44
    public function __construct(
45
        BundleResolverInterface $bundleResolver,
46
        FixtureLocatorInterface $fixtureLocator,
47
        LoaderInterface $loader,
48
        LoggerInterface $logger
49
    ) {
50
        $this->bundleResolver = $bundleResolver;
51
        $this->fixtureLocator = $fixtureLocator;
52
        if (false === $loader instanceof PersisterAwareInterface) {
0 ignored issues
show
Bug introduced by
The class Fidry\AliceDataFixtures\...PersisterAwareInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
53
            throw new \InvalidArgumentException(
54
                'Expected loader to be an instance of "%s".',
55
                PersisterAwareInterface::class
56
            );
57
        }
58
        $this->loader = $loader;
59
        $this->logger = $logger;
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function withLogger(LoggerInterface $logger): self
66
    {
67
        return new self($this->bundleResolver, $this->fixtureLocator, $this->loader, $logger);
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public function load(
74
        Application $application,
75
        EntityManagerInterface $manager,
76
        array $bundles,
77
        bool $append,
78
        string $environment = null,
79
        string $shard = null,
80
        bool $purgeWithTruncate = null
81
    ) {
82
        $bundles = $this->bundleResolver->resolveBundles($application, $bundles);
83
        $fixtureFiles = $this->fixtureLocator->locate($bundles, $environment);
84
85
        $this->logger->info(sprintf('  <comment>></comment> <info>%s</info>', 'fixtures found:'));
86
        foreach ($fixtureFiles as $fixture) {
87
            $this->logger->info(sprintf(sprintf('      <comment>-</comment> <info>%s</info>', $fixture)));
88
        }
89
90
        if ('' !== $shard) {
91
            $this->connectToShardConnection($manager, $shard);
92
        }
93
94
        $fixtures = $this->loadFixtures(
95
            $this->loader,
96
            $manager,
97
            $fixtureFiles,
98
            $application->getKernel()->getContainer()->getParameterBag()->all(),
0 ignored issues
show
Bug introduced by
The method getParameterBag() does not exist on Symfony\Component\Depend...tion\ContainerInterface. Did you maybe mean getParameter()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
99
            $append,
100
            $purgeWithTruncate
101
        );
102
103
        $this->logger->info(sprintf('  <comment>></comment> <info>%s</info>', 'fixtures loaded'));
104
105
        return $fixtures;
106
    }
107
108
    private function connectToShardConnection(EntityManagerInterface $manager, string $shard)
109
    {
110
        $connection = $manager->getConnection();
111
        if ($connection instanceof PoolingShardConnection) {
112
            $connection->connect($shard);
113
114
            return;
115
        }
116
117
        throw new \InvalidArgumentException(
118
            sprintf(
119
                'Could not establish a shard connection for the shard "%s". The connection must be an instance'
120
                .' of "%s", got "%s" instead.',
121
                $shard,
122
                PoolingShardConnection::class,
123
                get_class($connection)
124
            )
125
        );
126
    }
127
128
    /**
129
     * @param LoaderInterface|PersisterAwareInterface $loader
130
     * @param EntityManagerInterface                  $manager
131
     * @param string[]                                $files
132
     * @param array                                   $parameters
133
     * @param bool                                    $append
134
     * @param bool|null                               $purgeWithTruncate
135
     *
136
     * @return object[]
137
     */
138
    private function loadFixtures(
139
        LoaderInterface $loader,
140
        EntityManagerInterface $manager,
141
        array $files,
142
        array $parameters,
143
        bool $append,
144
        bool $purgeWithTruncate = null
145
    ) {
146
        if ($append && $purgeWithTruncate !== null) {
147
            throw new \LogicException(
148
                'Cannot append loaded fixtures and at the same time purge the database. Choose one.'
149
            );
150
        }
151
152
        $loader = $loader->withPersister(new ObjectManagerPersister($manager));
0 ignored issues
show
Documentation introduced by
$manager is of type object<Doctrine\ORM\EntityManagerInterface>, but the function expects a object<Doctrine\Common\P...stence\ManagerRegistry>.

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...
153
154
        if ($append) {
155
            return $loader->load($files, $parameters);
156
        }
157
158
        $purgeMode = (null === $purgeWithTruncate || false === $purgeWithTruncate)
159
            ? \Doctrine\Common\DataFixtures\Purger\ORMPurger::PURGE_MODE_DELETE
160
            : \Doctrine\Common\DataFixtures\Purger\ORMPurger::PURGE_MODE_TRUNCATE
161
        ;
162
        $purger = new OrmPurger($manager, $purgeMode);
0 ignored issues
show
Unused Code introduced by
The call to OrmPurger::__construct() has too many arguments starting with $purgeMode.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
163
        $loader = new PurgerLoader($loader, $purger, $purger);
164
165
        return $loader->load($files, $parameters);
166
    }
167
}
168