Completed
Push — master ( d93af0...970486 )
by Peter
05:51
created

Spec   D

Complexity

Total Complexity 57

Size/Duplication

Total Lines 648
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 45

Importance

Changes 0
Metric Value
wmc 57
lcom 0
cbo 45
dl 0
loc 648
rs 4.992
c 0
b 0
f 0

54 Methods

Rating   Name   Duplication   Size   Complexity  
A __callStatic() 0 12 3
A andX() 0 7 1
A orX() 0 7 1
A not() 0 4 1
A join() 0 4 1
A leftJoin() 0 4 1
A innerJoin() 0 4 1
A limit() 0 4 1
A offset() 0 4 1
A slice() 0 4 1
A orderBy() 0 4 1
A groupBy() 0 4 1
A select() 0 4 1
A addSelect() 0 4 1
A selectEntity() 0 4 1
A selectAs() 0 4 1
A selectHiddenAs() 0 4 1
A asArray() 0 4 1
A asSingleScalar() 0 4 1
A asScalar() 0 4 1
A cache() 0 4 1
A roundDateTimeParams() 0 4 1
A isNull() 0 4 1
A isNotNull() 0 4 1
A in() 0 4 1
A notIn() 0 4 1
A eq() 0 4 1
A neq() 0 4 1
A lt() 0 4 1
A lte() 0 4 1
A gt() 0 4 1
A gte() 0 4 1
A like() 0 4 1
A instanceOfX() 0 4 1
A memberOfX() 0 4 1
A countOf() 0 4 1
A having() 0 4 1
A field() 0 4 1
A value() 0 4 1
A values() 0 4 1
A likePattern() 0 4 1
A add() 0 4 1
A sub() 0 4 1
A mul() 0 4 1
A div() 0 4 1
A mod() 0 4 1
A bAnd() 0 4 1
A bOr() 0 4 1
A bXor() 0 4 1
A bLs() 0 4 1
A bRs() 0 4 1
A bNot() 0 4 1
A fun() 0 11 2
A alias() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like Spec 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 Spec, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Happyr\DoctrineSpecification;
4
5
use Happyr\DoctrineSpecification\Filter\Comparison;
6
use Happyr\DoctrineSpecification\Filter\Filter;
7
use Happyr\DoctrineSpecification\Filter\In;
8
use Happyr\DoctrineSpecification\Filter\InstanceOfX;
9
use Happyr\DoctrineSpecification\Filter\IsNotNull;
10
use Happyr\DoctrineSpecification\Filter\IsNull;
11
use Happyr\DoctrineSpecification\Filter\Like;
12
use Happyr\DoctrineSpecification\Filter\MemberOfX;
13
use Happyr\DoctrineSpecification\Logic\AndX;
14
use Happyr\DoctrineSpecification\Logic\Not;
15
use Happyr\DoctrineSpecification\Logic\OrX;
16
use Happyr\DoctrineSpecification\Operand\Addition;
17
use Happyr\DoctrineSpecification\Operand\Alias;
18
use Happyr\DoctrineSpecification\Operand\BitAnd;
19
use Happyr\DoctrineSpecification\Operand\BitLeftShift;
20
use Happyr\DoctrineSpecification\Operand\BitNot;
21
use Happyr\DoctrineSpecification\Operand\BitOr;
22
use Happyr\DoctrineSpecification\Operand\BitRightShift;
23
use Happyr\DoctrineSpecification\Operand\BitXor;
24
use Happyr\DoctrineSpecification\Operand\Division;
25
use Happyr\DoctrineSpecification\Operand\Field;
26
use Happyr\DoctrineSpecification\Operand\LikePattern;
27
use Happyr\DoctrineSpecification\Operand\Modulo;
28
use Happyr\DoctrineSpecification\Operand\Multiplication;
29
use Happyr\DoctrineSpecification\Operand\Operand;
30
use Happyr\DoctrineSpecification\Operand\Subtraction;
31
use Happyr\DoctrineSpecification\Operand\PlatformFunction;
32
use Happyr\DoctrineSpecification\Operand\Value;
33
use Happyr\DoctrineSpecification\Operand\Values;
34
use Happyr\DoctrineSpecification\Query\AddSelect;
35
use Happyr\DoctrineSpecification\Query\GroupBy;
36
use Happyr\DoctrineSpecification\Query\InnerJoin;
37
use Happyr\DoctrineSpecification\Query\Join;
38
use Happyr\DoctrineSpecification\Query\LeftJoin;
39
use Happyr\DoctrineSpecification\Query\Limit;
40
use Happyr\DoctrineSpecification\Query\Offset;
41
use Happyr\DoctrineSpecification\Query\OrderBy;
42
use Happyr\DoctrineSpecification\Query\QueryModifier;
43
use Happyr\DoctrineSpecification\Query\Select;
44
use Happyr\DoctrineSpecification\Query\Selection\SelectAs;
45
use Happyr\DoctrineSpecification\Query\Selection\SelectEntity;
46
use Happyr\DoctrineSpecification\Query\Selection\SelectHiddenAs;
47
use Happyr\DoctrineSpecification\Query\Slice;
48
use Happyr\DoctrineSpecification\Result\AsArray;
49
use Happyr\DoctrineSpecification\Result\AsScalar;
50
use Happyr\DoctrineSpecification\Result\AsSingleScalar;
51
use Happyr\DoctrineSpecification\Result\Cache;
52
use Happyr\DoctrineSpecification\Result\RoundDateTime;
53
use Happyr\DoctrineSpecification\Specification\CountOf;
54
use Happyr\DoctrineSpecification\Specification\Having;
55
56
/**
57
 * Factory class for the specifications.
58
 *
59
 * @method static PlatformFunction CONCAT($str1, $str2)
60
 * @method static PlatformFunction SUBSTRING($str, $start, $length = null) Return substring of given string.
61
 * @method static PlatformFunction TRIM($str) Trim the string by the given trim char, defaults to whitespaces.
62
 * @method static PlatformFunction LOWER($str) Returns the string lowercased.
63
 * @method static PlatformFunction UPPER($str) Return the upper-case of the given string.
64
 * @method static PlatformFunction IDENTITY($expression, $fieldMapping = null) Retrieve the foreign key column of association of the owning side
65
 * @method static PlatformFunction LENGTH($str) Returns the length of the given string
66
 * @method static PlatformFunction LOCATE($needle, $haystack, $offset = 0) Locate the first occurrence of the substring in the string.
67
 * @method static PlatformFunction ABS($expression)
68
 * @method static PlatformFunction SQRT($q) Return the square-root of q.
69
 * @method static PlatformFunction MOD($a, $b) Return a MOD b.
70
 * @method static PlatformFunction SIZE($collection) Return the number of elements in the specified collection
71
 * @method static PlatformFunction DATE_DIFF($date1, $date2) Calculate the difference in days between date1-date2.
72
 * @method static PlatformFunction BIT_AND($a, $b)
73
 * @method static PlatformFunction BIT_OR($a, $b)
74
 * @method static PlatformFunction MIN($a)
75
 * @method static PlatformFunction MAX($a)
76
 * @method static PlatformFunction AVG($a)
77
 * @method static PlatformFunction SUM($a)
78
 * @method static PlatformFunction COUNT($a)
79
 * @method static PlatformFunction CURRENT_DATE() Return the current date
80
 * @method static PlatformFunction CURRENT_TIME() Returns the current time
81
 * @method static PlatformFunction CURRENT_TIMESTAMP() Returns a timestamp of the current date and time.
82
 * @method static PlatformFunction DATE_ADD($date, $days, $unit) Add the number of days to a given date. (Supported units are DAY, MONTH)
83
 * @method static PlatformFunction DATE_SUB($date, $days, $unit) Substract the number of days from a given date. (Supported units are DAY, MONTH)
84
 */
