Completed
Push — master ( b96d89...96506e )
by max
03:28
created

InMemoryCriteria   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 336
Duplicated Lines 9.52 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 44
lcom 1
cbo 0
dl 32
loc 336
rs 8.3396
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getQuery() 0 13 3
A getEntityName() 0 4 1
A relation() 0 5 1
A equalTo() 0 16 3
A notEqualTo() 0 16 3
A lessThan() 0 16 3
A greaterThan() 0 16 3
A greaterThanOrEqualTo() 0 16 3
A lessThanOrEqualTo() 0 16 3
A like() 16 16 3
A isNull() 0 16 3
A isNotNull() 0 16 3
A in() 16 16 3
A between() 0 16 4
A order() 0 5 1
A limit() 0 5 1
A offset() 0 5 1
A getField() 0 4 1

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like InMemoryCriteria often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use InMemoryCriteria, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace T4webInfrastructure;
4
5
use T4webDomainInterface\Infrastructure\CriteriaInterface;
6
7
class InMemoryCriteria implements CriteriaInterface
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $entityName;
13
14
    /**
15
     * @var array
16
     */
17
    protected $callbacks;
18
19
    /**
20
     * @param string $entityName
21
     */
22
    public function __construct($entityName)
23
    {
24
        $this->entityName = $entityName;
25
        $this->callbacks = [];
26
    }
27
28
    /**
29
     * @return mixed
30
     */
31
    public function getQuery()
