Complex classes like DefinitionNormalizer 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 DefinitionNormalizer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | final class DefinitionNormalizer extends Normalizer |
||
25 | { |
||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | 21 | public function normalize($object, $format = null, array $context = array()) |
|
30 | { |
||
31 | 21 | if (!$object instanceof Definition) { |
|
32 | return; |
||
33 | } |
||
34 | |||
35 | 21 | $data = array(); |
|
36 | |||
37 | 21 | if (null !== $name = $object->getName()) { |
|
38 | 3 | $data['name'] = $this->normalizeAttribute($name, $format, $context); |
|
39 | } |
||
40 | |||
41 | 21 | if (null !== $description = $object->getDescription()) { |
|
42 | 3 | $data['description'] = $this->normalizeAttribute($description, $format, $context); |
|
43 | } |
||
44 | |||
45 | 21 | if (null !== $type = $object->getType()) { |
|
46 | 3 | $data['type'] = $type; |
|
47 | } |
||
48 | |||
49 | 21 | if (null !== $moreInfo = $object->getMoreInfo()) { |
|
50 | 2 | $data['moreInfo'] = $moreInfo; |
|
51 | } |
||
52 | |||
53 | 21 | if ($object instanceof InteractionDefinition) { |
|
54 | 10 | if (null !== $correctResponsesPattern = $object->getCorrectResponsesPattern()) { |
|
55 | 1 | $data['correctResponsesPattern'] = $object->getCorrectResponsesPattern(); |
|
56 | } |
||
57 | |||
58 | switch (true) { |
||
59 | 10 | case $object instanceof ChoiceInteractionDefinition: |
|
60 | 1 | $data['interactionType'] = 'choice'; |
|
61 | |||
62 | 1 | if (null !== $choices = $object->getChoices()) { |
|
63 | 1 | $data['choices'] = $this->normalizeAttribute($choices, $format, $context); |
|
64 | } |
||
65 | 1 | break; |
|
66 | case $object instanceof FillInInteractionDefinition: |
||
67 | 1 | $data['interactionType'] = 'fill-in'; |
|
68 | 1 | break; |
|
69 | case $object instanceof LikertInteractionDefinition: |
||
70 | 1 | $data['interactionType'] = 'likert'; |
|
71 | |||
72 | 1 | if (null !== $scale = $object->getScale()) { |
|
73 | 1 | $data['scale'] = $this->normalizeAttribute($scale, $format, $context); |
|
74 | } |
||
75 | 1 | break; |
|
76 | 7 | case $object instanceof LongFillInInteractionDefinition: |
|
77 | $data['interactionType'] = 'long-fill-in'; |
||
78 | break; |
||
79 | case $object instanceof MatchingInteractionDefinition: |
||
80 | 1 | $data['interactionType'] = 'matching'; |
|
81 | |||
82 | 1 | if (null !== $source = $object->getSource()) { |
|
83 | 1 | $data['source'] = $this->normalizeAttribute($source, $format, $context); |
|
84 | } |
||
85 | |||
86 | 1 | if (null !== $target = $object->getTarget()) { |
|
87 | 1 | $data['target'] = $this->normalizeAttribute($target, $format, $context); |
|
88 | } |
||
89 | 1 | break; |
|
90 | case $object instanceof NumericInteractionDefinition: |
||
91 | 1 | $data['interactionType'] = 'numeric'; |
|
92 | 1 | break; |
|
93 | case $object instanceof OtherInteractionDefinition: |
||
94 | 2 | $data['interactionType'] = 'other'; |
|
95 | 2 | break; |
|
96 | case $object instanceof PerformanceInteractionDefinition: |
||
97 | 1 | $data['interactionType'] = 'performance'; |
|
98 | |||
99 | 1 | if (null !== $steps = $object->getSteps()) { |
|
100 | 1 | $data['steps'] = $this->normalizeAttribute($steps, $format, $context); |
|
101 | } |
||
102 | 1 | break; |
|
103 | case $object instanceof SequencingInteractionDefinition: |
||
104 | 1 | $data['interactionType'] = 'sequencing'; |
|
105 | |||
106 | 1 | if (null !== $choices = $object->getChoices()) { |
|
107 | 1 | $data['choices'] = $this->normalizeAttribute($choices, $format, $context); |
|
108 | } |
||
109 | 1 | break; |
|
110 | case $object instanceof TrueFalseInteractionDefinition: |
||
111 | 1 | $data['interactionType'] = 'true-false'; |
|
112 | 1 | break; |
|
113 | } |
||
114 | } |
||
115 | |||
116 | 21 | if (empty($data)) { |
|
117 | 5 | return new \stdClass(); |
|
|
|||
118 | } |
||
119 | |||
120 | 16 | return $data; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | 139 | public function supportsNormalization($data, $format = null) |
|
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | 23 | public function denormalize($data, $class, $format = null, array $context = array()) |
|
218 | |||
219 | /** |
||
220 | * {@inheritdoc} |
||
221 | */ |
||
222 | 145 | public function supportsDenormalization($data, $type, $format = null) |
|
240 | } |
||
241 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.