Passed
Push — master ( f2dea1...58a092 )
by Nate
04:47
created

Object   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 286
Duplicated Lines 14.69 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 6
dl 42
loc 286
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
recordClass() 0 1 ?
A recordClassInstance() 0 4 1
A getRecordQuery() 0 20 2
A createRecord() 0 22 3
A findRecordByCondition() 0 13 2
A findRecordByCriteria() 18 18 3
A getRecordByCondition() 0 12 2
A getRecordByCriteria() 0 12 2
A findAllRecords() 0 4 1
A findAllRecordsByCondition() 0 9 1
B findAllRecordsByCriteria() 24 24 3
A getAllRecords() 0 11 1
A getAllRecordsByCondition() 0 12 2
A getAllRecordsByCriteria() 0 12 2
A notFoundRecordException() 0 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/spark/blob/master/LICENSE
6
 * @link       https://github.com/flipbox/spark
7
 */
8
9
namespace flipbox\spark\services\traits;
10
11
use Craft;
12
use flipbox\spark\exceptions\RecordNotFoundException;
13
use flipbox\spark\helpers\QueryHelper;
14
use flipbox\spark\helpers\RecordHelper;
15
use flipbox\spark\records\Record;
16
use yii\db\ActiveQuery;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.1.0
21
 */
22
trait Object
23
{
24
25
    /**
26
     * @return string
27
     */
28
    public abstract static function recordClass(): string;
29
30
    /**
31
     * @return string
32
     */
33
    public static function recordClassInstance(): string
34
    {
35
        return Record::class;
36
    }
37
38
    /**
39
     * @param array $config
40
     * @return \yii\db\ActiveQuery
41
     */
42
    public function getRecordQuery($config = []): ActiveQuery
43
    {
44
45
        /** @var Record $recordClass */
46
        $recordClass = $this->recordClass();
47
48
        $query = $recordClass::find();
49
50
        if ($config) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $config of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
51
52
            QueryHelper::configure(
53
                $query,
54
                $config
55
            );
56
57
        }
58
59
        return $query;
60
61
    }
62
63
    /*******************************************
64
     * CREATE
65
     *******************************************/
66
67
    /**
68
     * @param array $attributes
69
     * @param string $toScenario
70
     * @return Record
71
     */
72
    public function createRecord(array $attributes = [], string $toScenario = null)
73
    {
74
75
        /** @var string $recordClass */
76
        $recordClass = static::recordClass();
77
78
        /** @var Record $record */
79
        $record = new $recordClass();
80
81
        // Set scenario
82
        if ($toScenario) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $toScenario of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
83
            $record->setScenario($toScenario);
84
        }
85
86
        // Do we need to set properties too
87
        if (!empty($attributes)) {
88
            $record->setAttributes($attributes);
89
        }
90
91
        return $record;
92
93
    }
94
95
    /**
96
     * @param $condition
97
     * @param string $toScenario
98
     * @return Record|null
99
     */
100
    public function findRecordByCondition($condition, string $toScenario = null)
101
    {
102
103
        if (empty($condition)) {
104
            return null;
105
        }
106
107
        return $this->findRecordByCriteria(
108
            RecordHelper::conditionToCriteria($condition),
109
            $toScenario
110
        );
111
112
    }
113
114
    /**
115
     * @param $criteria
116
     * @param string $toScenario
117
     * @return Record
118
     */
119 View Code Duplication
    public function findRecordByCriteria($criteria, string $toScenario = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
    {
121
122
        $query = $this->getRecordQuery($criteria);
123
124
        /** @var Record $record */
125
        if ($record = $query->one()) {
126
127
            // Set scenario
128
            if ($toScenario) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $toScenario of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
129
                $record->setScenario($toScenario);
130
            }
131
132
        }
133
134
        return $record;
135
136
    }
137
138
    /**
139
     * @param $condition
140
     * @param string $toScenario
141
     * @return Record
142
     * @throws RecordNotFoundException
143
     */