32
    {
33
        $callbacks = $this->callbacks;
34
        return function(array $data) use ($callbacks) {
35
            foreach ($callbacks as $callback) {
36
                if (!$callback($data)) {
37
                    return false;
38
                }
39
            }
40
41
            return true;
42
        };
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function getEntityName()
49
    {
50
        return $this->entityName;
51
    }
52
53
    /**
54
     * @param string $entityName
55
     * @return CriteriaInterface
56
     */
57
    public function relation($entityName)
58
    {
59
        // not implemented
60
        return $this;
61
    }
62
63
    /**
64
     * @param string $attribute
65
     * @param bool|int|float|string $value
66
     * @return $this
67
     */
68
    public function equalTo($attribute, $value)
69
    {
70
        $this->callbacks[] = function(array $data) use ($attribute, $value) {
71
            if (!array_key_exists($attribute, $data)) {
72
                return false;
73
            }
74
75
            if ($data[$attribute] == $value) {
76
                return true;
77
            }
78
79
            return false;
80
        };
81
82
        return $this;
83
    }
84
85
    /**
86
     * @param string $attribute
87
     * @param bool|int|float|string $value
88
     * @return $this
89
     */
90
    public function notEqualTo($attribute, $value)
91
    {
92
        $this->callbacks[] = function(array $data) use ($attribute, $value) {
93
            if (!array_key_exists($attribute, $data)) {
94
                return false;
95
            }
96
97
            if ($data[$attribute] != $value) {
98
                return true;
99
            }
100
101
            return false;
102
        };
103
104
        return $this;
105
    }
106
107
    /**
108
     * @param string $attribute
109
     * @param int|float $value
110
     * @return $this
111
     */
112
    public function lessThan($attribute, $value)
113
    {
114
        $this->callbacks[] = function(array $data) use ($attribute, $value) {
115
            if (!array_key_exists($attribute, $data)) {
116
                return false;
117
            }
118
119
            if ($data[$attribute] < $value) {
120
                return true;
121
            }
122
123
            return false;
124
        };
125
126
        return $this;
127
    }
128
129
    /**
130
     * @param string $attribute
131
     * @param int|float $value
132
     * @return $this
133
     */
134
    public function greaterThan($attribute, $value)
135
    {
136
        $this->callbacks[] = function(array $data) use ($attribute, $value) {
137
            if (!array_key_exists($attribute, $data)) {
138
                return false;
139
            }
140
141
            if ($data[$attribute] > $value) {
142
                return true;
143
            }
144
145
            return false;
146
        };
147
148
        return $this;
149
    }
150
151
    /**
152
     * @param string $attribute
153
     * @param int|float $value
154
     * @return $this
155
     */
156
    public function greaterThanOrEqualTo($attribute, $value)
157
    {
158
        $this->callbacks[] = function(array $data) use ($attribute, $value) {
159
            if (!array_key_exists($attribute, $data)) {
160
                return false;
161
            }
162
163
            if ($data[$attribute] >= $value) {
164
                return true;
165
            }
166
167
            return false;
168
        };
169
170
        return $this;
171
    }
172
173
    /**
174
     * @param string $attribute
175
     * @param int|float $value
176
     * @return $this
177
     */
178
    public function lessThanOrEqualTo($attribute, $value)
179
    {
180
        $this->callbacks[] = function(array $data) use ($attribute, $value) {
181
            if (!array_key_exists($attribute, $data)) {
182
                return false;
183
            }
184
185
            if ($data[$attribute] <= $value) {
186
                return true;
187
            }
188
189
            return false;
190
        };
191
192
        return $this;
193
    }
194
195
    /**
196
     * @param string $attribute
197
     * @param int|float $value
198
     * @return $this
199
     */
200 View Code Duplication
    public function like($attribute, $value)
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...
201
    {
202
        $this->callbacks[] = function(array $data) use ($attribute, $value) {
203
            if (!array_key_exists($attribute, $data)) {
204
                return false;
205
            }
206
207
            if (strpos($data[$attribute], $value) !== false) {
208
                return true;
209
            }
210
211
            return false;
212
        };
213
214
        return $this;
215
    }
216
217
    /**
218
     * @param string $attribute
219
     * @return $this
220
     */
221
    public function isNull($attribute)
222
    {
223
        $this->callbacks[] = function(array $data) use ($attribute) {
224
            if (!array_key_exists($attribute, $data)) {
225
                return false;
226
            }
227
228
            if (is_null($data[$attribute])) {
229
                return true;
230
            }
231
232
            return false;
233
        };
234
235
        return $this;
236
    }
237
238
    /**
239
     * @param string $attribute
240
     * @return $this
241
     */
242
    public function isNotNull($attribute)
243
    {
244
        $this->callbacks[] = function(array $data) use ($attribute) {
245
            if (!array_key_exists($attribute, $data)) {
246
                return false;
247
            }
248
249
            if (!is_null($data[$attribute])) {
250
                return true;
251
            }
252
253
            return false;
254
        };
255
256
        return $this;
257
    }
258
259
    /**
260
     * @param string $attribute
261
     * @param array $values
262
     * @return $this
263
     */
264 View Code Duplication
    public function in($attribute, array $values)
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...
265
    {
266
        $this->callbacks[] = function(array $data) use ($attribute, $values) {
267
            if (!array_key_exists($attribute, $data)) {
268
                return false;
269
            }
270
271
            if (in_array($data[$attribute], $values)) {
272
                return true;
273
            }
274
275
            return false;
276
        };
277
278
        return $this;
279
    }
280
281
    /**
282
     * @param string $attribute
283
     * @param int|float|string $minValue
284
     * @param int|float|string $maxValue
285
     * @return $this
286
     */
287
    public function between($attribute, $minValue, $maxValue)
288
    {
289
        $this->callbacks[] = function(array $data) use ($attribute, $minValue, $maxValue) {
290
            if (!array_key_exists($attribute, $data)) {
291
                return false;
292
            }
293
            
294
            if ($data[$attribute] >= $minValue && $data[$attribute] <= $maxValue) {
295
                return true;
296
            }
297
298
            return false;
299
        };
300
301
        return $this;
302
    }
303
304
    /**
305
     * @param string $attribute
306
     * @return $this
307
     */
308
    public function order($attribute)
309
    {
310
        // not implemented
311
        return $this;
312
    }
313
314
    /**
315
     * @param int $limit
316
     * @return $this
317
     */
318
    public function limit($limit)
319
    {
320
        // not implemented
321
        return $this;
322
    }
323
324
    /**
325
     * @param int $offset
326
     * @return $this
327
     */
328
    public function offset($offset)
329
    {
330
        // not implemented
331
        return $this;
332
    }
333
334
    /**
335
     * @param $attribute
336
     * @return string
337
     */
338
    public function getField($attribute)
339
    {
340
        return $attribute;
341
    }
342
}
343