Passed
Branch testing (fdbc4a)
by Roman
09:06
created

Model   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 84.75%

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 7
dl 0
loc 192
ccs 50
cts 59
cp 0.8475
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getKey() 0 10 2
A newEloquentBuilder() 0 4 1
A newBaseQueryBuilder() 0 6 1
A freshTimestamp() 0 4 1
A fromDateTime() 0 14 3
A asDateTime() 0 9 2
A getQualifiedKeyName() 0 4 1
A qualifyColumn() 0 4 1
A __call() 0 9 2
A newCassandraCollection() 0 19 5
B originalIsEquivalent() 0 26 9
1
<?php
2
3
namespace lroman242\LaravelCassandra\Eloquent;
4
5
use Carbon\Carbon;
6
use Cassandra\Rows;
7
use Cassandra\Timestamp;
8
use lroman242\LaravelCassandra\CassandraTypesTrait;
9
use lroman242\LaravelCassandra\Collection;
10
use lroman242\LaravelCassandra\Query\Builder as QueryBuilder;
11
use Illuminate\Database\Eloquent\Model as BaseModel;
12
13
abstract class Model extends BaseModel
14
{
15
    use CassandraTypesTrait;
16
17
    /**
18
     * The connection name for the model.
19
     *
20
     * @var string
21
     */
22
    protected $connection = 'cassandra';
23
24
    /**
25
     * Indicates if the IDs are auto-incrementing.
26
     * This is not possible in cassandra so we override this
27
     *
28
     * @var bool
29
     */
30
    public $incrementing = false;
31
32
    /**
33
     * @inheritdoc
34
     */
35 55
    public function newEloquentBuilder($query)
36
    {
37 55
        return new Builder($query);
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43 55
    protected function newBaseQueryBuilder()
44
    {
45 55
        $connection = $this->getConnection();
46
47 55
        return new QueryBuilder($connection, null, $connection->getPostProcessor());
0 ignored issues
show
Compatibility introduced by
$connection of type object<Illuminate\Database\Connection> is not a sub-type of object<lroman242\LaravelCassandra\Connection>. It seems like you assume a child class of the class Illuminate\Database\Connection 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...
Documentation introduced by
$connection->getPostProcessor() is of type object<Illuminate\Databa...y\Processors\Processor>, but the function expects a null|object<lroman242\La...sandra\Query\Processor>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 13
    public function freshTimestamp()
54
    {
55 13
        return new Timestamp();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Cassandra\Timestamp(); (Cassandra\Timestamp) is incompatible with the return type of the parent method Illuminate\Database\Eloquent\Model::freshTimestamp of type Illuminate\Support\Carbon.

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...
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61 13
    public function fromDateTime($value)
62
    {
63
        // If the value is already a Timestamp instance, we don't need to parse it.
64 13
        if ($value instanceof Timestamp) {
0 ignored issues
show
Bug introduced by
The class Cassandra\Timestamp does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
65 13
            return $value;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $value; (Cassandra\Timestamp) is incompatible with the return type of the parent method Illuminate\Database\Eloquent\Model::fromDateTime of type integer|string.

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...
66
        }
67
68
        // Let Eloquent convert the value to a DateTime instance.
69 1
        if (!$value instanceof \DateTime) {
70 1
            $value = parent::asDateTime($value);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (asDateTime() instead of fromDateTime()). Are you sure this is correct? If so, you might want to change this to $this->asDateTime().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
71
        }
72
73 1
        return new Timestamp($value->getTimestamp() * 1000);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Cassandra\Ti...getTimestamp() * 1000); (Cassandra\Timestamp) is incompatible with the return type of the parent method Illuminate\Database\Eloquent\Model::fromDateTime of type integer|string.

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...
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79 6
    protected function asDateTime($value)
80
    {
81
        // Convert UTCDateTime instances.
82 6
        if ($value instanceof Timestamp) {
0 ignored issues
show
Bug introduced by
The class Cassandra\Timestamp does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
83 6
            return Carbon::instance($value->toDateTime());
84
        }
85
86
        return parent::asDateTime($value);
87
    }
88
89
    /**
90
     * Get the table qualified key name.
91
     * Cassandra does not support the table.column annotation so
92
     * we override this
93
     *
94
     * @return string
95
     */
96 8
    public function getQualifiedKeyName()
97
    {
98 8
        return $this->getKeyName();
99
    }
100
101
    /**
102
     * Qualify the given column name by the model's table.
103
     *
104
     * @param  string  $column
105
     * @return string
106
     */
107 2
    public function qualifyColumn($column)
108
    {
109 2
        return $column;
110
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115 55
    public function __call($method, $parameters)
116
    {
117
        // Unset method
118 55
        if ($method == 'unset') {
119
            return call_user_func_array([$this, 'drop'], $parameters);
120
        }
121
122 55
        return parent::__call($method, $parameters);
123
    }
124
125
    /**
126
     * Create a new Eloquent Collection instance.
127
     *
128
     * @param  Rows|array  $rows
129
     *
130
     * @return Collection
131
     *
132
     * @throws \Exception
133
     */
134 50
    public function newCassandraCollection($rows)
135
    {
136 50
        if (!is_array($rows) && !$rows instanceof Rows) {
0 ignored issues
show
Bug introduced by
The class Cassandra\Rows does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
137
            throw new \Exception('Wrong type to create collection');//TODO: customize error
138
        }
139
140 50
        $items = [];
141 50
        foreach ($rows as $row) {
142 47
            $items[] = $this->newFromBuilder($row);
143
        }
144
145 50
        $collection = new Collection($items);
146
147 50
        if ($rows instanceof Rows) {
0 ignored issues
show
Bug introduced by
The class Cassandra\Rows does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
148 19
            $collection->setRowsInstance($rows);
149
        }
150
151 50
        return $collection;
152
    }
153
154
    /**
155
     * Determine if the new and old values for a given key are equivalent.
156
     *
157
     * @param  string $key
158
     * @param  mixed $current
159
     * @return bool
160
     */
161 13
    public function originalIsEquivalent($key, $current)
162
    {
163 13
        if (!array_key_exists($key, $this->original)) {
164 13
            return false;
165
        }
166
167 2
        $original = $this->getOriginal($key);
168
169 2
        if ($current === $original) {
170 2
            return true;
171 2
        } elseif (is_null($current)) {
172
            return false;
173 2
        } elseif ($this->isDateAttribute($key)) {
174 2
            return $this->fromDateTime($current) ===
175 2
                $this->fromDateTime($original);
176 1
        } elseif ($this->hasCast($key)) {
177
            return $this->castAttribute($key, $current) ===
178
                $this->castAttribute($key, $original);
179 1
        } elseif ($this->isCassandraValueObject($current)) {
180
            return $this->valueFromCassandraObject($current) ===
181
                $this->valueFromCassandraObject($original);
182
        }
183
184 1
        return is_numeric($current) && is_numeric($original)
185 1
            && strcmp((string) $current, (string) $original) === 0;
186
    }
187
188
    /**
189
     * Get the value of the model's primary key.
190
     *
191
     * @return mixed
192
     */
193 16
    public function getKey()
194
    {
195 16
        $value = $this->getAttribute($this->getKeyName());
196
197 16
        if ($this->isCassandraValueObject($value)) {
198
            return $this->valueFromCassandraObject($this->getAttribute($this->getKeyName()));
199
        }
200
201 16
        return $value;
202
    }
203
204
}
205