Completed
Push — master ( 0f46ff...89eb26 )
by yuuki
11s
created

Builder::hasTable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 9.4285
c 0
b 0
f 0
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 Ytake\LaravelCouchbase\Database\CouchbaseConnection;
17
18
/**
19
 * Class Builder
20
 *
21
 * @author Yuuki Takezawa<[email protected]>
22
 */
23
class Builder extends \Illuminate\Database\Schema\Builder
24
{
25
    /**
26
     * The database connection instance.
27
     *
28
     * @var \Illuminate\Database\Connection|CouchbaseConnection
29
     */
30
    protected $connection;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 2
    public function hasTable($table)
36
    {
37
        try {
38 2
            if (!is_null($this->connection->openBucket($table)->getName())) {
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...
39 1
                return true;
40
            }
41 1
        } catch (\CouchbaseException $e) {
0 ignored issues
show
Bug introduced by
The class CouchbaseException 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...
42 1
            return false;
43
        }
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 1
    public function hasColumn($table, $column)
50
    {
51 1
        return true;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 1
    public function hasColumns($table, array $columns)
58
    {
59 1
        return true;
60
    }
61
62
    /**
63
     * needs administrator password, user
64
     * @param string       $collection
65
     * @param Closure|null $callback
66
     *
67
     * @return void
68
     */
69 2
    public function create($collection, Closure $callback = null)
70
    {
71 2
        $blueprint = $this->createBlueprint($collection);
72 2
        $blueprint->create();
73 2
        sleep(5);
74 2
        if ($callback) {
75 2
            $callback($blueprint);
76
        }
77 2
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function drop($collection)
83
    {
84
        $blueprint = $this->createBlueprint($collection);
85
        $blueprint->drop();
86
        sleep(5);
87
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type of the parent method Illuminate\Database\Schema\Builder::drop of type Illuminate\Database\Schema\Blueprint|null.

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...
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 2
    protected function createBlueprint($table, Closure $callback = null)
94
    {
95 2
        $blueprint = new Blueprint($table, $callback);
96 2
        $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...
97
98 2
        return $blueprint;
99
    }
100
}
101