Test Setup Failed
Push — master ( d6051c...9048ee )
by Roman
02:37
created

Model::freshTimestamp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 58
     * @var array
45
     */
46 58
    protected $primaryColumns = [];
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function newEloquentBuilder($query)
52 58
    {
53
        return new Builder($query);
54 58
    }
55
56 58
    /**
57
     * @inheritdoc
58
     */
59
    protected function newBaseQueryBuilder()
60
    {
61
        $connection = $this->getConnection();
62 13
63
        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 13
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69
    public function freshTimestamp()
70 13
    {
71
        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 13
74 13
    /**
75
     * @inheritdoc
76
     */
77
    public function fromDateTime($value)
78 1
    {
79 1
        // If the value is already a Timestamp instance, we don't need to parse it.
80
        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
            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 1
        }
83
84
        // Let Eloquent convert the value to a DateTime instance.
85
        if (!$value instanceof \DateTime) {
86
            $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 8
89
        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 8
92 8
    /**
93
     * @inheritdoc
94
     */
95
    protected function asDateTime($value)
96
    {
97
        // Convert UTCDateTime instances.
98
        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
            return Carbon::instance($value->toDateTime());
100
        }
101
102
        return parent::asDateTime($value);
103
    }
104
105 8
    /**
106
     * Get the table qualified key name.
107 8
     * Cassandra does not support the table.column annotation so
108
     * we override this
109
     *
110
     * @return string
111
     */
112
    public function getQualifiedKeyName()
113
    {
114
        return $this->getKeyName();
115
    }
116 2
117
    /**
118 2
     * Qualify the given column name by the model's table.
119
     *
120
     * @param string $column
121
     * @return string
122
     */
123
    public function qualifyColumn($column)
124 58
    {
125
        return $column;
126
    }
127 58
128
    /**
129
     * @inheritdoc
130
     */
131 58
    public function __call($method, $parameters)
132
    {
133
        // Unset method
134
        if ($method == 'unset') {
135
            return call_user_func_array([$this, 'drop'], $parameters);
136
        }
137
138
        return parent::__call($method, $parameters);
139
    }
140
141
    /**
142
     * Create a new Eloquent Collection instance.
143 53
     *
144
     * @param Rows|array $rows
145 53
     *
146
     * @return Collection
147
     *
148
     * @throws \Exception
149 53
     */
150 53
    public function newCassandraCollection($rows)
151 50
    {
152
        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 53
        }
155
156 53
        $items = [];
157 19
        foreach ($rows as $row) {
158
            $items[] = $this->newFromBuilder($row);
159
        }
160 53
161
        $collection = new Collection($items);
162
163
        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
            $collection->setRowsInstance($rows);
165
        }
166
167
        return $collection;
168
    }
169
170
    /**
171
     * Determine if the new and old values for a given key are equivalent.
172
     *
173 13
     * @param string $key
174
     * @param mixed $current
175 13
     *
176 13
     * @return bool
177
     *
178
     * @throws \Exception
179 2
     */
180
    public function originalIsEquivalent($key, $current)
181 2
    {
182 2
        if (!array_key_exists($key, $this->original)) {
183 2
            return false;
184
        }
185 2
186 2
        $original = $this->getOriginal($key);
187 2
188 1
        if ($current === $original) {
189
            return true;
190
        } elseif (is_null($current)) {
191 1
            return false;
192
        } elseif ($this->isDateAttribute($key)) {
193
            return $this->fromDateTime($current) ===
194
                $this->fromDateTime($original);
195
        } elseif ($this->hasCast($key)) {
196 1
            return $this->castAttribute($key, $current) ===
197 1
                $this->castAttribute($key, $original);
198
        } elseif ($this->isCassandraValueObject($current)) {
199
            return $this->valueFromCassandraObject($current) ===
200
                $this->valueFromCassandraObject($original);
201
        }
202
203
        return is_numeric($current) && is_numeric($original)
204
            && strcmp((string)$current, (string)$original) === 0;
205 16
    }
206
207 16
    /**
208
     * Get the value of the model's primary key.
209 16
     *
210
     * @return mixed
211
     */
212
    public function getKey()
213 16
    {
214
        $value = $this->getAttribute($this->getKeyName());
215
216
        if ($this->isCassandraValueObject($value)) {
217
            return $this->valueFromCassandraObject($this->getAttribute($this->getKeyName()));
218
        }
219
220
        return $value;
221
    }
222
223
    /**
224
     * Cast an attribute to a native PHP type.
225
     *
226 3
     * @param string $key
227
     * @param mixed $value
228 3
     *
229 1
     * @return mixed
230 1
     *
231 1
     * @throws \Exception
232
     */
233
    protected function castAttribute($key, $value)
234 2
    {
235
        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
            return (new \DateTime('today', new \DateTimeZone("+0")))
237
                ->modify('+' . $value->seconds() . ' seconds')
238 2
                ->format($this->getTimeFormat());
239
        }
240
241
        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
        return parent::castAttribute($key, $value);
246 1
    }
247
248 1
    /**
249
     * Get the format for time stored in database.
250
     *
251
     * @return string
252
     */
253
    public function getTimeFormat()
254
    {
255
        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
    public function save(array $options = [])
280
    {
281
        $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
        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
        if ($this->exists) {
294
            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
                $dirtyKeys = array_keys($this->getDirty());
299
                $dirtyPrimaryKeys = array_intersect($this->primaryColumns, $dirtyKeys);
300
                $dirtyPrimaryExists = count($dirtyPrimaryKeys) > 0;
301
302
                // Check if any of primary key columns is dirty
303
                if (!$dirtyPrimaryExists) {
304
                    $primaryColumnsExceptId = array_diff($this->primaryColumns, [$this->primaryKey]);
305
306
                    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
                    $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
                    $this->fireModelEvent('updated');
337
                }
338
            } else {
339
                $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
            $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
            if (!$this->getConnectionName() &&
350
                $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
        if ($saved) {
360
            $this->finishSave($options);
361
        }
362
363
        return $saved;
364
    }
365
366
}
367