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 (#198)
by Vincent
33:06
created

ORMExecutor::purge()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 15
ccs 2
cts 2
cp 1
rs 9.2
cc 4
eloc 9
nc 8
nop 0
crap 4
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\Doctrine\DataFixtures\Executor;
13
14
use Doctrine\Common\DataFixtures\Executor\ORMExecutor as DoctrineORMExecutor;
15
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
16
use Doctrine\DBAL\Platforms\MySqlPlatform;
17
use Doctrine\ORM\EntityManagerInterface;
18
use Hautelook\AliceBundle\Alice\DataFixtures\LoaderInterface;
19
20
/**
21
 * Class responsible for executing data fixtures.
22
 *
23
 * @author Théo FIDRY <[email protected]>
24
 */
25
class ORMExecutor extends DoctrineORMExecutor implements ExecutorInterface
26
{
27
    use ExecutorTrait;
28
29
    /**
30
     * @var LoaderInterface
31
     */
32
    private $loader;
33
34
    /**
35
     * Construct new fixtures loader instance.
36
     *
37
     * @param EntityManagerInterface $manager EntityManagerInterface instance used for persistence.
38
     * @param LoaderInterface        $loader
39
     * @param ORMPurger              $purger
40 49
     */
41
    public function __construct(EntityManagerInterface $manager, LoaderInterface $loader, ORMPurger $purger = null)
42 49
    {
43
        parent::__construct($manager, $purger);
44 49
45 49
        $this->loader = $loader;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50 48
     */
51
    public function purge()
52 48
    {
53 48
        $connection = $this->getObjectManager()->getConnection();
54
        $mysqlPlatform = $this->purger->getPurgeMode() === ORMPurger::PURGE_MODE_TRUNCATE
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\DataFixt...\Purger\PurgerInterface as the method getPurgeMode() does only exist in the following implementations of said interface: Doctrine\Common\DataFixtures\Purger\ORMPurger.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
55
            && $connection->getDatabasePlatform() instanceof MySqlPlatform;
56
        if ($mysqlPlatform) {
57
            $connection->exec('SET FOREIGN_KEY_CHECKS = 0;');
58
        }
59
60
        parent::purge();
61
62
        if ($mysqlPlatform) {
63
            $connection->exec('SET FOREIGN_KEY_CHECKS = 1;');
64
        }
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function execute(array $fixtures, $append = false)
71
    {
72
        $this->executeExecutor($this, $this->getObjectManager(), $this->loader, $fixtures, $append);
73
    }
74
}
75