85
class Spec
86
{
87
    /**
88
     * @param string $name
89
     * @param array  $arguments
90
     *
91
     * @return PlatformFunction
92
     */
93
    public static function __callStatic($name, array $arguments = [])
94
    {
95
        // allow use array in arguments of static function
96
        // Spec::DATE_DIFF([$date1, $date2]);
97
        // is equal
98
        // Spec::DATE_DIFF($date1, $date2);
99
        if (count($arguments) === 1 && is_array(current($arguments))) {
100
            $arguments = current($arguments);
101
        }
102
103
        return self::fun($name, $arguments);
104
    }
105
106
    /*
107
     * Logic
108
     */
109
110
    /**
111
     * @return AndX
112
     */
113
    public static function andX()
114
    {
115
        $args = func_get_args();
116
        $reflection = new \ReflectionClass(AndX::class);
117
118
        return $reflection->newInstanceArgs($args);
119
    }
120
121
    /**
122
     * @return OrX
123
     */
124
    public static function orX()
125
    {
126
        $args = func_get_args();
127
        $reflection = new \ReflectionClass(OrX::class);
128
129
        return $reflection->newInstanceArgs($args);
130
    }
131
132
    /**
133
     * @param Filter $spec
134
     *
135
     * @return Not
136
     */
137
    public static function not(Filter $spec)
138
    {
139
        return new Not($spec);
140
    }
141
142
    /*
143
     * Query modifier
144
     */
145
146
    /**
147
     * @param string $field
148
     * @param string $newAlias
149
     * @param string $dqlAlias
150
     *
151
     * @return Join
152
     */
153
    public static function join($field, $newAlias, $dqlAlias = null)
154
    {
155
        return new Join($field, $newAlias, $dqlAlias);
156
    }
157
158
    /**
159
     * @param string $field
160
     * @param string $newAlias
161
     * @param string $dqlAlias
162
     *
163
     * @return LeftJoin
164
     */
165
    public static function leftJoin($field, $newAlias, $dqlAlias = null)
166
    {
167
        return new LeftJoin($field, $newAlias, $dqlAlias);
168
    }
169
170
    /**
171
     * @param string $field
172
     * @param string $newAlias
173
     * @param string $dqlAlias
174
     *
175
     * @return InnerJoin
176
     */
177
    public static function innerJoin($field, $newAlias, $dqlAlias = null)
178
    {
179
        return new InnerJoin($field, $newAlias, $dqlAlias);
180
    }
181
182
    /**
183
     * @param int $count
184
     *
185
     * @return Limit
186
     */
187
    public static function limit($count)
188
    {
189
        return new Limit($count);
190
    }
191
192
    /**
193
     * @param int $count
194
     *
195
     * @return Offset
196
     */
197
    public static function offset($count)
198
    {
199
        return new Offset($count);
200
    }
201
202
    /**
203
     * @param int $sliceSize
204
     * @param int $sliceNumber
205
     *
206
     * @return Slice
207
     */
208
    public static function slice($sliceSize, $sliceNumber = 0)
209
    {
210
        return new Slice($sliceSize, $sliceNumber);
211
    }
212
213
    /**
214
     * @param string      $field
215
     * @param string      $order
216
     * @param string|null $dqlAlias
217
     *
218
     * @return OrderBy
219
     */
220
    public static function orderBy($field, $order = 'ASC', $dqlAlias = null)
221
    {
222
        return new OrderBy($field, $order, $dqlAlias);
223
    }
224
225
    /**
226
     * @param string $field
227
     * @param string $dqlAlias
228
     *
229
     * @return GroupBy
230
     */
231
    public static function groupBy($field, $dqlAlias = null)
232
    {
233
        return new GroupBy($field, $dqlAlias);
234
    }
235
236
    /*
237
     * Selection
238
     */
239
240
    /**
241
     * @param mixed $field
242
     *
243
     * @return Select
244
     */
245
    public static function select($field)
0 ignored issues
show
Unused Code introduced by
The parameter $field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
246
    {
247
        return new Select(func_get_args());
248
    }
249
250
    /**
251
     * @param mixed $field
252
     *
253
     * @return AddSelect
254
     */
255
    public static function addSelect($field)
0 ignored issues
show
Unused Code introduced by
The parameter $field is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
256
    {
257
        return new AddSelect(func_get_args());
258
    }
259
260
    /**
261
     * @param string $dqlAlias
262
     *
263
     * @return SelectEntity
264
     */
265
    public static function selectEntity($dqlAlias)
266
    {
267
        return new SelectEntity($dqlAlias);
268
    }
269
270
    /**
271
     * @param Filter|Operand|string $expression
272
     * @param string                $alias
273
     *
274
     * @return SelectAs
275
     */
276
    public static function selectAs($expression, $alias)
277
    {
278
        return new SelectAs($expression, $alias);
279
    }
280
281
    /**
282
     * @param Filter|Operand|string $expression
283
     * @param string                $alias
284
     *
285
     * @return SelectHiddenAs
286
     */
287
    public static function selectHiddenAs($expression, $alias)
288
    {
289
        return new SelectHiddenAs($expression, $alias);
290
    }
291
292
    /*
293
     * Result modifier
294
     */
295
296
    /**
297
     * @return AsArray
298
     */
299
    public static function asArray()
300
    {
301
        return new AsArray();
302
    }
303
304
    /**
305
     * @return AsSingleScalar
306
     */
307
    public static function asSingleScalar()
308
    {
309
        return new AsSingleScalar();
310
    }
311
312
    /**
313
     * @return AsScalar
314
     */
315
    public static function asScalar()
316
    {
317
        return new AsScalar();
318
    }
319
320
    /**
321
     * @param int $cacheLifetime How many seconds the cached entry is valid
322
     *
323
     * @return Cache
324
     */
325
    public static function cache($cacheLifetime)
326
    {
327
        return new Cache($cacheLifetime);
328
    }
329
330
    /**
331
     * @param int $roundSeconds How may seconds to round time
332
     *
333
     * @return RoundDateTime
334
     */
335
    public static function roundDateTimeParams($roundSeconds)
336
    {
337
        return new RoundDateTime($roundSeconds);
338
    }
339
340
    /*
341
     * Filters
342
     */
343
344
    /**
345
     * @param Operand|string $field
346
     * @param string|null    $dqlAlias
347
     *
348
     * @return IsNull
349
     */
350
    public static function isNull($field, $dqlAlias = null)
351
    {
352
        return new IsNull($field, $dqlAlias);
353
    }
354
355
    /**
356
     * @param Operand|string $field
357
     * @param string|null    $dqlAlias
358
     *
359
     * @return IsNotNull
360
     */
361
    public static function isNotNull($field, $dqlAlias = null)
362
    {
363
        return new IsNotNull($field, $dqlAlias);
364
    }
365
366
    /**
367
     * Make sure the $field has a value equals to $value.
368
     *
369
     * @param string $field
370
     * @param mixed  $value
371
     * @param string $dqlAlias
372
     *
373
     * @return In
374
     */
375
    public static function in($field, $value, $dqlAlias = null)
376
    {
377
        return new In($field, $value, $dqlAlias);
378
    }
379
380
    /**
381
     * @param string $field
382
     * @param mixed  $value
383
     * @param string $dqlAlias
384
     *
385
     * @return Not
386
     */
387
    public static function notIn($field, $value, $dqlAlias = null)
388
    {
389
        return new Not(new In($field, $value, $dqlAlias));
390
    }
391
392
    /**
393
     * @param Operand|string $field
394
     * @param Operand|mixed  $value
395
     * @param string|null    $dqlAlias
396
     *
397
     * @return Comparison
398
     */
399
    public static function eq($field, $value, $dqlAlias = null)
400
    {
401
        return new Comparison(Comparison::EQ, $field, $value, $dqlAlias);
402
    }
403
404
    /**
405
     * @param Operand|string $field
406
     * @param Operand|mixed  $value
407
     * @param string|null    $dqlAlias
408
     *
409
     * @return Comparison
410
     */
411
    public static function neq($field, $value, $dqlAlias = null)
412
    {
413
        return new Comparison(Comparison::NEQ, $field, $value, $dqlAlias);
414
    }
415
416
    /**
417
     * @param Operand|string $field
418
     * @param Operand|mixed  $value
419
     * @param string|null    $dqlAlias
420
     *
421
     * @return Comparison
422
     */
423
    public static function lt($field, $value, $dqlAlias = null)
424
    {
425
        return new Comparison(Comparison::LT, $field, $value, $dqlAlias);
426
    }
427
428
    /**
429
     * @param Operand|string $field
430
     * @param Operand|mixed  $value
431
     * @param string|null    $dqlAlias
432
     *
433
     * @return Comparison
434
     */
435
    public static function lte($field, $value, $dqlAlias = null)
436
    {
437
        return new Comparison(Comparison::LTE, $field, $value, $dqlAlias);
438
    }
439
440
    /**
441
     * @param Operand|string $field
442
     * @param Operand|mixed  $value
443
     * @param string|null    $dqlAlias
444
     *
445
     * @return Comparison
446
     */
447
    public static function gt($field, $value, $dqlAlias = null)
448
    {
449
        return new Comparison(Comparison::GT, $field, $value, $dqlAlias);
450
    }
451
452
    /**
453
     * @param Operand|string $field
454
     * @param Operand|mixed  $value
455
     * @param string|null    $dqlAlias
456
     *
457
     * @return Comparison
458
     */
459
    public static function gte($field, $value, $dqlAlias = null)
460
    {
461
        return new Comparison(Comparison::GTE, $field, $value, $dqlAlias);
462
    }
463
464
    /**
465
     * @param Operand|string $field
466
     * @param string         $value
467
     * @param string         $format
468
     * @param string|null    $dqlAlias
469
     *
470
     * @return Like
471
     */
472
    public static function like($field, $value, $format = Like::CONTAINS, $dqlAlias = null)
473
    {
474
        return new Like($field, $value, $format, $dqlAlias);
475
    }
476
477
    /**
478
     * @param string      $value
479
     * @param string|null $dqlAlias
480
     *
481
     * @return InstanceOfX
482
     */
483
    public static function instanceOfX($value, $dqlAlias = null)
484
    {
485
        return new InstanceOfX($value, $dqlAlias);
486
    }
487
488
    /**
489
     * @param string      $value
490
     * @param string|null $dqlAlias
491
     *
492
     * @return MemberOfX
493
     */
494
    public static function memberOfX($value, $dqlAlias = null)
495
    {
496
        return new MemberOfX($value, $dqlAlias);
497
    }
498
499
    /*
500
     * Specifications
501
     */
502
503
    /**
504
     * @param Filter|QueryModifier $spec
505
     *
506
     * @return CountOf
507
     */
508
    public static function countOf($spec)
509
    {
510
        return new CountOf($spec);
511
    }
512
513
    /**
514
     * @param Filter|string $spec
515
     *
516
     * @return Having
517
     */
518
    public static function having($spec)
519
    {
520
        return new Having($spec);
521
    }
522
523
    /*
524
     * Operands
525
     */
526
527
    /**
528
     * @param string $fieldName
529
     *
530
     * @return Field
531
     */
532
    public static function field($fieldName)
533
    {
534
        return new Field($fieldName);
535
    }
536
537
    /**
538
     * @param mixed           $value
539
     * @param int|string|null $valueType
540
     *
541
     * @return Value
542
     */
543
    public static function value($value, $valueType = null)
544
    {
545
        return new Value($value, $valueType);
546
    }
547
548
    /**
549
     * @param array           $values
550
     * @param int|string|null $valueType
551
     *
552
     * @return Values
553
     */
554
    public static function values($values, $valueType = null)
555
    {
556
        return new Values($values, $valueType);
557
    }
558
559
    /**
560
     * @param string $value
561
     * @param string $format
562
     *
563
     * @return LikePattern
564
     */
565
    public static function likePattern($value, $format = LikePattern::CONTAINS)
566
    {
567
        return new LikePattern($value, $format);
568
    }
569
570
    /*
571
     * Arithmetic operands
572
     */
573
574
    /**
575
     * @param Operand|string $field
576
     * @param Operand|mixed  $value
577
     *
578
     * @return Addition
579
     */
580
    public static function add($field, $value)
581
    {
582
        return new Addition($field, $value);
583
    }
584
585
    /**
586
     * @param Operand|string $field
587
     * @param Operand|mixed  $value
588
     *
589
     * @return Subtraction
590
     */
591
    public static function sub($field, $value)
592
    {
593
        return new Subtraction($field, $value);
594
    }
595
596
    /**
597
     * @param Operand|string $field
598
     * @param Operand|mixed  $value
599
     *
600
     * @return Multiplication
601
     */
602
    public static function mul($field, $value)
603
    {
604
        return new Multiplication($field, $value);
605
    }
606
607
    /**
608
     * @param Operand|string $field
609
     * @param Operand|mixed  $value
610
     *
611
     * @return Division
612
     */
613
    public static function div($field, $value)
614
    {
615
        return new Division($field, $value);
616
    }
617
618
    /**
619
     * @param Operand|string $field
620
     * @param Operand|mixed  $value
621
     *
622
     * @return Modulo
623
     */
624
    public static function mod($field, $value)
625
    {
626
        return new Modulo($field, $value);
627
    }
628
629
    /*
630
     * Bitwise operands
631
     */
632
633
    /**
634
     * @param Operand|string $field
635
     * @param Operand|mixed  $value
636
     *
637
     * @return BitAnd
638
     */
639
    public static function bAnd($field, $value)
640
    {
641
        return new BitAnd($field, $value);
642
    }
643
644
    /**
645
     * @param Operand|string $field
646
     * @param Operand|mixed  $value
647
     *
648
     * @return BitOr
649
     */
650
    public static function bOr($field, $value)
651
    {
652
        return new BitOr($field, $value);
653
    }
654
655
    /**
656
     * @param Operand|string $field
657
     * @param Operand|mixed  $value
658
     *
659
     * @return BitXor
660
     */
661
    public static function bXor($field, $value)
662
    {
663
        return new BitXor($field, $value);
664
    }
665
666
    /**
667
     * @param Operand|string $field
668
     * @param Operand|mixed  $value
669
     *
670
     * @return BitLeftShift
671
     */
672
    public static function bLs($field, $value)
673
    {
674
        return new BitLeftShift($field, $value);
675
    }
676
677
    /**
678
     * @param Operand|string $field
679
     * @param Operand|mixed  $value
680
     *
681
     * @return BitRightShift
682
     */
683
    public static function bRs($field, $value)
684
    {
685
        return new BitRightShift($field, $value);
686
    }
687
688
    /**
689
     * @param Operand|string $field
690
     *
691
     * @return BitNot
692
     */
693
    public static function bNot($field)
694
    {
695
        return new BitNot($field);
696
    }
697
698
    /**
699
     * Call DQL function.
700
     *
701
     * Usage:
702
     *  Spec::fun('CURRENT_DATE')
703
     *  Spec::fun('DATE_DIFF', $date1, $date2)
704
     *  Spec::fun('DATE_DIFF', [$date1, $date2])
705
     *
706
     * @param string $functionName
707
     * @param mixed  $arguments
708
     *
709
     * @return PlatformFunction
710
     */
711
    public static function fun($functionName, $arguments = [])
712
    {
713
        if (2 === func_num_args()) {
714
            $arguments = (array) $arguments;
715
        } else {
716
            $arguments = func_get_args();
717
            $functionName = array_shift($arguments);
718
        }
719
720
        return new PlatformFunction($functionName, $arguments);
721
    }
722
723
    /**
724
     * @param string $alias
725
     *
726
     * @return Alias
727
     */
728
    public static function alias($alias)
729
    {
730
        return new Alias($alias);
731
    }
732
}
733