Completed
Push — feature/EVO-7836-schema-hide-e... ( 388717 )
by
unknown
16:14
created

Schema   F

Complexity

Total Complexity 78

Size/Duplication

Total Lines 938
Duplicated Lines 0 %

Coupling/Cohesion

Components 9
Dependencies 3

Test Coverage

Coverage 4.19%

Importance

Changes 0
Metric Value
wmc 78
lcom 9
cbo 3
dl 0
loc 938
ccs 8
cts 191
cp 0.0419
rs 1.6901
c 0
b 0
f 0

61 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setTitle() 0 4 1
A getTitle() 0 4 1
A setDescription() 0 4 1
A getDescription() 0 4 1
C setType() 0 35 8
A getType() 0 4 1
A getMinLengthTypes() 0 4 1
A getFormat() 0 4 1
A setFormat() 0 4 1
A getNumericMinimum() 0 4 1
A setNumericMinimum() 0 4 1
A getNumericMaximum() 0 4 1
A setNumericMaximum() 0 4 1
A getMinLength() 0 4 1
A setMinLength() 0 4 1
A getMaxLength() 0 4 1
A setMaxLength() 0 4 1
A getMinItems() 0 4 1
A setMinItems() 0 4 1
A getMaxItems() 0 4 1
A setMaxItems() 0 4 1
A getEnum() 0 4 1
A setEnum() 0 4 1
A getRegexPattern() 0 4 1
A setRegexPattern() 0 4 1
A getDocumentClass() 0 4 1
A setDocumentClass() 0 4 1
A getConstraints() 0 4 1
A setConstraints() 0 4 1
A addConstraint() 0 4 1
A setItems() 0 4 1
A getItems() 0 4 1
A addProperty() 0 4 1
A removeProperty() 0 6 2
A getProperty() 0 7 2
A getProperties() 0 8 2
A setAdditionalProperties() 0 4 1
A getAdditionalProperties() 0 4 1
A setRequired() 0 5 1
A getRequired() 0 9 2
A setTranslatable() 0 8 2
A isTranslatable() 0 9 2
A setRefCollection() 0 4 1
A getRefCollection() 0 9 2
A setEventNames() 0 4 1
A getEventNames() 0 9 2
A setReadOnly() 0 4 1
A getReadOnly() 0 4 2
A isRecordOriginModifiable() 0 4 1
A setRecordOriginModifiable() 0 4 1
A isRecordOriginException() 0 4 1
A setRecordOriginException() 0 4 1
A isVersioning() 0 4 1
A setIsVersioning() 0 4 1
A setSearchable() 0 4 1
A getSearchable() 0 7 2
A getTextIndexes() 0 4 1
A setTextIndexes() 0 4 1
A isExpose() 0 4 1
A setExpose() 0 4 1

How to fix   Complexity   

Complex Class

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

1
<?php
2
/**
3
 * Graviton Schema Document
4
 */
5
6
namespace Graviton\SchemaBundle\Document;
7
8
use \Doctrine\Common\Collections\ArrayCollection;
9
10
/**
11
 * Graviton\SchemaBundle\Document\Schema
12
 *
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
15
 * @link     http://swisscom.ch
16
 */
17
class Schema
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $title;
23
24
    /**
25
     * @var string
26
     */
27
    protected $description;
28
29
    /**
30
     * @var SchemaType
31
     */
32
    protected $type;
33
34
    /**
35
     * @var string
36
     */
37
    protected $format;
38
39
    /**
40
     * @var Schema
41
     */
42
    protected $items;
43
44
    /**
45
     * @var ArrayCollection
46
     */
47
    protected $properties;
48
49
    /**
50
     * @var SchemaAdditionalProperties
51
     */
52
    protected $additionalProperties;
53
54
    /**
55
     * @var string[]
56
     */
57
    protected $required = [];
58
59
    /**
60
     * @var boolean
61
     */
62
    protected $translatable;
63
64
    /**
65
     * @var array
66
     */
67
    protected $refCollection = [];
68
69
    /**
70
     * possible event names this collection implements (queue events)
71
     *
72
     * @var array
73
     */
74
    protected $eventNames = [];
75
76
    /**
77
     * @var bool
78
     */
79
    protected $readOnly = false;
80
81
    /**
82
     * @var bool
83
     */
84
    protected $recordOriginModifiable;
85
86
    /**
87
     * @var bool
88
     */
89
    protected $recordOriginException;
90
91
    /**
92
     * @var bool
93
     */
94
    protected $isVersioning;
95
96
    /**
97
     * @var string[]
98
     */
99
    protected $searchable = [];
