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 |
||
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 $groups; |
||
150 | |||
151 | /** |
||
152 | * @var array<string> |
||
153 | */ |
||
154 | protected $constraints; |
||
155 | |||
156 | /** |
||
157 | * @var array<string> |
||
158 | */ |
||
159 | protected $textIndexes; |
||
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() |
|
222 | |||
223 | /** |
||
224 | * set title |
||
225 | * |
||
226 | * @param string $title title |
||
227 | * |
||
228 | * @return void |
||
229 | */ |
||
230 | 2 | public function setTitle($title) |
|
234 | |||
235 | /** |
||
236 | * get title |
||
237 | * |
||
238 | * @return string |
||
239 | */ |
||
240 | 2 | public function getTitle() |
|
244 | |||
245 | /** |
||
246 | * set description |
||
247 | * |
||
248 | * @param string $description description |
||
249 | * |
||
250 | * @return void |
||
251 | */ |
||
252 | public function setDescription($description) |
||
256 | |||
257 | /** |
||
258 | * get description |
||
259 | * |
||
260 | * @return string |
||
261 | */ |
||
262 | public function getDescription() |
||
266 | |||
267 | /** |
||
268 | * set type |
||
269 | * |
||
270 | * @param string|array $types types |
||
271 | * |
||
272 | * @return void |
||
273 | */ |
||
274 | public function setType($types) |
||
309 | |||
310 | /** |
||
311 | * get type |
||
312 | * |
||
313 | * @return SchemaType type |
||
314 | */ |
||
315 | public function getType() |
||
319 | |||
320 | /** |
||
321 | * get MinLengthTypes |
||
322 | * |
||
323 | * @return array MinLengthTypes |
||
324 | */ |
||
325 | public function getMinLengthTypes() |
||
329 | |||
330 | /** |
||
331 | * get format |
||
332 | * |
||
333 | * @return string format |
||
334 | */ |
||
335 | public function getFormat() |
||
339 | |||
340 | /** |
||
341 | * sets format |
||
342 | * |
||
343 | * @param string $format format |
||
344 | * |
||
345 | * @return void |
||
346 | */ |
||
347 | public function setFormat($format) |
||
351 | |||
352 | /** |
||
353 | * get numeric minimum |
||
354 | * |
||
355 | * @return float numeric minimum |
||
356 | */ |
||
357 | public function getNumericMinimum() |
||
361 | |||
362 | /** |
||
363 | * set numeric minimum |
||
364 | * |
||
365 | * @param float $numericMinimum numeric mimimum |
||
366 | * |
||
367 | * @return void |
||
368 | */ |
||
369 | public function setNumericMinimum($numericMinimum) |
||
373 | |||
374 | /** |
||
375 | * get numeric maximum |
||
376 | * |
||
377 | * @return float numeric maximum |
||
378 | */ |
||
379 | public function getNumericMaximum() |
||
383 | |||
384 | /** |
||
385 | * set numeric maximum |
||
386 | * |
||
387 | * @param float $numericMaximum maximum |
||
388 | * |
||
389 | * @return void |
||
390 | */ |
||
391 | public function setNumericMaximum($numericMaximum) |
||
395 | |||
396 | /** |
||
397 | * set min length |
||
398 | * |
||
399 | * @return int length |
||
400 | */ |
||
401 | public function getMinLength() |
||
405 | |||
406 | /** |
||
407 | * get min length |
||
408 | * |
||
409 | * @param int $minLength length |
||
410 | * |
||
411 | * @return void |
||
412 | */ |
||
413 | public function setMinLength($minLength) |
||
417 | |||
418 | /** |
||
419 | * gets maxlength |
||
420 | * |
||
421 | * @return int length |
||
422 | */ |
||
423 | public function getMaxLength() |
||
427 | |||
428 | /** |
||
429 | * set maxlength |
||
430 | * |
||
431 | * @param int $maxLength length |
||
432 | * |
||
433 | * @return void |
||
434 | */ |
||
435 | public function setMaxLength($maxLength) |
||
439 | |||
440 | /** |
||
441 | * set min Items |
||
442 | * |
||
443 | * @return int Items |
||
444 | */ |
||
445 | public function getMinItems() |
||
449 | |||
450 | /** |
||
451 | * get min Items |
||
452 | * |
||
453 | * @param int $minItems length |
||
454 | * |
||
455 | * @return void |
||
456 | */ |
||
457 | public function setMinItems($minItems) |
||
461 | |||
462 | /** |
||
463 | * gets maxItems |
||
464 | * |
||
465 | * @return int Items |
||
466 | */ |
||
467 | public function getMaxItems() |
||
471 | |||
472 | /** |
||
473 | * set maxItems |
||
474 | * |
||
475 | * @param int $maxItems Items |
||
476 | * |
||
477 | * @return void |
||
478 | */ |
||
479 | public function setMaxItems($maxItems) |
||
483 | |||
484 | /** |
||
485 | * get Enum |
||
486 | * |
||
487 | * @return array Enum |
||
|
|||
488 | */ |
||
489 | public function getEnum() |
||
493 | |||
494 | /** |
||
495 | * set Enum |
||
496 | * |
||
497 | * @param array $enum enum |
||
498 | * |
||
499 | * @return void |
||
500 | */ |
||
501 | public function setEnum(array $enum) |
||
505 | |||
506 | /** |
||
507 | * get regex pattern |
||
508 | * |
||
509 | * @return string pattern |
||
510 | */ |
||
511 | public function getRegexPattern() |
||
515 | |||
516 | /** |
||
517 | * set regex pattern |
||
518 | * |
||
519 | * @param string $regexPattern regex pattern |
||
520 | * |
||
521 | * @return void |
||
522 | */ |
||
523 | public function setRegexPattern($regexPattern) |
||
527 | |||
528 | /** |
||
529 | * get DocumentClass |
||
530 | * |
||
531 | * @return string DocumentClass |
||
532 | */ |
||
533 | public function getDocumentClass() |
||
537 | |||
538 | /** |
||
539 | * set DocumentClass |
||
540 | * |
||
541 | * @param string $documentClass documentClass |
||
542 | * |
||
543 | * @return void |
||
544 | */ |
||
545 | public function setDocumentClass($documentClass) |
||
549 | |||
550 | /** |
||
551 | * get Groups |
||
552 | * |
||
553 | * @return array Groups |
||
554 | */ |
||
555 | public function getGroups() |
||
556 | { |
||
557 | return $this->groups; |
||
558 | } |
||
559 | |||
560 | /** |
||
561 | * set Groups |
||
562 | * |
||
563 | * @param array $groups groups |
||
564 | * |
||
565 | * @return void |
||
566 | */ |
||
567 | public function setGroups($groups) |
||
568 | { |
||
569 | $this->groups = $groups; |
||
570 | } |
||
571 | |||
572 | /** |
||
573 | * get Constraints |
||
574 | * |
||
575 | * @return mixed Constraints |
||
576 | */ |
||
577 | public function getConstraints() |
||
578 | { |
||
579 | return $this->constraints; |
||
580 | } |
||
581 | |||
582 | /** |
||
583 | * set Constraints |
||
584 | * |
||
585 | * @param mixed $constraints constraints |
||
586 | * |
||
587 | * @return void |
||
588 | */ |
||
589 | public function setConstraints($constraints) |
||
590 | { |
||
591 | $this->constraints = $constraints; |
||
592 | } |
||
593 | |||
594 | /** |
||
595 | * add a constraint |
||
596 | * |
||
597 | * @param string $name constraint name |
||
598 | * |
||
599 | * @return void |
||
600 | */ |
||
601 | public function addConstraint($name) |
||
602 | { |
||
603 | $this->constraints[] = $name; |
||
604 | } |
||
605 | |||
606 | /** |
||
607 | * set items |
||
608 | * |
||
609 | * @param Schema $items items schema |
||
610 | * |
||
611 | * @return void |
||
612 | */ |
||
613 | public function setItems($items) |
||
614 | { |
||
615 | $this->items = $items; |
||
616 | } |
||
617 | |||
618 | /** |
||
619 | * get items |
||
620 | * |
||
621 | * @return Schema |
||
622 | */ |
||
623 | public function getItems() |
||
624 | { |
||
625 | return $this->items; |
||
626 | } |
||
627 | |||
628 | /** |
||
629 | * add a property |
||
630 | * |
||
631 | * @param string $name property name |
||
632 | * @param Schema $property property |
||
633 | * |
||
634 | * @return void |
||
635 | */ |
||
636 | public function addProperty($name, $property) |
||
637 | { |
||
638 | $this->properties->set($name, $property); |
||
639 | } |
||
640 | |||
641 | /** |
||
642 | * removes a property |
||
643 | * |
||
644 | * @param string $name property name |
||
645 | * |
||
646 | * @return void |
||
647 | */ |
||
648 | public function removeProperty($name) |
||
654 | |||
655 | /** |
||
656 | * returns a property |
||
657 | * |
||
658 | * @param string $name property name |
||
659 | * |
||
660 | * @return null|Schema property |
||
661 | */ |
||
662 | public function getProperty($name = null) |
||
663 | { |
||
664 | if (is_null($name)) { |
||
665 | return null; |
||
666 | } |
||
667 | return $this->properties->get($name); |
||
668 | } |
||
669 | |||
670 | /** |
||
671 | * get properties |
||
672 | * |
||
673 | * @return ArrayCollection|null |
||
674 | */ |
||
675 | public function getProperties() |
||
676 | { |
||
677 | if ($this->properties->isEmpty()) { |
||
678 | return null; |
||
679 | } else { |
||
680 | return $this->properties; |
||
681 | } |
||
682 | } |
||
683 | |||
684 | /** |
||
685 | * set additionalProperties on schema |
||
686 | * |
||
687 | * @param SchemaAdditionalProperties $additionalProperties additional properties |
||
688 | * |
||
689 | * @return void |
||
690 | */ |
||
691 | public function setAdditionalProperties(SchemaAdditionalProperties $additionalProperties) |
||
692 | { |
||
693 | $this->additionalProperties = $additionalProperties; |
||
694 | } |
||
695 | |||
696 | /** |
||
697 | * get addtionalProperties for schema |
||
698 | * |
||
699 | * @return SchemaAdditionalProperties |
||
700 | */ |
||
701 | public function getAdditionalProperties() |
||
705 | |||
706 | /** |
||
707 | * set required variables |
||
708 | * |
||
709 | * @param string[] $required array of required fields |
||
710 | * |
||
711 | * @return void |
||
712 | */ |
||
713 | public function setRequired(array $required) |
||
714 | { |
||
715 | // needed as indexes could we off and we want to enforce an array after json_encode |
||
716 | $this->required = array_values($required); |
||
717 | } |
||
718 | |||
719 | /** |
||
720 | * get required fields |
||
721 | * |
||
722 | * @return string[]|null |
||
723 | */ |
||
724 | public function getRequired() |
||
725 | { |
||
726 | $required = $this->required; |
||
727 | if (empty($required)) { |
||
728 | $required = null; |
||
729 | } |
||
730 | |||
731 | return $required; |
||
732 | } |
||
733 | |||
734 | /** |
||
735 | * set translatable flag |
||
736 | * |
||
737 | * This flag is a local extension to json schema. |
||
738 | * |
||
739 | * @param boolean $translatable translatable flag |
||
740 | * |
||
741 | * @return void |
||
742 | */ |
||
743 | public function setTranslatable($translatable) |
||
744 | { |
||
745 | if ($translatable === true) { |
||
746 | $this->setType('translatable'); |
||
747 | } else { |
||
748 | $this->setType('string'); |
||
749 | } |
||
750 | } |
||
751 | |||
752 | /** |
||
753 | * get translatable flag |
||
754 | * |
||
755 | * @return boolean |
||
756 | */ |
||
757 | public function isTranslatable() |
||
766 | |||
767 | /** |
||
768 | * set a array of urls that can extref refer to |
||
769 | * |
||
770 | * @param array $refCollection urls |
||
771 | * |
||
772 | * @return void |
||
773 | */ |
||
774 | public function setRefCollection(array $refCollection) |
||
778 | |||
779 | /** |
||
780 | * get a collection of urls that can extref refer to |
||
781 | * |
||
782 | * @return array |
||
783 | */ |
||
784 | public function getRefCollection() |
||
793 | |||
794 | /** |
||
795 | * set an array of possible event names |
||
796 | * |
||
797 | * @param array $eventNames event names |
||
798 | * |
||
799 | * @return void |
||
800 | */ |
||
801 | public function setEventNames(array $eventNames) |
||
805 | |||
806 | /** |
||
807 | * get a collection of possible event names |
||
808 | * |
||
809 | * @return array |
||
810 | */ |
||
811 | public function getEventNames() |
||
812 | { |
||
813 | $collection = $this->eventNames; |
||
814 | if (empty($collection)) { |
||
815 | $collection = null; |
||
816 | } |
||
817 | |||
818 | return $collection; |
||
819 | } |
||
820 | |||
821 | /** |
||
822 | * Set the readOnly flag |
||
823 | * |
||
824 | * @param bool $readOnly ReadOnly flag |
||
825 | * |
||
826 | * @return void |
||
827 | */ |
||
828 | public function setReadOnly($readOnly) |
||
832 | |||
833 | /** |
||
834 | * Get the readOnly flag. |
||
835 | * Returns null if the flag is set to false so the serializer will ignore it. |
||
836 | * |
||
837 | * @return bool|null true if readOnly isset to true or null if not |
||
838 | */ |
||
839 | public function getReadOnly() |
||
843 | |||
844 | /** |
||
845 | * get RecordOriginModifiable |
||
846 | * |
||
847 | * @return boolean RecordOriginModifiable |
||
848 | */ |
||
849 | public function isRecordOriginModifiable() |
||
853 | |||
854 | /** |
||
855 | * set RecordOriginModifiable |
||
856 | * |
||
857 | * @param boolean $recordOriginModifiable recordOriginModifiable |
||
858 | * |
||
859 | * @return void |
||
860 | */ |
||
861 | public function setRecordOriginModifiable($recordOriginModifiable) |
||
865 | |||
866 | /** |
||
867 | * get RecordOriginException |
||
868 | * |
||
869 | * @return boolean RecordOriginException |
||
870 | */ |
||
871 | public function isRecordOriginException() |
||
875 | |||
876 | /** |
||
877 | * set RecordOriginException |
||
878 | * |
||
879 | * @param boolean $recordOriginException recordOriginException |
||
880 | * |
||
881 | * @return void |
||
882 | */ |
||
883 | public function setRecordOriginException($recordOriginException) |
||
887 | |||
888 | /** |
||
889 | * isVersioning |
||
890 | * |
||
891 | * @return bool IsVersioning |
||
892 | */ |
||
893 | public function isVersioning() |
||
897 | |||
898 | /** |
||
899 | * set IsVersioning |
||
900 | * |
||
901 | * @param bool $isVersioning isVersioning |
||
902 | * |
||
903 | * @return void |
||
904 | */ |
||
905 | public function setIsVersioning($isVersioning) |
||
909 | |||
910 | /** |
||
911 | * set searchable variables |
||
912 | * |
||
913 | * @param string[] $searchable array of searchable fields |
||
914 | * |
||
915 | * @return void |
||
916 | */ |
||
917 | public function setSearchable($searchable) |
||
921 | |||
922 | /** |
||
923 | * get searchable fields |
||
924 | * |
||
925 | * @return string[]|null |
||
926 | */ |
||
927 | public function getSearchable() |
||
934 | |||
935 | /** |
||
936 | * @return array |
||
937 | */ |
||
938 | public function getTextIndexes() |
||
942 | |||
943 | /** |
||
944 | * get textIndexes fields |
||
945 | * |
||
946 | * @param array $textIndexes Data array of special text search values |
||
947 | * |
||
948 | * @return void |
||
949 | */ |
||
950 | public function setTextIndexes($textIndexes) |
||
954 | } |
||
955 |
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.