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 ( 28563d...300a48 )
by Théo
110:15 queued 75:14
created

DoctrineOrmLoader::__construct()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 21
nc 3
nop 5
1
<?php
2
3
/*
4
 * This file is part of the Hautelook\AliceBundle package.
5
 *
6
 * (c) Baldur Rensch <[email protected]>
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 Hautelook\AliceBundle\Loader;
13
14
use Doctrine\DBAL\Sharding\PoolingShardConnection;
15
use Doctrine\ORM\EntityManagerInterface;
16
use Fidry\AliceDataFixtures\Bridge\Doctrine\Persister\ObjectManagerPersister;
17
use Fidry\AliceDataFixtures\Bridge\Doctrine\Purger\Purger;
18
use Fidry\AliceDataFixtures\Loader\FileResolverLoader;
19
use Fidry\AliceDataFixtures\Loader\PurgerLoader;
20
use Fidry\AliceDataFixtures\LoaderInterface;
21
use Fidry\AliceDataFixtures\Persistence\PersisterAwareInterface;
22
use Fidry\AliceDataFixtures\Persistence\PurgeMode;
23
use Fidry\AliceDataFixtures\Persistence\Purger\NullPurger;
24
use Hautelook\AliceBundle\BundleResolverInterface;
25
use Hautelook\AliceBundle\FixtureLocatorInterface;
26
use Hautelook\AliceBundle\LoaderInterface as AliceBundleLoaderInterface;
27
use Hautelook\AliceBundle\LoggerAwareInterface;
28
use Hautelook\AliceBundle\Resolver\File\KernelFileResolver;
29
use InvalidArgumentException;
30
use LogicException;
31
use Nelmio\Alice\IsAServiceTrait;
32
use Psr\Log\LoggerInterface;
33
use Psr\Log\NullLogger;
34
use Symfony\Bundle\FrameworkBundle\Console\Application;
35
use Symfony\Component\HttpKernel\KernelInterface;
36
37
final class DoctrineOrmLoader implements AliceBundleLoaderInterface, LoggerAwareInterface
38
{
39
    use IsAServiceTrait;
40
41
    private $bundleResolver;
42
    private $fixtureLocator;
43
    /** @var LoaderInterface|PersisterAwareInterface */
44
    private $purgeLoader;
45
    /** @var LoaderInterface|PersisterAwareInterface */
46
    private $appendLoader;
47
    private $logger;
48
49
    public function __construct(
50
        BundleResolverInterface $bundleResolver,
51
        FixtureLocatorInterface $fixtureLocator,
52
        LoaderInterface $purgeLoader,
53
        LoaderInterface $appendLoader,
54
        LoggerInterface $logger = null
55
    ) {
56
        $this->bundleResolver = $bundleResolver;
57
        $this->fixtureLocator = $fixtureLocator;
58
59
        if (false === $purgeLoader instanceof PersisterAwareInterface) {
60
            throw new InvalidArgumentException(
61
                sprintf(
62
                    'Expected loader to be an instance of "%s".',
63
                    PersisterAwareInterface::class
64
                )
65
            );
66
        }
67
68
        if (false === $appendLoader instanceof PersisterAwareInterface) {
69
            throw new InvalidArgumentException(
70
                sprintf(
71
                    'Expected loader to be an instance of "%s".',
72
                    PersisterAwareInterface::class
73
                )
74
            );
75
        }
76
77
        $this->purgeLoader = $purgeLoader;
78
        $this->appendLoader = $purgeLoader;
79
        $this->logger = $logger ?? new NullLogger();
80
    }
81
82
    /**
83
     * @inheritdoc
84
     */
85
    public function withLogger(LoggerInterface $logger): self
86
    {
87
        return new self($this->bundleResolver, $this->fixtureLocator, $this->purgeLoader, $logger);
0 ignored issues
show
Bug introduced by
It seems like $this->purgeLoader 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...
88
    }
89
90
    /**
91
     * @inheritdoc
92
     */
93
    public function load(
94
        Application $application,
95
        EntityManagerInterface $manager,
96
        array $bundles,
97
        string $environment,
98
        bool $append,
99
        bool $purgeWithTruncate,
100
        string $shard = null
101
    ) {
102
        $bundles = $this->bundleResolver->resolveBundles($application, $bundles);
103
        $fixtureFiles = $this->fixtureLocator->locateFiles($bundles, $environment);
104
105
        $this->logger->info('fixtures found', ['files' => $fixtureFiles]);
106
107
        if (null !== $shard) {
108
            $this->connectToShardConnection($manager, $shard);
109
        }
110
111
        $fixtures = $this->loadFixtures(
112
            $this->purgeLoader,
0 ignored issues
show
Bug introduced by
It seems like $this->purgeLoader 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...
113
            $manager,
114
            $fixtureFiles,
115
            $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...
116
            $append,
117
            $purgeWithTruncate
118
        );
119
120
        $this->logger->info('fixtures loaded');
121
122
        return $fixtures;
123
    }
124
125
    private function connectToShardConnection(EntityManagerInterface $manager, string $shard)
126
    {
127
        $connection = $manager->getConnection();
128
        if ($connection instanceof PoolingShardConnection) {
129
            $connection->connect($shard);
130
131
            return;
132
        }
133
134
        throw new InvalidArgumentException(
135
            sprintf(
136
                'Could not establish a shard connection for the shard "%s". The connection must be an instance'
137
                .' of "%s", got "%s" instead.',
138
                $shard,
139
                PoolingShardConnection::class,
140
                get_class($connection)
141
            )
142
        );
143
    }
144
145
    /**
146
     * @param LoaderInterface|PersisterAwareInterface $loader
147
     * @param EntityManagerInterface                  $manager
148
     * @param string[]                                $files
149
     * @param array                                   $parameters
150
     * @param bool                                    $append
151
     * @param bool|null                               $purgeWithTruncate
152
     *
153
     * @return object[]
154
     */
155
    private function loadFixtures(
156
        LoaderInterface $loader,
0 ignored issues
show
Unused Code introduced by
The parameter $loader is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
157
        EntityManagerInterface $manager,
158
        array $files,
159
        array $parameters,
160
        bool $append,
161
        bool $purgeWithTruncate
162
    ) {
163
        if ($append && $purgeWithTruncate) {
164
            throw new LogicException(
165
                'Cannot append loaded fixtures and at the same time purge the database. Choose one.'
166
            );
167
        }
168
169
        $persister = new ObjectManagerPersister($manager);
170
171
        if (true === $append) {
172
            $loader = $this->appendLoader->withPersister($persister);
0 ignored issues
show
Bug introduced by
The method withPersister does only exist in Fidry\AliceDataFixtures\...PersisterAwareInterface, but not in Fidry\AliceDataFixtures\LoaderInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
173
174
            return $loader->load($files, $parameters);
175
        }
176
177
        $loader = $this->purgeLoader->withPersister($persister);
178
179
        $purgeMode = (true === $purgeWithTruncate)
180
            ? PurgeMode::createTruncateMode()
181
            : PurgeMode::createDeleteMode()
182
        ;
183
184
        return $loader->load($files, $parameters, [], $purgeMode);
185
    }
186
}
187