100
101
    /**
102
     * @var int
103
     */
104
    protected $minLength;
105
106
    /**
107
     * @var int
108
     */
109
    protected $maxLength;
110
111
    /**
112
     * @var int
113
     */
114
    protected $minItems;
115
116
    /**
117
     * @var int
118
     */
119
    protected $maxItems;
120
121
    /**
122
     * @var float
123
     */
124
    protected $numericMinimum;
125
126
    /**
127
     * @var float
128
     */
129
    protected $numericMaximum;
130
131
    /**
132
     * @var SchemaEnum
133
     */
134
    protected $enum;
135
136
    /**
137
     * @var string
138
     */
139
    protected $regexPattern;
140
141
    /**
142
     * @var string
143
     */
144
    protected $documentClass;
145
146
    /**
147
     * @var array<string>
148
     */
149
    protected $constraints;
150
151
    /**
152
     * @var array<string>
153
     */
154
    protected $textIndexes;
155
156
    /**
157
     * @var boolean
158
     */
159
    protected $expose;
160
161
    /**
162
     * these are the BSON primitive types.
163
     * http://json-schema.org/latest/json-schema-core.html#anchor8
164
     * every type set *not* in this set will be carried over to 'format'
165
     *
166
     * @var string[]
167
     */
168
    protected $primitiveTypes = [
169
        'array',
170
        'boolean',
171
        'integer',
172
        'number',
173
        'null',
174
        'object',
175
        'string'
176
    ];
177
178
    /**
179
     * those are types that when they are required, a minimal length
180
     * shall be specified in schema (or allow null if not required; that will lead
181
     * to the inclusion of "null" in the "type" property array)
182
     *
183
     * @var array
184
     */
185
    protected $minLengthTypes = [
186
        'integer',
187
        'number',
188
        'float',
189
        'double',
190
        'decimal',
191
        'string',
192
        'date',
193
        'extref'
194
    ];
195
196
    /**
197
     * known non-primitive types we map to primitives here.
198
     * the type itself is set to the format.
199
     *
200
     * @var string[]
201
     */
202
    protected $specialTypeMapping = [
203
        'extref' => 'string',
204
        'translatable' => 'object',
205
        'date' => 'string',
206
        'float' => 'number',
207
        'double' => 'number',
208
        'decimal' => 'number'
209
    ];
210
211
    protected $formatOverrides = [
212
        'date' => 'date-time'
213
    ];
214
215
    /**
216
     * Build properties
217
     */
218 2
    public function __construct()
219
    {
220 2
        $this->properties = new ArrayCollection();
221 2
    }
222
223
    /**
224
     * set title
225
     *
226
     * @param string $title title
227
     *
228
     * @return void
229
     */
230 2
    public function setTitle($title)
231
    {
232 2
        $this->title = $title;
233 2
    }
234
235
    /**
236
     * get title
237
     *
238
     * @return string
239
     */
240 2
    public function getTitle()
241
    {
242 2
        return $this->title;
243
    }
244
245
    /**
246
     * set description
247
     *
248
     * @param string $description description
249
     *
250
     * @return void
251
     */
252
    public function setDescription($description)
253
    {
254
        $this->description = $description;
255
    }
256
257
    /**
258
     * get description
259
     *
260
     * @return string
261
     */
262
    public function getDescription()
263
    {
264
        return $this->description;
265
    }
266
267
    /**
268
     * set type
269
     *
270
     * @param string|array $types types
271
     *
272
     * @return void
273
     */
274
    public function setType($types)
275
    {
276
        if (!is_array($types)) {
277
            $types = [$types];
278
        }
279
280
        $typesToSet = [];
281
        foreach ($types as $type) {
282
            if ($type === 'int') {
283
                $type = 'integer';
284
            }
285
            if ($type === 'hash') {
286
                $type = 'object';
287
            }
288
289
            // handle non-primitive types
290
            if (!in_array($type, $this->primitiveTypes)) {
291
                $setType = 'string';
292
                if (isset($this->specialTypeMapping[$type])) {
293
                    $setType = $this->specialTypeMapping[$type];
294
                }
295
                $typesToSet[] = $setType;
296
297
                if (isset($this->formatOverrides[$type])) {
298
                    $type = $this->formatOverrides[$type];
299
                }
300
301
                $this->setFormat($type);
302
            } else {
303
                $typesToSet[] = $type;
304
            }
305
        }
306
307
        $this->type = new SchemaType($typesToSet);
308
    }
309
310
    /**
311
     * get type
312
     *
313
     * @return SchemaType type
314
     */
315
    public function getType()
316
    {
317
        return $this->type;
318
    }
