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.

AbstractCommand::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 12
cts 14
cp 0.8571
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0116
1
<?php
2
3
namespace Bernard\Driver\Doctrine\Command;
4
5
use Bernard\Driver\Doctrine\MessagesSchema;
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\Schema\Schema;
8
use Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer as Synchronizer;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
abstract class AbstractCommand extends Command
15
{
16 8
    public function __construct($name)
17
    {
18 8
        parent::__construct('bernard:doctrine:'.$name);
19 8
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 8
    public function configure()
25
    {
26 8
        $this->addOption('dump-sql', null, InputOption::VALUE_NONE, 'Output generated SQL statements instead of applying them');
27 8
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 8
    public function execute(InputInterface $input, OutputInterface $output)
33
    {
34 8
        $schema = new Schema();
35 8
        MessagesSchema::create($schema);
36 8
        $sync = $this->getSynchronizer($this->getHelper('connection')->getConnection());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method getConnection() does only exist in the following implementations of said interface: Doctrine\DBAL\Tools\Cons...Helper\ConnectionHelper.

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...
37
38 8
        if ($input->getOption('dump-sql')) {
39 4
            $output->writeln(implode(';'.PHP_EOL, $this->getSql($sync, $schema)).';');
40
41 4
            return;
42
        }
43
44 4
        $output->writeln('<comment>ATTENTION</comment>: This operation should not be executed in a production environment.'.PHP_EOL);
45 4
        $output->writeln('Applying database schema changes...');
46 4
        $this->applySql($sync, $schema);
47 4
        $output->writeln('Schema changes applied successfully!');
48 4
    }
49
50
    /**
51
     * @return \Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer
52
     */
53 2
    protected function getSynchronizer(Connection $connection)
54
    {
55 2
        return new Synchronizer($connection);
56
    }
57
58
    /**
59
     * @param \Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer $sync
60
     * @param \Doctrine\DBAL\Schema\Schema                                  $schema
61
     *
62
     * @return array
63
     */
64
    abstract protected function getSql(Synchronizer $sync, Schema $schema);
65
66
    /**
67
     * @param \Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer $sync
68
     * @param \Doctrine\DBAL\Schema\Schema                                  $schema
69
     */
70
    abstract protected function applySql(Synchronizer $sync, Schema $schema);
71
}
72