Completed
Push — master ( 08d649...702658 )
by Jeremy
02:55
created

DBAL/Schema/CustomPostgreSqlSchemaManager.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Wallabag\CoreBundle\Doctrine\DBAL\Schema;
4
5
use Doctrine\DBAL\Schema\PostgreSqlSchemaManager;
6
use Doctrine\DBAL\Schema\Sequence;
7
8
/**
9
 * This custom schema manager fix the PostgreSQL 10 problem.
10
 *
11
 * @see https://github.com/wallabag/wallabag/issues/3479
12
 * @see https://github.com/doctrine/dbal/issues/2868
13
 */
14
class CustomPostgreSqlSchemaManager extends PostgreSqlSchemaManager
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    protected function _getPortableSequenceDefinition($sequence)
20
    {
21
        $sequenceName = $sequence['relname'];
22
        if ('public' !== $sequence['schemaname']) {
23
            $sequenceName = $sequence['schemaname'] . '.' . $sequence['relname'];
24
        }
25
26
        $query = 'SELECT min_value, increment_by FROM ' . $this->_platform->quoteIdentifier($sequenceName);
27
28
        // the `method_exists` is only to avoid test to fail:
29
        // DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticConnection doesn't support the `getServerVersion`
30
        if (method_exists($this->_conn->getWrappedConnection(), 'getServerVersion') && (float) ($this->_conn->getWrappedConnection()->getServerVersion()) >= 10) {
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\DBAL\Driver\Connection as the method getServerVersion() does only exist in the following implementations of said interface: Doctrine\DBAL\Connection, Doctrine\DBAL\Connections\MasterSlaveConnection, Doctrine\DBAL\Driver\DrizzlePDOMySql\Connection, Doctrine\DBAL\Driver\IBMDB2\DB2Connection, Doctrine\DBAL\Driver\Mysqli\MysqliConnection, Doctrine\DBAL\Driver\OCI8\OCI8Connection, Doctrine\DBAL\Driver\PDOConnection, Doctrine\DBAL\Driver\PDOSqlsrv\Connection, Doctrine\DBAL\Driver\SQL...e\SQLAnywhereConnection, Doctrine\DBAL\Driver\SQLSrv\SQLSrvConnection, Doctrine\DBAL\Portability\Connection, Doctrine\DBAL\Sharding\PoolingShardConnection.

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...
31
            $query = "SELECT min_value, increment_by FROM pg_sequences WHERE schemaname = 'public' AND sequencename = " . $this->_conn->quote($sequenceName);
32
        }
33
34
        $data = $this->_conn->fetchAll($query);
35
36
        return new Sequence($sequenceName, $data[0]['increment_by'], $data[0]['min_value']);
37
    }
38
}
39