Passed
Pull Request — master (#17)
by Orkhan
03:43
created

UpdatesEntity::findAndUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Orkhanahmadov\EloquentRepository\Repository\Concerns;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Builder;
7
use Orkhanahmadov\EloquentRepository\Repository\Contracts\Cacheable;
8
9
/**
10
 * @method Builder|Model find(int $modelId)
11
 * @method void invalidateCache()
12
 */
13
trait UpdatesEntity
14
{
15
    /**
16
     * Finds a model with ID and updates it with given properties.
17
     *
18
     * @param int|string $modelId
19
     * @param mixed $properties
20
     *
21
     * @return Builder|Model
22
     */
23
    public function findAndUpdate($modelId, $properties)
24
    {
25
        $model = $this->find($modelId);
0 ignored issues
show
Bug introduced by
It seems like $modelId can also be of type string; however, parameter $modelId of Orkhanahmadov\EloquentRe...s\UpdatesEntity::find() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
        $model = $this->find(/** @scrutinizer ignore-type */ $modelId);
Loading history...
26
27
        return $this->update($model, $properties);
0 ignored issues
show
Bug introduced by
It seems like $model can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $model of Orkhanahmadov\EloquentRe...UpdatesEntity::update() does only seem to accept Illuminate\Database\Eloquent\Model, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

27
        return $this->update(/** @scrutinizer ignore-type */ $model, $properties);
Loading history...
28
    }
29
30
    /**
31
     * Updates a model given properties.
32
     *
33
     * @param Model $model
34
     * @param mixed $properties
35
     *
36
     * @return Builder|Model
37
     */
38
    public function update($model, $properties)
39
    {
40
        if ($this instanceof Cacheable) {
41
            $this->invalidateCache($model);
0 ignored issues
show
Unused Code introduced by
The call to Orkhanahmadov\EloquentRe...tity::invalidateCache() has too many arguments starting with $model. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
            $this->/** @scrutinizer ignore-call */ 
42
                   invalidateCache($model);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
42
        }
43
44
        $model->fill($properties)->save();
45
46
        return $model->refresh();
47
    }
48
}
49