Passed
Branch testing (3dc953)
by Roman
09:00
created

Model   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 252
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 73.42%

Importance

Changes 0
Metric Value
wmc 39
lcom 1
cbo 8
dl 0
loc 252
ccs 58
cts 79
cp 0.7342
rs 9.28
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
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 originalIsNumericallyEquivalent() 0 15 4
A getQualifiedKeyName() 0 4 1
A qualifyColumn() 0 4 1
B setAttribute() 0 33 7
A __call() 0 9 2
A newCassandraCollection() 0 19 5
B originalIsEquivalent() 0 26 9
A getKey() 0 10 2
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
use Illuminate\Support\Str;
13
14
abstract class Model extends BaseModel
15
{
16
    use CassandraTypesTrait;
17
18
    /**
19
     * The connection name for the model.
20
     *
21
     * @var string
22
     */
23
    protected $connection = 'cassandra';
24
25
    /**
26
     * Indicates if the IDs are auto-incrementing.
27
     * This is not possible in cassandra so we override this
28
     *
29
     * @var bool
30
     */
31
    public $incrementing = false;
32
33
    /**
34
     * @inheritdoc
35
     */
36 55
    public function newEloquentBuilder($query)
37
    {
38 55
        return new Builder($query);
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44 55
    protected function newBaseQueryBuilder()
45
    {
46 55
        $connection = $this->getConnection();
47
48 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...
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54 13
    public function freshTimestamp()
55
    {
56 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...
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62 13
    public function fromDateTime($value)
63
    {
64
        // If the value is already a Timestamp instance, we don't need to parse it.
65 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...
66 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...
67
        }
68
69
        // Let Eloquent convert the value to a DateTime instance.
70 1
        if (!$value instanceof \DateTime) {
71 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...
72
        }
73
74 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...
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80 6
    protected function asDateTime($value)
81
    {
82
        // Convert UTCDateTime instances.
83 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...
84 6
            return Carbon::instance($value->toDateTime());
85
        }
86
87
        return parent::asDateTime($value);
88
    }
89
90
    /**
91
     * @inheritdoc
92
     */
93
    protected function originalIsNumericallyEquivalent($key)
94
    {
95
        $current = $this->attributes[$key];
96
        $original = $this->original[$key];
97
98
        // Date comparison.
99
        if (in_array($key, $this->getDates())) {
100
            $current = $current instanceof Timestamp ? $this->asDateTime($current) : $current;
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...
101
            $original = $original instanceof Timestamp ? $this->asDateTime($original) : $original;
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...
102
103
            return $current == $original;
104
        }
105
106
        return parent::originalIsNumericallyEquivalent($key);
107
    }
108
109
    /**
110
     * Get the table qualified key name.
111
     * Cassandra does not support the table.column annotation so
112
     * we override this
113
     *
114
     * @return string
115
     */
116 8
    public function getQualifiedKeyName()
117
    {
118 8
        return $this->getKeyName();
119
    }
120
121
    /**
122
     * Qualify the given column name by the model's table.
123
     *
124
     * @param  string  $column
125
     * @return string
126
     */
127 2
    public function qualifyColumn($column)
128
    {
129 2
        return $column;
130
    }
131
132
     /**
133
     * Set a given attribute on the model.
134
     *
135
     * @param  string  $key
136
     * @param  mixed  $value
137
     * @return $this
138
     */
139 14
    public function setAttribute($key, $value)
140
    {
141
        // First we will check for the presence of a mutator for the set operation
142
        // which simply lets the developers tweak the attribute as it is set on
143
        // the model, such as "json_encoding" an listing of data for storage.
144 14
        if ($this->hasSetMutator($key)) {
145
            $method = 'set' . Str::studly($key) . 'Attribute';
146
147
            return $this->{$method}($value);
148
        }
149
150
        // If an attribute is listed as a "date", we'll convert it from a DateTime
151
        // instance into a form proper for storage on the database tables using
152
        // the connection grammar's date format. We will auto set the values.
153 14
        elseif ($value !== null && $this->isDateAttribute($key)) {
154 13
            $value = $this->fromDateTime($value);
155
        }
156
157 14
        if ($this->isJsonCastable($key) && !is_null($value)) {
158
            $value = $this->castAttributeAsJson($key, $value);
159
        }
160
161
        // If this attribute contains a JSON ->, we'll set the proper value in the
162
        // attribute's underlying array. This takes care of properly nesting an
163
        // attribute in the array's value in the case of deeply nested items.
164 14
        if (Str::contains($key, '->')) {
165
            return $this->fillJsonAttribute($key, $value);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->fillJsonAttribute($key, $value); (lroman242\LaravelCassandra\Eloquent\Model) is incompatible with the return type of the parent method Illuminate\Database\Eloquent\Model::setAttribute of type Illuminate\Database\Eloq...\Concerns\HasAttributes.

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...
166
        }
167
168 14
        $this->attributes[$key] = $value;
169
170 14
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (lroman242\LaravelCassandra\Eloquent\Model) is incompatible with the return type of the parent method Illuminate\Database\Eloquent\Model::setAttribute of type Illuminate\Database\Eloq...\Concerns\HasAttributes.

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...
171
    }
172
    
173
    /**
174
     * @inheritdoc
175
     */
176 55
    public function __call($method, $parameters)
177
    {
178
        // Unset method
179 55
        if ($method == 'unset') {
180
            return call_user_func_array([$this, 'drop'], $parameters);
181
        }
182
183 55
        return parent::__call($method, $parameters);
184
    }
185
186
    /**
187
     * Create a new Eloquent Collection instance.
188
     *
189
     * @param  Rows|array  $rows
190
     *
191
     * @return Collection
192
     *
193
     * @throws \Exception
194
     */
195 50
    public function newCassandraCollection($rows)
196
    {
197 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...
198
            throw new \Exception('Wrong type to create collection');//TODO: customize error
199
        }
200
201 50
        $items = [];
202 50
        foreach ($rows as $row) {
203 47
            $items[] = $this->newFromBuilder($row);
204
        }
205
206 50
        $collection = new Collection($items);
207
208 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...
209 19
            $collection->setRowsInstance($rows);
210
        }
211
212 50
        return $collection;
213
    }
214
215
    /**
216
     * Determine if the new and old values for a given key are equivalent.
217
     *
218
     * @param  string $key
219
     * @param  mixed $current
220
     * @return bool
221
     */
222 13
    public function originalIsEquivalent($key, $current)
223
    {
224 13
        if (!array_key_exists($key, $this->original)) {
225 13
            return false;
226
        }
227
228 2
        $original = $this->getOriginal($key);
229
230 2
        if ($current === $original) {
231 2
            return true;
232 2
        } elseif (is_null($current)) {
233
            return false;
234 2
        } elseif ($this->isDateAttribute($key)) {
235 2
            return $this->fromDateTime($current) ===
236 2
                $this->fromDateTime($original);
237 1
        } elseif ($this->hasCast($key)) {
238
            return $this->castAttribute($key, $current) ===
239
                $this->castAttribute($key, $original);
240 1
        } elseif ($this->isCassandraValueObject($current)) {
241
            return $this->valueFromCassandraObject($current) ===
242
                $this->valueFromCassandraObject($original);
243
        }
244
245 1
        return is_numeric($current) && is_numeric($original)
246 1
            && strcmp((string) $current, (string) $original) === 0;
247
    }
248
249
    /**
250
     * Get the value of the model's primary key.
251
     *
252
     * @return mixed
253
     */
254 16
    public function getKey()
255
    {
256 16
        $value = $this->getAttribute($this->getKeyName());
257
258 16
        if ($this->isCassandraValueObject($value)) {
259
            return $this->valueFromCassandraObject($this->getAttribute($this->getKeyName()));
260
        }
261
262 16
        return $value;
263
    }
264
265
}
266