Passed
Pull Request — master (#40)
by Giacomo
04:42
created

Builder::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 11
rs 10
1
<?php
2
3
namespace OfflineAgency\MongoAutoSync\Eloquent;
4
5
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
6
use Jenssegers\Mongodb\Eloquent\Builder as MongoDbEloquentBuilder;
7
8
class Builder extends MongoDbEloquentBuilder
9
{
10
    /**
11
     * {@inheritdoc}
12
     */
13
    public function update(array $values, array $options = [])
14
    {
15
        // Intercept operations on embedded models and delegate logic
16
        // to the parent relation instance.
17
        if ($relation = $this->model->getParentRelation()) {
18
            $relation->performUpdate($this->model, $values);
19
20
            return 1;
21
        }
22
23
        return $this->toBase()->update($this->addUpdatedAtColumn($values), $options);
0 ignored issues
show
Unused Code introduced by
The call to Illuminate\Database\Query\Builder::update() has too many arguments starting with $options. ( Ignorable by Annotation )

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

23
        return $this->toBase()->/** @scrutinizer ignore-call */ update($this->addUpdatedAtColumn($values), $options);

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...
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function insert(array $values)
30
    {
31
        // Intercept operations on embedded models and delegate logic
32
        // to the parent relation instance.
33
        if ($relation = $this->model->getParentRelation()) {
34
            $relation->performInsert($this->model, $values);
35
36
            return true;
37
        }
38
39
        return EloquentBuilder::insert($values);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function insertGetId(array $values, $sequence = null)
46
    {
47
        // Intercept operations on embedded models and delegate logic
48
        // to the parent relation instance.
49
        if ($relation = $this->model->getParentRelation()) {
50
            $relation->performInsert($this->model, $values);
51
52
            return $this->model->getKey();
53
        }
54
55
        return EloquentBuilder::insertGetId($values, $sequence);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function delete()
62
    {
63
        // Intercept operations on embedded models and delegate logic
64
        // to the parent relation instance.
65
        if ($relation = $this->model->getParentRelation()) {
66
            $relation->performDelete($this->model);
67
68
            return $this->model->getKey();
69
        }
70
71
        return EloquentBuilder::delete();
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function increment($column, $amount = 1, array $extra = [])
78
    {
79
        // Intercept operations on embedded models and delegate logic
80
        // to the parent relation instance.
81
        if ($relation = $this->model->getParentRelation()) {
0 ignored issues
show
Unused Code introduced by
The assignment to $relation is dead and can be removed.
Loading history...
82
            $value = $this->model->{$column};
83
84
            // When doing increment and decrements, Eloquent will automatically
85
            // sync the original attributes. We need to change the attribute
86
            // temporary in order to trigger an update query.
87
            $this->model->{$column} = null;
88
89
            $this->model->syncOriginalAttribute($column);
90
91
            $result = $this->model->update([$column => $value]);
92
93
            return $result;
94
        }
95
96
        return EloquentBuilder::increment($column, $amount, $extra);
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function decrement($column, $amount = 1, array $extra = [])
103
    {
104
        // Intercept operations on embedded models and delegate logic
105
        // to the parent relation instance.
106
        if ($relation = $this->model->getParentRelation()) {
0 ignored issues
show
Unused Code introduced by
The assignment to $relation is dead and can be removed.
Loading history...
107
            $value = $this->model->{$column};
108
109
            // When doing increment and decrements, Eloquent will automatically
110
            // sync the original attributes. We need to change the attribute
111
            // temporary in order to trigger an update query.
112
            $this->model->{$column} = null;
113
114
            $this->model->syncOriginalAttribute($column);
115
116
            return $this->model->update([$column => $value]);
117
        }
118
119
        return EloquentBuilder::decrement($column, $amount, $extra);
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function chunkById($count, callable $callback, $column = '_id', $alias = null)
126
    {
127
        return EloquentBuilder::chunkById($count, $callback, $column, $alias);
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function raw($expression = null)
134
    {
135
        // Get raw results from the query builder.
136
        $results = $this->query->raw($expression);
137
138
        // Convert MongoCursor results to a collection of models.
139
        if ($results instanceof Cursor) {
0 ignored issues
show
Bug introduced by
The type OfflineAgency\MongoAutoSync\Eloquent\Cursor was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
140
            $results = iterator_to_array($results, false);
141
142
            return $this->model->hydrate($results);
143
        } // Convert Mongo BSONDocument to a single object.
144
        elseif ($results instanceof BSONDocument) {
0 ignored issues
show
Bug introduced by
The type OfflineAgency\MongoAutoSync\Eloquent\BSONDocument was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
145
            $results = $results->getArrayCopy();
0 ignored issues
show
Bug introduced by
The method getArrayCopy() does not exist on Illuminate\Database\Query\Expression. ( Ignorable by Annotation )

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

145
            /** @scrutinizer ignore-call */ 
146
            $results = $results->getArrayCopy();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
146
147
            return $this->model->newFromBuilder((array) $results);
148
        } // The result is a single object.
149
        elseif (is_array($results) && array_key_exists('_id', $results)) {
0 ignored issues
show
introduced by
The condition is_array($results) is always false.
Loading history...
150
            return $this->model->newFromBuilder((array) $results);
151
        }
152
153
        return $results;
154
    }
155
156
    /**
157
     * Add the "updated at" column to an array of values.
158
     * TODO Remove if https://github.com/laravel/framework/commit/6484744326531829341e1ff886cc9b628b20d73e
159
     * wiil be reverted
160
     * Issue in laravel frawework https://github.com/laravel/framework/issues/27791.
161
     * @param array $values
162
     * @return array
163
     */
164
    protected function addUpdatedAtColumn(array $values)
165
    {
166
        if (! $this->model->usesTimestamps() || $this->model->getUpdatedAtColumn() === null) {
167
            return $values;
168
        }
169
170
        $column = $this->model->getUpdatedAtColumn();
171
        $values = array_merge(
172
            [$column => $this->model->freshTimestampString()],
173
            $values
174
        );
175
176
        return $values;
177
    }
178
}
179