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
Push — master ( be0626...df8c18 )
by Théo
33:01
created

DoctrineOrmLoader::load()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 23
nc 2
nop 7
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\FileResolverLoader;
10
use Fidry\AliceDataFixtures\Loader\PurgerLoader;
11
use Fidry\AliceDataFixtures\LoaderInterface;
12
use Fidry\AliceDataFixtures\Persistence\PersisterAwareInterface;
13
use Fidry\AliceDataFixtures\Persistence\PurgeMode;
14
use Hautelook\AliceBundle\BundleResolverInterface;
15
use Hautelook\AliceBundle\FixtureLocatorInterface;
16
use Hautelook\AliceBundle\LoaderInterface as AliceBundleLoaderInterface;
17
use Hautelook\AliceBundle\LoggerAwareInterface;
18
use Hautelook\AliceBundle\Resolver\File\KernelFileResolver;
19
use Nelmio\Alice\NotClonableTrait;
20
use Psr\Log\LoggerInterface;
21
use Symfony\Bundle\FrameworkBundle\Console\Application;
22
use Symfony\Component\HttpKernel\KernelInterface;
23
24
final class DoctrineOrmLoader implements AliceBundleLoaderInterface, LoggerAwareInterface
25
{
26
    use NotClonableTrait;
27
28
    /**
29
     * @var BundleResolverInterface
30
     */
31
    private $bundleResolver;
32
33
    /**
34
     * @var FixtureLocatorInterface
35
     */
36
    private $fixtureLocator;
37
38
    /**
39
     * @var LoaderInterface|PersisterAwareInterface
40
     */
41
    private $loader;
42
43
    /**
44
     * @var LoggerInterface
45
     */
46
    private $logger;
47
48
    public function __construct(
49
        BundleResolverInterface $bundleResolver,
50
        FixtureLocatorInterface $fixtureLocator,
51
        LoaderInterface $loader,
52
        LoggerInterface $logger
53
    ) {
54
        $this->bundleResolver = $bundleResolver;
55
        $this->fixtureLocator = $fixtureLocator;
56
        if (false === $loader instanceof PersisterAwareInterface) {
57
            throw new \InvalidArgumentException(
58
                sprintf(
59
                    'Expected loader to be an instance of "%s".',
60
                    PersisterAwareInterface::class
61
                )
62
            );
63
        }
64
        $this->loader = $loader;
65
        $this->logger = $logger;
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function withLogger(LoggerInterface $logger): self
72
    {
73
        return new self($this->bundleResolver, $this->fixtureLocator, $this->loader, $logger);
0 ignored issues
show
Bug introduced by
It seems like $this->loader can also be of type object<Fidry\AliceDataFi...ersisterAwareInterface>; however, Hautelook\AliceBundle\Lo...rmLoader::__construct() does only seem to accept object<Fidry\AliceDataFixtures\LoaderInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    public function load(
80
        Application $application,
81
        EntityManagerInterface $manager,
82
        array $bundles,
83
        string $environment,
84
        bool $append,
85
        bool $purgeWithTruncate,
86
        string $shard = null
87
    ) {
88
        $bundles = $this->bundleResolver->resolveBundles($application, $bundles);
89
        $fixtureFiles = $this->fixtureLocator->locateFiles($bundles, $environment);
90
91
        $this->logger->info('fixtures found', ['files' => $fixtureFiles]);
92
93
        if (null !== $shard) {
94
            $this->connectToShardConnection($manager, $shard);
95
        }
96
97
        $fixtures = $this->loadFixtures(
98
            $this->loader,
0 ignored issues
show
Bug introduced by
It seems like $this->loader can also be of type object<Fidry\AliceDataFi...ersisterAwareInterface>; however, Hautelook\AliceBundle\Lo...mLoader::loadFixtures() does only seem to accept object<Fidry\AliceDataFixtures\LoaderInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
99
            $application->getKernel(),
100
            $manager,
101
            $fixtureFiles,
102
            $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...
103
            $append,
104
            $purgeWithTruncate
105
        );
106
107
        $this->logger->info('fixtures loaded');
108
109
        return $fixtures;
110
    }
111
112
    private function connectToShardConnection(EntityManagerInterface $manager, string $shard)
113
    {
114
        $connection = $manager->getConnection();
115
        if ($connection instanceof PoolingShardConnection) {
116
            $connection->connect($shard);
117
118
            return;
119
        }
120
121
        throw new \InvalidArgumentException(
122
            sprintf(
123
                'Could not establish a shard connection for the shard "%s". The connection must be an instance'
124
                .' of "%s", got "%s" instead.',
125
                $shard,
126
                PoolingShardConnection::class,
127
                get_class($connection)
128
            )
129
        );
130
    }
131
132
    /**
133
     * @param LoaderInterface|PersisterAwareInterface $loader
134
     * @param KernelInterface                         $kernel
135
     * @param EntityManagerInterface                  $manager
136
     * @param string[]                                $files
137
     * @param array                                   $parameters
138
     * @param bool                                    $append
139
     * @param bool|null                               $purgeWithTruncate
140
     *
141
     * @return \object[]
142
     */
143
    private function loadFixtures(
144
        LoaderInterface $loader,
145
        KernelInterface $kernel,
146
        EntityManagerInterface $manager,
147
        array $files,
148
        array $parameters,
149
        bool $append,
150
        bool $purgeWithTruncate = null
151
    ) {
152
        if ($append && $purgeWithTruncate !== null) {
153
            throw new \LogicException(
154
                'Cannot append loaded fixtures and at the same time purge the database. Choose one.'
155
            );
156
        }
157
158
        $loader = $loader->withPersister(new ObjectManagerPersister($manager));
159
        if (true === $append) {
160
            return $loader->load($files, $parameters);
161
        }
162
163
        $purgeMode = (true === $purgeWithTruncate)
164
            ? PurgeMode::createTruncateMode()
165
            : PurgeMode::createDeleteMode()
166
        ;
167
168
        $purger = new OrmPurger($manager, $purgeMode);
169
        $loader = new PurgerLoader($loader, $purger, $purger);
0 ignored issues
show
Unused Code introduced by
The call to PurgerLoader::__construct() has too many arguments starting with $purger.

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...
170
        $loader = new FileResolverLoader($loader, new KernelFileResolver($kernel));
171
172
        return $loader->load($files, $parameters);
173
    }
174
}
175