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.
Test Setup Failed
Push — master ( 57c0d0...75a351 )
by Pierre-Luc
04:56
created

Connection::getSchemaBuilder()   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 0
1
<?php
2
3
namespace fuitad\LaravelCassandra;
4
5
use Cassandra;
6
7
class Connection extends \Illuminate\Database\Connection
8
{
9
    /**
10
     * The Cassandra keyspace
11
     *
12
     * @var string
13
     */
14
    protected $keyspace;
15
16
    /**
17
     * The Cassandra cluster
18
     *
19
     * @var \Cassandra\Cluster
20
     */
21
    protected $cluster;
22
23
    /**
24
     * The Cassandra connection handler.
25
     *
26
     * @var \Cassandra\Session
27
     */
28
    protected $session;
29
30
    /**
31
     * Create a new database connection instance.
32
     *
33
     * @param  array   $config
34
     */
35
    public function __construct(array $config)
36
    {
37
        $this->config = $config;
38
39
        // You can pass options directly to the Cassandra constructor
40
        $options = array_get($config, 'options', []);
41
42
        // Create the connection
43
        $this->cluster = $this->createCluster(null, $config, $options);
44
45
        if (isset($options['keyspace']) || isset($config['keyspace'])) {
46
            $this->keyspace = $config['keyspace'];
47
            $this->session = $this->cluster->connect($config['keyspace']);
48
        }
49
50
        $this->useDefaultPostProcessor();
51
52
        $this->useDefaultSchemaGrammar();
53
54
        $this->setQueryGrammar($this->getDefaultQueryGrammar());
55
    }
56
57
    /**
58
     * Begin a fluent query against a database table.
59
     *
60
     * @param  string  $table
61
     * @return Query\Builder
62
     */
63
    public function table($table)
64
    {
65
        $processor = $this->getPostProcessor();
66
67
        $query = new Query\Builder($this, $processor);
0 ignored issues
show
Compatibility introduced by
$processor of type object<Illuminate\Databa...y\Processors\Processor> is not a sub-type of object<fuitad\LaravelCassandra\Query\Processor>. It seems like you assume a child class of the class Illuminate\Database\Query\Processors\Processor to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
68
69
        return $query->from($table);
70
    }
71
72
    /**
73
     * Get a schema builder instance for the connection.
74
     *
75
     * @return Schema\Builder
76
     */
77
    public function getSchemaBuilder()
78
    {
79
        return new Schema\Builder($this);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \fuitad\Larav...\Schema\Builder($this); (fuitad\LaravelCassandra\Schema\Builder) is incompatible with the return type of the parent method Illuminate\Database\Connection::getSchemaBuilder of type Illuminate\Database\Schema\Builder.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
80
    }
81
82
    /**
83
     * return Cassandra cluster.
84
     *
85
     * @return \Cassandra\Cluster
86
     */
87
    public function getCassandraCluster()
88
    {
89
        return $this->cluster;
90
    }
91
92
    /**
93
     * return Cassandra Session.
94
     *
95
     * @return \Cassandra\Session
96
     */
97
    public function getCassandraSession()
98
    {
99
        return $this->session;
100
    }
101
102
    /**
103
     * Return the Cassandra keyspace
104
     *
105
     * @return string
106
     */
107
    public function getKeyspace()
108
    {
109
        return $this->keyspace;
110
    }
111
112
    /**
113
     * Create a new Cassandra cluster object.
114
     *
115
     * @param  string  $dsn
116
     * @param  array   $config
117
     * @param  array   $options
118
     * @return \Cassandra\Cluster
119
     */
120
    protected function createCluster($dsn, array $config, array $options)
0 ignored issues
show
Unused Code introduced by
The parameter $dsn is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
121
    {
122
        // By default driver options is an empty array.
123
        $driverOptions = [];
0 ignored issues
show
Unused Code introduced by
$driverOptions is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
124
125
        if (isset($config['driver_options']) && is_array($config['driver_options'])) {
126
            $driverOptions = $config['driver_options'];
0 ignored issues
show
Unused Code introduced by
$driverOptions is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
127
        }
128
129
        $cluster = Cassandra::cluster();
130
131
        // Check if the credentials are not already set in the options
132 View Code Duplication
        if (!isset($options['username']) && !empty($config['username'])) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
            $options['username'] = $config['username'];
134
        }
135 View Code Duplication
        if (!isset($options['password']) && !empty($config['password'])) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
            $options['password'] = $config['password'];
137
        }