144
    public function getRecordByCondition($condition, string $toScenario = null)
145
    {
146
147
        if (!$record = $this->findRecordByCondition($condition, $toScenario)) {
148
149
            $this->notFoundRecordException();
150
151
        }
152
153
        return $record;
154
155
    }
156
157
    /**
158
     * @param $criteria
159
     * @param string $toScenario
160
     * @return Record
161
     * @throws RecordNotFoundException
162
     */
163
    public function getRecordByCriteria($criteria, string $toScenario = null)
164
    {
165
166
        if (!$record = $this->findRecordByCriteria($criteria, $toScenario)) {
167
168
            $this->notFoundRecordException();
169
170
        }
171
172
        return $record;
173
174
    }
175
176
177
    /**
178
     * @param string $toScenario
179
     * @return Record[]
180
     */
181
    public function findAllRecords(string $toScenario = null)
182
    {
183
        return $this->findAllRecordsByCondition(null, $toScenario);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

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...
184
    }
185
186
    /**
187
     * @param array $condition
188
     * @param string $toScenario
189
     * @return Record[]
190
     */
191
    public function findAllRecordsByCondition($condition = [], string $toScenario = null)
192
    {
193
194
        return $this->findAllRecordsByCriteria(
195
            RecordHelper::conditionToCriteria($condition),
196
            $toScenario
197
        );
198
199
    }
200
201
    /**
202
     * @param array $criteria
203
     * @param string $toScenario
204
     * @return Record[]
205
     */
206 View Code Duplication
    public function findAllRecordsByCriteria($criteria = [], string $toScenario = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
207
    {
208
209
        $query = $this->getRecordQuery($criteria);
210
211
        /** @var Record[] $record s */
212
        $records = $query->all();
213
214
        // Set scenario
215
        if ($toScenario) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $toScenario of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
216
217
            /** @var Record $record */
218
            foreach ($records as $record) {
219
220
                // Set scenario
221
                $record->setScenario($toScenario);
222
223
            }
224
225
        }
226
227
        return $records;
228
229
    }
230
231
232
    /**
233
     * @deprecated
234
     * @param array $condition
235
     * @param string $toScenario
236
     * @return Record[]
237
     * @throws RecordNotFoundException
238
     */
239
    public function getAllRecords($condition = [], string $toScenario = null)
240
    {
241
242
        Craft::$app->getDeprecator()->log(
243
            __METHOD__,
244
            'Use the "getAllRecordsByCondition" method'
245
        );
246
247
        return $this->getAllRecordsByCondition($condition, $toScenario);
248
249
    }
250
251
    /**
252
     * @param array $condition
253
     * @param string $toScenario
254
     * @return Record[]
255
     * @throws RecordNotFoundException
256
     */
257
    public function getAllRecordsByCondition($condition = [], string $toScenario = null)
258
    {
259
260
        if (!$records = $this->findAllRecordsByCondition($condition, $toScenario)) {
261
262
            $this->notFoundRecordException();
263
264
        }
265
266
        return $records;
267
268
    }
269
270
    /**
271
     * @param array $criteria
272
     * @param string $toScenario
273
     * @return Record[]
274
     * @throws RecordNotFoundException
275
     */
276
    public function getAllRecordsByCriteria($criteria = [], string $toScenario = null)
277
    {
278
279
        if (!$records = $this->findAllRecordsByCriteria($criteria, $toScenario)) {
280
281
            $this->notFoundRecordException();
282
283
        }
284
285
        return $records;
286
287
    }
288
289
    /*******************************************
290
     * EXCEPTIONS
291
     *******************************************/
292
293
    /**
294
     * @throws RecordNotFoundException
295
     */
296
    protected function notFoundRecordException()
297
    {
298
299
        throw new RecordNotFoundException(
300
            sprintf(
301
                "Record does not exist."
302
            )
303
        );
304
305
    }
306
307
}
308