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 ( 1144b4...828920 )
by Márk
07:09 queued 04:40
created

AbstractCommand::getSynchronizer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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
/**
15
 * @package Bernard
16
 */
17
abstract class AbstractCommand extends Command
18
{
19
    public function __construct($name)
20
    {
21
        parent::__construct('bernard:doctrine:' . $name);
22
    }
23
24
    /**
25
     * {@inheritDoc}
26
     */
27
    public function configure()
28
    {
29
        $this->addOption('dump-sql', null, InputOption::VALUE_NONE, 'Output generated SQL statements instead of applying them');
30
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35
    public function execute(InputInterface $input, OutputInterface $output)
36
    {
37
        $schema = new Schema;
38
        MessagesSchema::create($schema);
39
        $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...
40
41
        if ($input->getOption('dump-sql')) {
42
            $output->writeln(implode(';' . PHP_EOL, $this->getSql($sync, $schema)) . ';');
43
            return;
44
        }
45
46
        $output->writeln('<comment>ATTENTION</comment>: This operation should not be executed in a production environment.' . PHP_EOL);
47
        $output->writeln('Applying database schema changes...');
48
        $this->applySql($sync, $schema);
49
        $output->writeln('Schema changes applied successfully!');
50
    }
51
52
    /**
53
     * @return \Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer
54
     */
55
    protected function getSynchronizer(Connection $connection)
56
    {
57
        return new Synchronizer($connection);
58
    }
59
60
    /**
61
     * @param \Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer $sync
62
     * @param \Doctrine\DBAL\Schema\Schema $schema
63
     * @return array
64
     */
65
    abstract protected function getSql(Synchronizer $sync, Schema $schema);
66
67
    /**
68
     * @param \Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer $sync
69
     * @param \Doctrine\DBAL\Schema\Schema $schema
70
     */
71
    abstract protected function applySql(Synchronizer $sync, Schema $schema);
72
}
73