Passed
Branch master (9048ee)
by Roman
09:26
created

Model::newCassandraCollection()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 9
cts 10
cp 0.9
rs 9.3222
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5.025
1
<?php
2
3
namespace lroman242\LaravelCassandra\Eloquent;
4
5
use Carbon\Carbon;
6
use Cassandra\Date;
7
use Cassandra\Rows;
8
use Cassandra\Time;
9
use Cassandra\Timestamp;
10
use lroman242\LaravelCassandra\CassandraTypesTrait;
11
use lroman242\LaravelCassandra\Collection;
12
use lroman242\LaravelCassandra\Query\Builder as QueryBuilder;
13
use Illuminate\Database\Eloquent\Model as BaseModel;
14
15
abstract class Model extends BaseModel
16
{
17
    use CassandraTypesTrait;
18
19
    /**
20
     * The connection name for the model.
21
     *
22
     * @var string
23
     */
24
    protected $connection = 'cassandra';
25
26
    /**
27
     * Indicates if the IDs are auto-incrementing.
28
     * This is not possible in cassandra so we override this
29
     *
30
     * @var bool
31
     */
32
    public $incrementing = false;
33
34
    /**
35
     * The storage format of the model's time columns.
36
     *
37
     * @var string
38
     */
39
    protected $timeFormat;
40
41
    /**
42
     * List of columns in primary key
43
     *
44
     * @var array
45
     */
46
    protected $primaryColumns = [];
47
48
    /**
49
     * @inheritdoc
50
     */
51 58
    public function newEloquentBuilder($query)
52
    {
53 58
        return new Builder($query);
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59 58
    protected function newBaseQueryBuilder()
60
    {
61 58
        $connection = $this->getConnection();
62
63 58
        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...
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69 13
    public function freshTimestamp()
70
    {
71 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...
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77 13
    public function fromDateTime($value)
78
    {
79
        // If the value is already a Timestamp instance, we don't need to parse it.
80 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...
81 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...
82
        }
83
84
        // Let Eloquent convert the value to a DateTime instance.
85 1
        if (!$value instanceof \DateTime) {
86 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...
87
        }
88
89 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...
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95 8
    protected function asDateTime($value)
96
    {
97
        // Convert UTCDateTime instances.
98 8
        if ($value instanceof Timestamp || $value instanceof Date) {
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...
Bug introduced by
The class Cassandra\Date 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...
99 8
            return Carbon::instance($value->toDateTime());
100
        }
101
102
        return parent::asDateTime($value);
103
    }
104
105
    /**
106
     * Get the table qualified key name.
107
     * Cassandra does not support the table.column annotation so
108
     * we override this
109
     *
110
     * @return string
111
     */
112 8
    public function getQualifiedKeyName()
113
    {
114 8
        return $this->getKeyName();
115
    }
116
117
    /**
118
     * Qualify the given column name by the model's table.
119
     *
120
     * @param string $column
121
     * @return string
122
     */
123 2
    public function qualifyColumn($column)
124
    {
125 2
        return $column;
126
    }
127
128
    /**
129
     * @inheritdoc
130
     */
131 58
    public function __call($method, $parameters)
132
    {
133
        // Unset method
134 58
        if ($method == 'unset') {
135
            return call_user_func_array([$this, 'drop'], $parameters);
136
        }
137
138 58
        return parent::__call($method, $parameters);
139
    }
140
141
    /**
142
     * Create a new Eloquent Collection instance.
143
     *
144
     * @param Rows|array $rows
145
     *
146
     * @return Collection
147
     *
148
     * @throws \Exception
149
     */
150 53
    public function newCassandraCollection($rows)
151
    {
152 53
        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...
153
            throw new \Exception('Wrong type to create collection');//TODO: customize error
154
        }
155
156 53
        $items = [];
157 53
        foreach ($rows as $row) {
158 50
            $items[] = $this->newFromBuilder($row);
159
        }
160
161 53
        $collection = new Collection($items);
162
163 53
        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...
164 19
            $collection->setRowsInstance($rows);
165
        }
166
167 53
        return $collection;
168
    }
169
170
    /**
171
     * Determine if the new and old values for a given key are equivalent.
172
     *
173
     * @param string $key
174
     * @param mixed $current
175
     *
176
     * @return bool
177
     *
178
     * @throws \Exception
179
     */
180 13
    public function originalIsEquivalent($key, $current)
181
    {
182 13
        if (!array_key_exists($key, $this->original)) {
183 13
            return false;
184
        }
185
186 2
        $original = $this->getOriginal($key);
187
188 2
        if ($current === $original) {
189 2
            return true;
190 2
        } elseif (is_null($current)) {
191
            return false;
192 2
        } elseif ($this->isDateAttribute($key)) {
193 2
            return $this->fromDateTime($current) ===
194 2
                $this->fromDateTime($original);
195 1
        } elseif ($this->hasCast($key)) {
196
            return $this->castAttribute($key, $current) ===
197
                $this->castAttribute($key, $original);
198 1
        } elseif ($this->isCassandraValueObject($current)) {
199
            return $this->valueFromCassandraObject($current) ===
200
                $this->valueFromCassandraObject($original);
201
        }
202
203 1
        return is_numeric($current) && is_numeric($original)
204 1
            && strcmp((string)$current, (string)$original) === 0;
205
    }
206
207
    /**
208
     * Get the value of the model's primary key.
209
     *
210
     * @return mixed
211
     */
212 16
    public function getKey()
213
    {
214 16
        $value = $this->getAttribute($this->getKeyName());
215
216 16
        if ($this->isCassandraValueObject($value)) {
217
            return $this->valueFromCassandraObject($this->getAttribute($this->getKeyName()));
218
        }
219
220 16
        return $value;
221
    }
222
223
    /**
224
     * Cast an attribute to a native PHP type.
225
     *
226
     * @param string $key
227
     * @param mixed $value
228
     *
229
     * @return mixed
230
     *
231
     * @throws \Exception
232
     */
233 3
    protected function castAttribute($key, $value)
234
    {
235 3
        if ($this->getCastType($key) == 'string' && $value instanceof Time) {
0 ignored issues
show
Bug introduced by
The class Cassandra\Time 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...
236 1
            return (new \DateTime('today', new \DateTimeZone("+0")))
237 1
                ->modify('+' . $value->seconds() . ' seconds')
238 1
                ->format($this->getTimeFormat());
239
        }
240
241 2
        if ($this->getCastType($key) == 'int' && $value instanceof Time) {
0 ignored issues
show
Bug introduced by
The class Cassandra\Time 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...
242
            return $value->seconds();
243
        }
244
245 2
        return parent::castAttribute($key, $value);
246
    }
247
248
    /**
249
     * Get the format for time stored in database.
250
     *
251
     * @return string
252
     */
253 1
    public function getTimeFormat()
254
    {
255 1
        return $this->timeFormat ?: 'H:i:s';
256
    }
257
258
    /**
259
     * Get the format for time stored in database.
260
     *
261
     * @param string $format
262
     *
263
     * @return self
264
     */
265
    public function setTimeFormat($format)
266
    {
267
        $this->timeFormat = $format;
268
269
        return $this;
270
    }
271
272
    /**
273
     * Save the model to the database.
274
     *
275
     * @param array $options
276
     *
277
     * @return bool
278
     */
279 13
    public function save(array $options = [])
280
    {
281 13
        $query = $this->newQueryWithoutScopes();
282
283
        // If the "saving" event returns false we'll bail out of the save and return
284
        // false, indicating that the save failed. This provides a chance for any
285
        // listeners to cancel save operations if validations fail or whatever.
286 13
        if ($this->fireModelEvent('saving') === false) {
287
            return false;
288
        }
289
290
        // If the model already exists in the database we can just update our record
291
        // that is already in this database using the current IDs in this "where"
292
        // clause to only update this model. Otherwise, we'll just insert them.
293 13
        if ($this->exists) {
294 2
            if ($this->isDirty()) {
295
                // If any of primary key columns where updated cassandra won't be able
296
                // to process update of existed record. That is why existed record will
297
                // be deleted and inserted new one
298 2
                $dirtyKeys = array_keys($this->getDirty());
299 2
                $dirtyPrimaryKeys = array_intersect($this->primaryColumns, $dirtyKeys);
300 2
                $dirtyPrimaryExists = count($dirtyPrimaryKeys) > 0;
301
302
                // Check if any of primary key columns is dirty
303 2
                if (!$dirtyPrimaryExists) {
304 2
                    $primaryColumnsExceptId = array_diff($this->primaryColumns, [$this->primaryKey]);
305
306 2
                    foreach ($primaryColumnsExceptId as $key) {
307
                        $query->where($key, $this->attributes[$key]);
0 ignored issues
show
Bug introduced by
The method where does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

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...
308
                    }
309
310 2
                    $saved = $this->performUpdate($query);
0 ignored issues
show
Bug introduced by
It seems like $query defined by $this->newQueryWithoutScopes() on line 281 can also be of type object<Illuminate\Database\Eloquent\Model>; however, Illuminate\Database\Eloq...\Model::performUpdate() does only seem to accept object<Illuminate\Database\Eloquent\Builder>, 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...
311
                } else {
312
                    $this->fireModelEvent('updating');
313
314
                    // Disable model deleting, deleted, creating and created events
315
                    $ed = $this->getEventDispatcher();
316
                    $this->unsetEventDispatcher();
317
318
                    $oldValues = $this->original;
319
                    // Insert new record (duplicate)
320
                    $saved = $this->performInsert($query);
0 ignored issues
show
Bug introduced by
It seems like $query defined by $this->newQueryWithoutScopes() on line 281 can also be of type object<Illuminate\Database\Eloquent\Model>; however, Illuminate\Database\Eloq...\Model::performInsert() does only seem to accept object<Illuminate\Database\Eloquent\Builder>, 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...
321
322
                    // Delete old record
323
                    $deleteQuery = static::query();
324
325
                    foreach ($this->primaryColumns as $key) {
326
                        $deleteQuery->where($key, $oldValues[$key]);
327
                    }
328
329
                    $deleteQuery->delete();
330
331
                    //Already in silent mode
332
                    if ($ed !== null) {
333
                        $this->setEventDispatcher($ed);
334
                    }
335
336 2
                    $this->fireModelEvent('updated');
337
                }
338
            } else {
339 2
                $saved = true;
340
            }
341
        }
342
343
        // If the model is brand new, we'll insert it into our database and set the
344
        // ID attribute on the model to the value of the newly inserted row's ID
345
        // which is typically an auto-increment value managed by the database.
346
        else {
347 13
            $saved = $this->performInsert($query);
0 ignored issues
show
Bug introduced by
It seems like $query defined by $this->newQueryWithoutScopes() on line 281 can also be of type object<Illuminate\Database\Eloquent\Model>; however, Illuminate\Database\Eloq...\Model::performInsert() does only seem to accept object<Illuminate\Database\Eloquent\Builder>, 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...
348
349 13
            if (!$this->getConnectionName() &&
350 13
                $connection = $query->getConnection()
0 ignored issues
show
Bug introduced by
The method getConnection does only exist in Illuminate\Database\Eloquent\Model, but not in Illuminate\Database\Eloquent\Builder.

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...
351
            ) {
352
                $this->setConnection($connection->getName());
353
            }
354
        }
355
356
        // If the model is successfully saved, we need to do a few more things once
357
        // that is done. We will call the "saved" method here to run any actions
358
        // we need to happen after a model gets successfully saved right here.
359 13
        if ($saved) {
360 13
            $this->finishSave($options);
361
        }
362
363 13
        return $saved;
364
    }
365
366
}
367