319
320
    /**
321
     * get MinLengthTypes
322
     *
323
     * @return array MinLengthTypes
324
     */
325
    public function getMinLengthTypes()
326
    {
327
        return $this->minLengthTypes;
328
    }
329
330
    /**
331
     * get format
332
     *
333
     * @return string format
334
     */
335
    public function getFormat()
336
    {
337
        return $this->format;
338
    }
339
340
    /**
341
     * sets format
342
     *
343
     * @param string $format format
344
     *
345
     * @return void
346
     */
347
    public function setFormat($format)
348
    {
349
        $this->format = $format;
350
    }
351
352
    /**
353
     * get numeric minimum
354
     *
355
     * @return float numeric minimum
356
     */
357
    public function getNumericMinimum()
358
    {
359
        return $this->numericMinimum;
360
    }
361
362
    /**
363
     * set numeric minimum
364
     *
365
     * @param float $numericMinimum numeric mimimum
366
     *
367
     * @return void
368
     */
369
    public function setNumericMinimum($numericMinimum)
370
    {
371
        $this->numericMinimum = $numericMinimum;
372
    }
373
374
    /**
375
     * get numeric maximum
376
     *
377
     * @return float numeric maximum
378
     */
379
    public function getNumericMaximum()
380
    {
381
        return $this->numericMaximum;
382
    }
383
384
    /**
385
     * set numeric maximum
386
     *
387
     * @param float $numericMaximum maximum
388
     *
389
     * @return void
390
     */
391
    public function setNumericMaximum($numericMaximum)
392
    {
393
        $this->numericMaximum = $numericMaximum;
394
    }
395
396
    /**
397
     * set min length
398
     *
399
     * @return int length
400
     */
401
    public function getMinLength()
402
    {
403
        return $this->minLength;
404
    }
405
406
    /**
407
     * get min length
408
     *
409
     * @param int $minLength length
410
     *
411
     * @return void
412
     */
413
    public function setMinLength($minLength)
414
    {
415
        $this->minLength = $minLength;
416
    }
417
418
    /**
419
     * gets maxlength
420
     *
421
     * @return int length
422
     */
423
    public function getMaxLength()
424
    {
425
        return $this->maxLength;
426
    }
427
428
    /**
429
     * set maxlength
430
     *
431
     * @param int $maxLength length
432
     *
433
     * @return void
434
     */
435
    public function setMaxLength($maxLength)
436
    {
437
        $this->maxLength = $maxLength;
438
    }
439
440
    /**
441
     * set min Items
442
     *
443
     * @return int Items
444
     */
445
    public function getMinItems()
446
    {
447
        return $this->minItems;
448
    }
449
450
    /**
451
     * get min Items
452
     *
453
     * @param int $minItems length
454
     *
455
     * @return void
456
     */
457
    public function setMinItems($minItems)
458
    {
459
        $this->minItems = $minItems;
460
    }
461
462
    /**
463
     * gets maxItems
464
     *
465
     * @return int Items
466
     */
467
    public function getMaxItems()
468
    {
469
        return $this->maxItems;
470
    }
471
472
    /**
473
     * set maxItems
474
     *
475
     * @param int $maxItems Items
476
     *
477
     * @return void
478
     */
479
    public function setMaxItems($maxItems)
480
    {
481
        $this->maxItems = $maxItems;
482
    }
483
484
    /**
485
     * get Enum
486
     *
487
     * @return array Enum
0 ignored issues
show
Documentation introduced by
Should the return type not be SchemaEnum?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
488
     */
489
    public function getEnum()
490
    {
491
        return $this->enum;
492
    }
493
494
    /**
495
     * set Enum
496
     *
497
     * @param array $enum enum
498
     *
499
     * @return void
500
     */
501
    public function setEnum(array $enum)
502
    {
503
        $this->enum = new SchemaEnum($enum);
504
    }
505
506
    /**
507
     * get regex pattern
508
     *
509
     * @return string pattern
510
     */
511
    public function getRegexPattern()
512
    {
513
        return $this->regexPattern;
514
    }
515
516
    /**
517
     * set regex pattern
518
     *
519
     * @param string $regexPattern regex pattern
520
     *
521
     * @return void
522
     */
523
    public function setRegexPattern($regexPattern)
524
    {
525
        $this->regexPattern = $regexPattern;
526
    }
527
528
    /**
529
     * get DocumentClass
530
     *
531
     * @return string DocumentClass
532
     */
533
    public function getDocumentClass()
534
    {
535
        return $this->documentClass;
536
    }
