Passed
Pull Request — develop (#27)
by Glynn
02:48
created

ComparissonCases.php$7 ➔ objectComparisons()   B

Complexity

Conditions 2

Size

Total Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 72
rs 8.6109
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace TestStub;
4
5
use DateTime;
6
use PinkCrab\FunctionConstructors\Arrays as Arr;
7
use PinkCrab\FunctionConstructors\Strings as Str;
8
use PinkCrab\FunctionConstructors\Comparisons as Comp;
9
use PinkCrab\FunctionConstructors\GeneralFunctions as Func;
10
11
class ComparissonCases
12
{
13
    /**
14
     * Returns an array of strings for comparisson tests.
15
     * Using 'pass' as the type will produce matching cases, else failues.
16
     *
17
     * @param string $type
18
     * @return array
19
     */
20
    public static function stringComparisson(string $type): array
21
    {
22
        if ($type === 'pass') {
23
            return array(
24
                array(
25
                    'expected' => 'hello',
26
                    'test'     => 'hello',
27
                ),
28
                array(
29
                    'expected' => '  t',
30
                    'test'     => '  t',
31
                ),
32
                array(
33
                    'expected' => '123',
34
                    'test'     => '123',
35
                ),
36
                array(
37
                    'expected' => '     8   u    t    y',
38
                    'test'     => '     8   u    t    y',
39
                ),
40
            );
41
        } else {
42
            return array(
43
                array(
44
                    'expected' => 'hello',
45
                    'test'     => 'hi',
46
                ),
47
                array(
48
                    'expected' => 't  ',
49
                    'test'     => '  t',
50
                ),
51
                array(
52
                    'expected' => '123',
53
                    'test'     => 123,
54
                ),
55
                array(
56
                    'expected' => '     8   u    t    y',
57
                    'test'     => '8   u    t    y',
58
                ),
59
            );
60
        }
61
    }
62
63
    /**
64
     * Returns an array of integers for comparisson tests.
65
     * Using 'pass' as the type will produce matching cases, else failues.
66
     *
67
     * @param string $type
68
     * @return array
69
     */
70
    public static function integerComparisons(string $type): array
71
    {
72
        if ($type === 'pass') {
73
            return array(
74
                array(
75
                    'expected' => 12,
76
                    'test'     => 12,
77
                ),
78
                array(
79
                    'expected' => 85, // OCTAL
80
                    'test'     => 00125,
81
                ),
82
                array(
83
                    'expected' => E_USER_NOTICE,
84
                    'test'     => 1024,
85
                ),
86
                array(
87
                    'expected' => 24,
88
                    'test'     => 6 * 4,
89
                ),
90
                array(
91
                    'expected' => 4,
92
                    'test'     => count(array( 1, 2, 3, 4 )),
93
                ),
94
            );
95
        } else {
96
            return array(
97
                array(
98
                    'expected' => 1,
99
                    'test'     => 1.1,
100
                ),
101
                array(
102
                    'expected' => 1 - 0,
103
                    'test'     => 0,
104
                ),
105
                array(
106
                    'expected' => 4 * 4,
107
                    'test'     => 44,
108
                ),
109
                array(
110
                    'expected' => 12 / 12,
111
                    'test'     => 0,
112
                ),
113
            );
114
        }
115
    }
116
117
    /**
118
     * Returns an array of floats for comparisson tests.
119
     * Using 'pass' as the type will produce matching cases, else failues.
120
     *
121
     * @param string $type
122
     * @return array
123
     */
124
    public static function floatComparisons(string $type): array
125
    {
126
        if ($type === 'pass') {
127
            return array(
128
                array(
129
                    'expected' => sqrt(2.25),
130
                    'test'     => 1.5,
131
                ),
132
                array(
133
                    'expected' => 78.5,
134
                    'test'     => 78.5,
135
                ),
136
                array(
137
                    'expected' => (12 / 5),
138
                    'test'     => 2.4,
139
                ),
140
                array(
141
                    'expected' => 2 / M_PI,
142
                    'test'     => M_2_PI,
143
                ),
144
            );
145
        } else {
146
            return array(
147
                array(
148
                    'expected' => sqrt(143),
149
                    'test'     => 12,
150
                ),
151
                array(
152
                    'expected' => 78.5,
153
                    'test'     => 80,
154
                ),
155
                array(
156
                    'expected' => (12 / 5),
157
                    'test'     => 2.5,
158
                ),
159
                array(
160
                    'expected' => M_PI,
161
                    'test'     => 3.2,
162
                ),
163
            );
164
        }
165
    }
166
167
    /**
168
     * Returns an array of integers for comparisson tests.
169
     * Using 'pass' as the type will produce matching cases, else failues.
170
     *
171
     * @param string $type
172
     * @return array
173
     */
174
    public static function arrayComparisons(string $type): array
175
    {
176
        if ($type === 'pass') {
177
            return array(
178
                array(
179
                    'expected' => array( 1, 2, 3, 4, 5 ),
180
                    'test'     => explode(',', '1,2,3,4,5'),
181
                ),
182
                array(
183
                    'expected' => array( 1, 2, 3, 'a', 'b', 'c' ),
184
                    'test'     => array_merge(
185
                        array( 1, 2, 3 ),
186
                        array( 'a', 'b', 'c' )
187
                    ),
188
                ),
189
                array(
190
                    'expected' => array( 2, 4, 6, 8, 10 ),
191
                    'test'     => array_map(
192
                        function ($e) {
193
                            return $e * 2;
194
                        },
195
                        array( 1, 2, 3, 4, 5 )
196
                    ),
197
                ),
198
                array(
199
                    'expected' => array( 'name' => 'Barry' ),
200
                    'test'     => (array) new class () {
201
                        public $name = 'Barry';
202
                    },
203
                ),
204
            );
205
        } else {
206
            return array(
207
                array(
208
                    'expected' => array( 1, 2, 3, 4, 5, 6 ),
209
                    'test'     => explode(',', '1,2,3,4,5'),
210
                ),
211
                array(
212
                    'expected' => array( 1, 2, 3, 'a', 'b', 'd' ),
213
                    'test'     => array_merge(
214
                        array( 1, 2, 3 ),
215
                        array( 'a', 'b', 'c' )
216
                    ),
217
                ),
218
                array(
219
                    'expected' => array( 2, 4, 6, 8, 10 ),
220
                    'test'     => array_map(
221
                        function ($e) {
222
                            return $e * 3;
223
                        },
224
                        array( 1, 2, 3, 4, 5 )
225
                    ),
226
                ),
227
                array(
228
                    'expected' => array( 'name' => 'Barry' ),
229
                    'test'     => (array) new class () {
230
                        public $name = 'Jane';
231
                    },
232
                ),
233
            );
234
        }
235
    }
236
237
    /**
238
     * Returns an object of integers for comparisson tests.
239
     * Using 'pass' as the type will produce matching cases, else failues.
240
     *
241
     * @param string $type
242
     * @return array
243
     */
244
    public static function objectComparisons(string $type): array
245
    {
246
        if ($type === 'pass') {
247
            $a = new class () {
248
                public $name = 'I WAS A VAR';
249
            };
250
            $b = new class () {
251
                public $name = 'I WAS A VAR';
252
            };
253
254
            return array(
255
                array(
256
                    'expected' => $a,
257
                    'test'     => $b,
258
                ),
259
                array(
260
                    'expected' => new class () {
261
                        public $name = 'Barry';
262
                    },
263
                    'test'     => (object) array( 'name' => 'Barry' ),
264
                ),
265
                array(
266
                    'expected' => (object) array(
267
                        'a' => 'a',
268
                        'b' => 'b',
269
                    ),
270
                    'test'     => new class () {
271
                        public $a = 'a';
272
                        public $b = 'b';
273
                    },
274
                ),
275
                array(
276
                    'expected' => new \stdClass(),
277
                    'test'     => (object) array(),
278
                ),
279
                array(
280
                    'expected' => json_decode(json_encode(array( 'itWas' => 'anArray' ))),
281
                    'test'     => (object) array( 'itWas' => 'anArray' ),
282
                ),
283
            );
284
        } else {
285
            return array(
286
                array(
287
                    'expected' => new class () {
288
                        public $name = 'Barry';
289
                    },
290
                    'test'     => (object) array( 'name' => 'Daisy' ),
291
                ),
292
                array(
293
                    'expected' => (object) array(
294
                        'a' => 'b',
295
                        'b' => 'a',
296
                    ),
297
                    'test'     => new class () {
298
                        public $a = 'a';
299
                        public $b = 'b';
300
                    },
301
                ),
302
                array(
303
                    'expected' => (object) array( 'unlucky' => 'mate' ),
304
                    'test'     => new \stdClass(),
305
                ),
306
                array(
307
                    'expected' => json_decode(
308
                        json_encode(
309
                            array(
310
                                'itWas'  => 'anArray',
311
                                'really' => false,
312
                            )
313
                        )
314
                    ),
315
                    'test'     => (object) array( 'itWas' => 'anArray' ),
316
                ),
317
            );
318
        }
319
    }
320
321
    /**
322
     * Returns an object of integers for comparisson tests.
323
     * Using 'pass' as the type will produce matching cases, else failues.
324
     *
325
     * @param string $type
326
     * @return array
327
     */
328
    public static function equalToOrComparisson(string $type): array
329
    {
330
        if ($type === 'pass') {
331
            return array(
332
                array(
333
                    'needles'  => array(
334
                        new class () {
335
                            public $name = 'Barry';
336
                        },
337
                        new class () {
338
                            public $name = 'Jane';
339
                        },
340
                    ),
341
                    'haystack' => (object) array( 'name' => 'Barry' ),
342
                ),
343
                array(
344
                    'needles'  => array(
345
                        new class () {
346
                            public $name = 'Barry';
347
                            public $points = 46587;
348
                        },
349
                        new class () {
350
                            public $name = 'Jane';
351
                            public $points = 132645;
352
                        },
353
                    ),
354
                    'haystack' => new class () {
355
                        public $name = 'Barry';
356
                        public $points = 46587;
357
                    },
358
                ),
359
                array(
360
                    'needles'  => array( 1, 2, 3, 4, 6 ),
361
                    'haystack' => 4,
362
                ),
363
                array(
364
                    'needles'  => array( 'a', 'b', 'c', 'd' ),
365
                    'haystack' => 'c',
366
                ),
367
                array(
368
                    'needles'  => array( array( 'a' ), array( 'b' ), 'c', array( 'd' ) ),
369
                    'haystack' => array( 'a' ),
370
                ),
371
372
            );
373
        } else {
374
            return array(
375
                array(
376
                    'needles'  => array(
377
                        new class () {
378
                            public $name = 'Barry';
379
                        },
380
                        new class () {
381
                            public $name = 'Jane';
382
                        },
383
                    ),
384
                    'haystack' => (object) array( 'name' => 'Rebbeca' ),
385
                ),
386
                array(
387
                    'needles'  => array(
388
                        new class () {
389
                            public $name = 'Barry';
390
                            public $points = 46587;
391
                        },
392
                        new class () {
393
                            public $name = 'Jane';
394
                            public $points = 132645;
395
                        },
396
                    ),
397
                    'haystack' => new class () {
398
                        public $name = 'Barry';
399
                        public $points = 88888;
400
                    },
401
                ),
402
                array(
403
                    'needles'  => array( 1, 2, 3, 4, 6 ),
404
                    'haystack' => 41,
405
                ),
406
                array(
407
                    'needles'  => array( 'a', 'b', 'c', 'd' ),
408
                    'haystack' => 'R',
409
                ),
410
                array(
411
                    'needles'  => array( array( 'a' ), array( 'b' ), 'c', array( 'd' ) ),
412
                    'haystack' => array( 'a', 'B' ),
413
                ),
414
            );
415
        }
416
    }
417
418
    /**
419
     * Returns an array of mixed scalar types.
420
     * Using 'pass' as the type will produce matching cases, else failues.
421
     *
422
     * @param string $type
423
     * @return array
424
     */
425
    public static function scalarComparisons(string $type): array
426
    {
427
        if ($type === 'pass') {
428
            return array(
429
                array( // String
430
                    'dfsdfs',
431
                    'dfdsfsdfs',
432
                    '          _    ',
433
                    '123',
434
                    '£$%',
435
                    '!',
436
                    "\r",
437
                ),
438
                array( // Int
439
                    123,
440
                    0015,
441
                    (12 * 12),
442
                    __LINE__,
443
                    PHP_INT_MIN,
444
                    E_USER_NOTICE,
445
                    hexdec('ee'),
446
                    bindec('110011'),
447
                ),
448
                array( // FLoat
449
                    sqrt(143),
450
                    78.5,
451
                    (12 / 5),
452
                    M_PI,
453
454
                ),
455
                array( // Srray
456
                    array( 'xx' ),
457
                    array( true, false, null ),
458
                    explode(',', 'this,is,a,test,string'),
459
                    array( array( 1, array( 1, 5, 6, 7, array( 44, array( 5 ) ) ) ) ),
460
                ),
461
                array( // Object
462
                    new class () {
463
                    },
464
                    json_decode(json_encode(array( 'itWas' => 'anArray' ))),
465
                    new DateTime('2020-01-01T15:03:01.012345Z'),
466
                    dir(__DIR__),
467
                    new \stdClass(),
468
                    (object) array( 'iWasAlso' => 'anArray' ),
469
                ),
470
            );
471
        } else {
472
            return array(
473
                array( // Mixed madness
474
                    'dfsdfs',
475
                    'dfdsfsdfs',
476
                    '          _    ',
477
                    123,
478
                    (12 * 12),
479
                    __LINE__,
480
                    PHP_INT_MIN,
481
                    json_decode(json_encode(array( 'itWas' => 'anArray' ))),
482
                    new DateTime('2020-01-01T15:03:01.012345Z'),
483
                    E_USER_NOTICE,
484
                    sqrt(144),
485
                    (12 / 5),
486
                    M_PI,
487
                    array( true, false, null ),
488
                    explode(',', 'this,is,a,test,string'),
489
                    hexdec('ee'),
490
                    bindec('110011'),
491
                ),
492
                array( // Array and casted arrays.
493
                    array( 'xx' ),
494
                    explode(',', 'this,is,a,test,string'),
495
                    (object) array( 'iWasAlso' => 'anArray' ),
496
                ),
497
                array( // Int, FLoat, String
498
                    12,
499
                    12.1,
500
                    '12',
501
                ),
502
            );
503
        }
504
    }
505
506
    public static function groupSingleAndComparisonsArrays($type = 'fail'): array
507
    {
508
        if ($type === 'pass') {
509
            return array(
510
                array(
511
                    'array'    => array(
512
                        'HI john hello',
513
                        'HI mark hello',
514
                        'HI claire hello',
515
                    ),
516
                    'expected' => array( 'HI john hello' ),
517
                    'function' => Arr\filterAnd(
518
                        Str\contains('john'),
519
                        Str\startsWith('HI')
520
                    ),
521
                ),
522
523
                // Array contents
524
                array(
525
                    'array'    => array(
526
                        array(
527
                            'a' => 'alpha',
528
                            'b' => 'b',
529
                            'c' => 'c',
530
                        ),
531
                        (object) array(
532
                            'a' => 'delta',
533
                            'b' => 'b',
534
                            'c' => 'c',
535
                        ),
536
                        array(
537
                            'a' => 'bravo',
538
                            'b' => 'd',
539
                            'c' => 'e',
540
                        ),
541
542
                        array(
543
                            'a' => 'charlie',
544
                            'b' => 'b',
545
                            'c' => 'e',
546
                        ),
547
                    ),
548
                    'expected' => array(
549
                        array(
550
                            'a' => 'alpha',
551
                            'b' => 'b',
552
                            'c' => 'c',
553
                        ),
554
                        (object) array(
555
                            'a' => 'delta',
556
                            'b' => 'b',
557
                            'c' => 'c',
558
                        ),
559
                    ),
560
                    'function' => Arr\filterAnd(
561
                        Func\compose(Func\getProperty('b'), Comp\isEqualTo('b')),
562
                        Func\compose(Func\getProperty('c'), Comp\isEqualTo('c'))
563
                    ),
564
                ),
565
566
            );
567
        } else {
568
            return array(
569
                array(
570
                    'array'    => array(
571
                        'HI john hello',
572
                        'HI mark hello',
573
                        'HI claire hello',
574
                    ),
575
                    'expected' => array( 'HI john hello' ),
576
                    'function' => Arr\filterAnd(
577
                        Str\contains('John'),
578
                        Str\startsWith('HI')
579
                    ),
580
                ),
581
582
                // Array contents
583
                array(
584
                    'array'    => array(
585
                        array(
586
                            'a' => 'alpha',
587
                            'b' => 'b',
588
                            'c' => 'c',
589
                        ),
590
                        (object) array(
591
                            'a' => 'delta',
592
                            'b' => 'b',
593
                            'c' => 'c',
594
                        ),
595
                        array(
596
                            'a' => 'bravo',
597
                            'b' => 'd',
598
                            'c' => 'e',
599
                        ),
600
601
                        array(
602
                            'a' => 'charlie',
603
                            'b' => 'b',
604
                            'c' => 'e',
605
                        ),
606
                    ),
607
                    'expected' => array(
608
                        array(
609
                            'a' => 'alpha',
610
                            'b' => 'b',
611
                            'c' => 'c',
612
                        ),
613
                        (object) array(
614
                            'a' => 'delta',
615
                            'b' => 'b',
616
                            'c' => 'c',
617
                        ),
618
                    ),
619
                    'function' => Arr\filterAnd(
620
                        Func\compose(Func\getProperty('b'), Comp\isEqualTo('b')),
621
                        Func\compose(Func\getProperty('c'), Comp\isEqualTo('cAt'))
622
                    ),
623
                ),
624
625
            );
626
        }
627
    }
628
629
    public static function groupSingleAndComparisonsStrings(string $type): array
630
    {
631
        // Functions.
632
        $appleWithDashes = Comp\groupAnd(
633
            Str\contains('APPLE'),
634
            Str\startsWith('--')
635
        );
636
        $randomStrings   = Comp\groupAnd(
637
            Str\contains('ffff'),
638
            Str\contains('trt'),
639
            Str\startsWith('dg')
640
        );
641
        $numbersInString = Comp\groupAnd(
642
            Str\contains('666'),
643
            Str\contains('333')
644
        );
645
        if ($type === 'pass') {
646
            return array(
647
                array(
648
                    'value'    => array(
649
                        '--APPLE',
650
                        '--hjkgjhgAPPLE',
651
                        '--------APPLE',
652
                        '--------AAAAAAAAAAPPLE',
653
                    ),
654
                    'function' => $appleWithDashes,
655
                ),
656
                array(
657
                    'value'    => array(
658
                        'dgdsgsdgsdgsdtrtdghgfdsffffgd',
659
                        'dgjjkdffffshgjktrtdhgjksdhgjksdhk',
660
                    ),
661
                    'function' => $randomStrings,
662
                ),
663
                array(
664
                    'value'    => array(
665
                        'dsf333sdfsd666fgdfdfgdf',
666
                        'dgdf333sddasda666sdas',
667
                    ),
668
                    'function' => $numbersInString,
669
                ),
670
            );
671
        } else {
672
            return array(
673
                array(
674
                    'value'    => array(
675
                        '--PEAR',
676
                        '__hjkgjhgAPPLE',
677
                        '--------APPEARLE',
678
                        '--------AAAAAAAAAAPPLPEAR',
679
                    ),
680
                    'function' => $appleWithDashes,
681
                ),
682
                array(
683
                    'value'    => array(
684
                        'iuoiuouiouio',
685
                        '.kkj.ik.kjk.jk.jkjk.jk',
686
                    ),
687
                    'function' => $randomStrings,
688
                ),
689
                array(
690
                    'value'    => array(
691
                        'dsfsdfsd555fgdfdfgdf',
692
                        'dgdf332sddasdasdas',
693
                    ),
694
                    'function' => $numbersInString,
695
                ),
696
            );
697
        }
698
    }
699
700
    public static function groupedAndOrComparissonMixed(string $type): array
701
    {
702
        $orAndOr1  = Comp\groupAnd(
703
            Comp\groupOr(
704
                Str\contains('666'),
705
                Str\contains('333')
706
            ),
707
            Comp\groupOr(
708
                Str\startsWith('dg'),
709
                Str\startsWith('ff')
710
            )
711
        );
712
        $andOrAnd1 = Comp\groupOr(
713
            Comp\groupAnd(
714
                Str\startsWith('dg'),
715
                Str\contains('666')
716
            ),
717
            Comp\groupAnd(
718
                Str\contains('333'),
719
                Str\startsWith('ff')
720
            )
721
        );
722
723
        if ($type === 'pass') {
724
            return array(
725
                array(
726
                    'value'    => array(
727
                        'dg666_____jkhjkhkjhk',
728
                        'dg333_____jkhjkhkjhk',
729
                        'ff666_____jkhjkhkjhk',
730
                        'ff333_____jkhjkhkjhk',
731
                        'dg_hjjkhkj_uyuiyiuyiu666_____jkhjkhkjhk',
732
                        'dg_hjjkhkj_uyuiyiuyiu333_____jkhjkhkjhk',
733
                        'ff_hjjkhkj_uyuiyiuyiu666_____jkhjkhkjhk',
734
                        'ff_hjjkhkj_uyuiyiuyiu333_____jkhjkhkjhk',
735
                    ),
736
                    'function' => $orAndOr1,
737
                ),
738
                array(
739
                    'value'    => array(
740
                        'dg666_____jkhjkhkjhk',
741
                        'ff333_____jkhjkhkjhk',
742
                        'dg_hjjkhkj_uyuiyiuyiu666_____jkhjkhkjhk',
743
                        'ff_hjjkhkj_uyuiyiuyiu333_____jkhjkhkjhk',
744
                    ),
745
                    'function' => $andOrAnd1,
746
                ),
747
            );
748
        } else {
749
            return array(
750
                array(
751
                    'value'    => array(
752
                        '555tt_____jkhjkhkjhk',
753
                        '333tt_____jkhjkhkjhk',
754
                        '555ff_____jkhjkhkjhk',
755
                        '333ff_____jkhjkhkjhk',
756
                        '_hjjkhkj_uyuiyiuyiu555tt_____jkhjkhkjhk',
757
                        '_hjjkhkj_uyuiyiuyiu333tt_____jkhjkhkjhk',
758
                        '_hjjkhkj_uyuiyiuyiu555ff_____jkhjkhkjhk',
759
                        '_hjjkhkj_uyuiyiuyiu333ff_____jkhjkhkjhk',
760
                    ),
761
                    'function' => $orAndOr1,
762
                ),
763
                array(
764
                    'value'    => array(
765
                        '666ff_____jkhjkhkjhk',
766
                        '333dg_____jkhjkhkjhk',
767
                        '_hjjkhkj_uyuiyiuyiu666ff_____jkhjkhkjhk',
768
                        '_hjjkhkj_uyuiyiuyiu333dg_____jkhjkhkjhk',
769
                    ),
770
                    'function' => $andOrAnd1,
771
                ),
772
            );
773
        }
774
    }
775
}
776