Builder   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
dl 0
loc 83
ccs 21
cts 27
cp 0.7778
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A hasTable() 0 13 3
A hasColumn() 0 4 1
A hasColumns() 0 4 1
A create() 0 9 2
A drop() 0 8 1
A createBlueprint() 0 7 1
1
<?php
2
3
/**
4
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10
 * THE SOFTWARE.
11
 */
12
13
namespace Ytake\LaravelCouchbase\Schema;
14
15
use Closure;
16
use Couchbase\Exception;
17
use Ytake\LaravelCouchbase\Database\CouchbaseConnection;
18
19
/**
20
 * Class Builder
21
 *
22
 * @author Yuuki Takezawa<[email protected]>
23
 */
24
class Builder extends \Illuminate\Database\Schema\Builder
25
{
26
    /**
27
     * The database connection instance.
28
     *
29
     * @var \Illuminate\Database\Connection|CouchbaseConnection
30
     */
31
    protected $connection;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 4
    public function hasTable($table)
37
    {
38
        try {
39 4
            $bucketInfo = $this->connection->openBucket($table)->manager()->info();
0 ignored issues
show
Bug introduced by
The method openBucket does only exist in Ytake\LaravelCouchbase\D...ase\CouchbaseConnection, but not in Illuminate\Database\Connection.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
40 2
            if (!is_null($bucketInfo['name'])) {
41 2
                return true;
42
            }
43
44
            return true;
45 2
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The class Couchbase\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
46 2
            return false;
47
        }
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 2
    public function hasColumn($table, $column)
54
    {
55 2
        return true;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 2
    public function hasColumns($table, array $columns)
62
    {
63 2
        return true;
64
    }
65
66
    /**
67
     * needs administrator password, user
68
     *
69
     * @param string       $collection
70
     * @param Closure|null $callback
71
     *
72
     * @return void
73
     */
74 8
    public function create($collection, Closure $callback = null)
75
    {
76 8
        $blueprint = $this->createBlueprint($collection);
77 8
        $blueprint->create();
78 8
        sleep(10);
79 8
        if ($callback) {
80 8
            $callback($blueprint);
81
        }
82 8
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function drop($collection)
88
    {
89
        $blueprint = $this->createBlueprint($collection);
90
        $blueprint->drop();
91
        sleep(10);
92
93
        return true;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 8
    protected function createBlueprint($table, Closure $callback = null): Blueprint
100
    {
101 8
        $blueprint = new Blueprint($table, $callback);
102 8
        $blueprint->connector($this->connection);
0 ignored issues
show
Bug introduced by
It seems like $this->connection can also be of type object<Illuminate\Database\Connection>; however, Ytake\LaravelCouchbase\S...\Blueprint::connector() does only seem to accept object<Ytake\LaravelCouc...se\CouchbaseConnection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
103
104 8
        return $blueprint;
105
    }
106
}
107