537
538
    /**
539
     * set DocumentClass
540
     *
541
     * @param string $documentClass documentClass
542
     *
543
     * @return void
544
     */
545
    public function setDocumentClass($documentClass)
546
    {
547
        $this->documentClass = $documentClass;
548
    }
549
550
    /**
551
     * get Constraints
552
     *
553
     * @return mixed Constraints
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
554
     */
555
    public function getConstraints()
556
    {
557
        return $this->constraints;
558
    }
559
560
    /**
561
     * set Constraints
562
     *
563
     * @param mixed $constraints constraints
564
     *
565
     * @return void
566
     */
567
    public function setConstraints($constraints)
568
    {
569
        $this->constraints = $constraints;
570
    }
571
572
    /**
573
     * add a constraint
574
     *
575
     * @param string $name constraint name
576
     *
577
     * @return void
578
     */
579
    public function addConstraint($name)
580
    {
581
        $this->constraints[] = $name;
582
    }
583
584
    /**
585
     * set items
586
     *
587
     * @param Schema $items items schema
588
     *
589
     * @return void
590
     */
591
    public function setItems($items)
592
    {
593
        $this->items = $items;
594
    }
595
596
    /**
597
     * get items
598
     *
599
     * @return Schema
600
     */
601
    public function getItems()
602
    {
603
        return $this->items;
604
    }
605
606
    /**
607
     * add a property
608
     *
609
     * @param string $name     property name
610
     * @param Schema $property property
611
     *
612
     * @return void
613
     */
614
    public function addProperty($name, $property)
615
    {
616
        $this->properties->set($name, $property);
617
    }
618
619
    /**
620
     * removes a property
621
     *
622
     * @param string $name property name
623
     *
624
     * @return void
625
     */
626
    public function removeProperty($name)
627
    {
628
        if ($this->properties->containsKey($name)) {
629
            $this->properties->remove($name);
630
        }
631
    }
632
633
    /**
634
     * returns a property
635
     *
636
     * @param string $name property name
0 ignored issues
show
Documentation introduced by
Should the type for parameter $name not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
637
     *
638
     * @return null|Schema property
639
     */
640
    public function getProperty($name = null)
641
    {
642
        if (is_null($name)) {
643
            return null;
644
        }
645
        return $this->properties->get($name);
646
    }
647
648
    /**
649
     * get properties
650
     *
651
     * @return ArrayCollection|null
652
     */
653
    public function getProperties()
654
    {
655
        if ($this->properties->isEmpty()) {
656
            return null;
657
        } else {
658
            return $this->properties;
659
        }
660
    }
661
662
    /**
663
     * set additionalProperties on schema
664
     *
665
     * @param SchemaAdditionalProperties $additionalProperties additional properties
666
     *
667
     * @return void
668
     */
669
    public function setAdditionalProperties(SchemaAdditionalProperties $additionalProperties)
670
    {
671
        $this->additionalProperties = $additionalProperties;
672
    }
673
674
    /**
675
     * get addtionalProperties for schema
676
     *
677
     * @return SchemaAdditionalProperties
678
     */
679
    public function getAdditionalProperties()
680
    {
681
        return $this->additionalProperties;
682
    }
683
684
    /**
685
     * set required variables
686
     *
687
     * @param string[] $required array of required fields
688
     *
689
     * @return void
690
     */
691
    public function setRequired(array $required)
692
    {
693
        // needed as indexes could we off and we want to enforce an array after json_encode
694
        $this->required = array_values($required);
695
    }
696
697
    /**
698
     * get required fields
699
     *
700
     * @return string[]|null
701
     */
702
    public function getRequired()
703
    {
704
        $required = $this->required;
705
        if (empty($required)) {
706
            $required = null;
707
        }
708
709
        return $required;
710
    }
711
712
    /**
713
     * set translatable flag
714
     *
715
     * This flag is a local extension to json schema.
716
     *
717
     * @param boolean $translatable translatable flag
718
     *
719
     * @return void
720
     */
721
    public function setTranslatable($translatable)
722
    {
723
        if ($translatable === true) {
724
            $this->setType('translatable');
725
        } else {
726
            $this->setType('string');
727
        }
728
    }
729
730
    /**
731
     * get translatable flag
732
     *
733
     * @return boolean
734
     */
735
    public function isTranslatable()
736
    {
737
        $ret = false;
738
        if ($this->getFormat() == 'translatable') {
739
            $ret = true;
740
        }
741
742
        return $ret;
743
    }
744
745
    /**
746
     * set a array of urls that can extref refer to
747
     *
748
     * @param array $refCollection urls
749
     *
750
     * @return void
751
     */
