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.

Builder::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
namespace duxet\RethinkDB\Schema;
4
5
use Closure;
6
use duxet\Rethinkdb\Connection;
7
use duxet\Rethinkdb\Schema\Blueprint;
8
use r;
9
10
class Builder extends \Illuminate\Database\Schema\Builder
11
{
12
    /**
13
     * Create a new database Schema manager.
14
     *
15
     * @param Connection $connection
16
     */
17
    public function __construct(Connection $connection)
18
    {
19
        $this->connection = $connection;
20
    }
21
22
    /**
23
     * Determine if the given table exists.
24
     *
25
     * @param string $table
26
     *
27
     * @return bool
28
     */
29
    public function hasTable($table)
30
    {
31
        $conn = $this->connection->getConnection();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Illuminate\Database\Connection as the method getConnection() does only exist in the following sub-classes of Illuminate\Database\Connection: duxet\Rethinkdb\Connection. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
32
        $db = r\db($this->connection->getDatabaseName());
33
        $tables = $db->tableList()->run($conn);
34
35
        return in_array($table, $tables);
36
    }
37
38
    /**
39
     * Create a new table on the schema.
40
     *
41
     * @param string  $table
42
     * @param Closure $callback
43
     *
44
     * @return bool
45
     */
46
    public function create($table, Closure $callback = null)
47
    {
48
        $blueprint = $this->createBlueprint($table);
49
        $blueprint->create();
50
        if ($callback) {
51
            $callback($blueprint);
52
        }
53
    }
54
55
    /**
56
     * Drop a table from the schema.
57
     *
58
     * @param string $table
59
     *
60
     * @return bool
61
     */
62
    public function drop($table)
63
    {
64
        $blueprint = $this->createBlueprint($table);
65
66
        return $blueprint->drop();
0 ignored issues
show
Bug Compatibility introduced by
The expression $blueprint->drop(); of type boolean|null adds the type boolean to the return on line 66 which is incompatible with the return type of the parent method Illuminate\Database\Schema\Builder::drop of type Illuminate\Database\Schema\Blueprint|null.
Loading history...
67
    }
68
69
    /**
70
     * Modify a table on the schema.
71
     *
72
     * @param string  $table
73
     * @param Closure $callback
74
     *
75
     * @return bool
76
     */
77
    public function table($table, Closure $callback)
78
    {
79
        $blueprint = $this->createBlueprint($table);
80
        if ($callback) {
81
            $callback($blueprint);
82
        }
83
    }
84
85
    /**
86
     * Create a new command set with a Closure.
87
     *
88
     * @param string  $table
89
     * @param Closure $callback
90
     *
91
     * @return Blueprint
92
     */
93
    protected function createBlueprint($table, Closure $callback = null)
94
    {
95
        return new Blueprint($this->connection, $table);
96
    }
97
}
98