138
139
        // Authentication
140
        if (isset($options['username']) && isset($options['password'])) {
141
            $cluster->withCredentials($options['username'], $options['password']);
142
        }
143
144
        // Contact Points/Host
145
        if (isset($options['contactpoints']) || (isset($config['host']) && !empty($config['host']))) {
146
            $contactPoints = $config['host'];
147
148
            if (isset($options['contactpoints'])) {
149
                $contactPoints = $options['contactpoints'];
150
            }
151
152
            $cluster->withContactPoints($contactPoints);
153
        }
154
155
        if (!isset($options['port']) && !empty($config['port'])) {
156
            $cluster->withPort((int) $config['port']);
157
        }
158
159
        return $cluster->build();
160
    }
161
162
    /**
163
     * Disconnect from the underlying Cassandra connection.
164
     */
165
    public function disconnect()
166
    {
167
        unset($this->connection);
168
    }
169
170
    /**
171
     * Get the PDO driver name.
172
     *
173
     * @return string
174
     */
175
    public function getDriverName()
176
    {
177
        return 'cassandra';
178
    }
179
180
    /**
181
     * Run a select statement against the database.
182
     *
183
     * @param  string  $query
184
     * @param  array  $bindings
185
     * @param  bool  $useReadPdo
186
     * @return array
187
     */
188 View Code Duplication
    public function select($query, $bindings = [], $useReadPdo = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
189
    {
190
        return $this->run($query, $bindings, function ($query, $bindings) use ($useReadPdo) {
191
            if ($this->pretending()) {
192
                return [];
193
            }
194
195
            $statement = $this->session->prepare($query);
196
197
            return $this->session->execute($statement, new \Cassandra\ExecutionOptions(['arguments' => $bindings]));
198
        });
199
    }
200
201
    /**
202
     * Execute an SQL statement and return the boolean result.
203
     *
204
     * @param  string  $query
205
     * @param  array   $bindings
206
     * @return bool
207
     */
208 View Code Duplication
    public function statement($query, $bindings = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
209
    {
210
        return $this->run($query, $bindings, function ($query, $bindings) {
211
            if ($this->pretending()) {
212
                return [];
213
            }
214
215
            $statement = $this->session->prepare($query);
216
217
            return $this->session->execute($statement, new \Cassandra\ExecutionOptions(['arguments' => $bindings]));
218
        });
219
    }
220
221
    /**
222
     * Because Cassandra is an eventually consistent database, it's not possible to obtain
223
     * the affected count for statements so we're just going to return 0, based on the idea
224
     * that if the query fails somehow, an exception will be thrown
225
     *
226
     * @param  string  $query
227
     * @param  array   $bindings
228
     * @return int
229
     */
230 View Code Duplication
    public function affectingStatement($query, $bindings = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
231
    {
232
        return $this->run($query, $bindings, function ($query, $bindings) {
233
            if ($this->pretending()) {
234
                return 0;
235
            }
236
237
            $statement = $this->session->prepare($query);
238
239
            $this->session->execute($statement, new \Cassandra\ExecutionOptions(['arguments' => $bindings]));
240
241
            return 1;
242
        });
243
    }
244
245
    /**
246
     * @inheritdoc
247
     */
248
    protected function getDefaultPostProcessor()
249
    {
250
        return new Query\Processor();
251
    }
252
253
    /**
254
     * @inheritdoc
255
     */
256
    protected function getDefaultQueryGrammar()
257
    {
258
        return new Query\Grammar();
259
    }
260
261
    /**
262
     * @inheritdoc
263
     */
264
    protected function getDefaultSchemaGrammar()
265
    {
266
        //return new Schema\Grammar();
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
267
    }
268
269
    /**
270
     * Dynamically pass methods to the connection.
271
     *
272
     * @param  string  $method
273
     * @param  array   $parameters
274
     * @return mixed
275
     */
276
    public function __call($method, $parameters)
277
    {
278
        return call_user_func_array([$this->cluster, $method], $parameters);
279
    }
280
}
281