752
    public function setRefCollection(array $refCollection)
753
    {
754
        $this->refCollection = $refCollection;
755
    }
756
757
    /**
758
     * get a collection of urls that can extref refer to
759
     *
760
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
761
     */
762
    public function getRefCollection()
763
    {
764
        $collection = $this->refCollection;
765
        if (empty($collection)) {
766
            $collection = null;
767
        }
768
769
        return $collection;
770
    }
771
772
    /**
773
     * set an array of possible event names
774
     *
775
     * @param array $eventNames event names
776
     *
777
     * @return void
778
     */
779
    public function setEventNames(array $eventNames)
780
    {
781
        $this->eventNames = array_values($eventNames);
782
    }
783
784
    /**
785
     * get a collection of possible event names
786
     *
787
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
788
     */
789
    public function getEventNames()
790
    {
791
        $collection = $this->eventNames;
792
        if (empty($collection)) {
793
            $collection = null;
794
        }
795
796
        return $collection;
797
    }
798
799
    /**
800
     * Set the readOnly flag
801
     *
802
     * @param bool $readOnly ReadOnly flag
803
     *
804
     * @return void
805
     */
806
    public function setReadOnly($readOnly)
807
    {
808
        $this->readOnly = (bool) $readOnly;
809
    }
810
811
    /**
812
     * Get the readOnly flag.
813
     * Returns null if the flag is set to false so the serializer will ignore it.
814
     *
815
     * @return bool|null true if readOnly isset to true or null if not
816
     */
817
    public function getReadOnly()
818
    {
819
        return $this->readOnly ? true : null;
820
    }
821
822
    /**
823
     * get RecordOriginModifiable
824
     *
825
     * @return boolean RecordOriginModifiable
826
     */
827
    public function isRecordOriginModifiable()
828
    {
829
        return $this->recordOriginModifiable;
830
    }
831
832
    /**
833
     * set RecordOriginModifiable
834
     *
835
     * @param boolean $recordOriginModifiable recordOriginModifiable
836
     *
837
     * @return void
838
     */
839
    public function setRecordOriginModifiable($recordOriginModifiable)
840
    {
841
        $this->recordOriginModifiable = $recordOriginModifiable;
842
    }
843
844
    /**
845
     * get RecordOriginException
846
     *
847
     * @return boolean RecordOriginException
848
     */
849
    public function isRecordOriginException()
850
    {
851
        return $this->recordOriginException;
852
    }
853
854
    /**
855
     * set RecordOriginException
856
     *
857
     * @param boolean $recordOriginException recordOriginException
858
     *
859
     * @return void
860
     */
861
    public function setRecordOriginException($recordOriginException)
862
    {
863
        $this->recordOriginException = $recordOriginException;
864
    }
865
866
    /**
867
     * isVersioning
868
     *
869
     * @return bool IsVersioning
870
     */
871
    public function isVersioning()
872
    {
873
        return $this->isVersioning;
874
    }
875
876
    /**
877
     * set IsVersioning
878
     *
879
     * @param bool $isVersioning isVersioning
880
     *
881
     * @return void
882
     */
883
    public function setIsVersioning($isVersioning)
884
    {
885
        $this->isVersioning = $isVersioning;
886
    }
887
888
    /**
889
     * set searchable variables
890
     *
891
     * @param string[] $searchable array of searchable fields
892
     *
893
     * @return void
894
     */
895
    public function setSearchable($searchable)
896
    {
897
        $this->searchable = $searchable;
898
    }
899
900
    /**
901
     * get searchable fields
902
     *
903
     * @return string[]|null
904
     */
905
    public function getSearchable()
906
    {
907
        if (empty($this->searchable)) {
908
            return null;
909
        }
910
        return $this->searchable;
911
    }
912
913
    /**
914
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
915
     */
916
    public function getTextIndexes()
917
    {
918
        return $this->textIndexes;
919
    }
920
921
    /**
922
     * get textIndexes fields
923
     *
924
     * @param array $textIndexes Data array of special text search values
925
     *
926
     * @return void
927
     */
928
    public function setTextIndexes($textIndexes)
929
    {
930
        $this->textIndexes = $textIndexes;
931
    }
932
933
    /**
934
     * Visible item in schema output
935
     *
936
     * @return bool
937
     */
938
    public function isExpose()
939
    {
940
        return (boolean) $this->expose;
941
    }
942
943
    /**
944
     * Set visible en schema serializer
945
     *
946
     * @param bool $expose boolean value
947
     *
948
     * @return void
949
     */
950
    public function setExpose($expose)
951
    {
952
        $this->expose = $expose;
953
    }
954
}
955