Total Complexity | 57 |
Total Lines | 184 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 1 | Features | 0 |
Complex classes like ToTraceString 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.
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 ToTraceString, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | abstract class ToTraceString |
||
25 | { |
||
26 | /** |
||
27 | * Returns the text representation of the current object. |
||
28 | * |
||
29 | * @param IEdmElement|null $element reference to the calling object |
||
30 | * @return string the text representation of the current object |
||
31 | */ |
||
32 | public static function toTraceString(?IEdmElement $element): string |
||
33 | { |
||
34 | switch (true) { |
||
35 | case $element instanceof ISchemaElement: |
||
36 | assert($element instanceof ISchemaElement); |
||
37 | return $element->fullName(); |
||
38 | case $element instanceof ITypeReference: |
||
39 | assert($element instanceof ITypeReference); |
||
40 | $s = '['; |
||
41 | $s .= self::toTraceString($element->getDefinition()); |
||
42 | $s = self::appendKeyValue( |
||
43 | $s, |
||
44 | EdmConstants::FacetName_Nullable, |
||
45 | $element->getNullable() ? 'TRUE' : 'FALSE' |
||
46 | ); |
||
47 | if ($element->isPrimitive()) { |
||
48 | $s = self::appendFacets($s, $element->asPrimitive()); |
||
49 | } |
||
50 | $s .= ']'; |
||
51 | return $s; |
||
52 | case $element instanceof IProperty: |
||
53 | assert($element instanceof IProperty); |
||
54 | $type = $element->getType(); |
||
55 | return $element->getName() ?? '' . ':' . ($type !== null ? self::toTraceString($type) : ''); |
||
56 | case $element instanceof IEntityReferenceType: |
||
57 | assert($element instanceof IEntityReferenceType); |
||
58 | return strval(TypeKind::EntityReference()->getKey()) . '(' . (null !== $element->getEntityType() ? |
||
59 | self::toTraceString($element->getEntityType()) : '') . ')'; |
||
60 | case $element instanceof ICollectionType: |
||
61 | assert($element instanceof ICollectionType); |
||
62 | return strval(TypeKind::Collection()->getKey()) . '(' . (null !== $element->getElementType() ? |
||
63 | self::toTraceString($element->getElementType()) : '') . ')'; |
||
64 | case $element instanceof IRowType: |
||
65 | assert($element instanceof IRowType); |
||
66 | $s = TypeKind::Row()->getKey(); |
||
67 | $s .= '('; |
||
68 | foreach ($element->properties() as $prop) { |
||
69 | if (null === $prop) { |
||
70 | continue; |
||
71 | } |
||
72 | $s .= self::toTraceString($prop); |
||
73 | $s .= ', '; |
||
74 | } |
||
75 | $s = substr($s, 0, -2); |
||
76 | $s .= ')'; |
||
77 | return $s; |
||
78 | default: |
||
79 | return EdmConstants::Value_UnknownType; |
||
80 | } |
||
81 | } |
||
82 | |||
83 | private static function appendFacets(string $s, IPrimitiveTypeReference $type): string |
||
84 | { |
||
85 | switch ($type->primitiveKind()) { |
||
86 | case PrimitiveTypeKind::Binary(): |
||
87 | $s = self::appendBinaryFacets($s, $type->asBinary()); |
||
88 | break; |
||
89 | case PrimitiveTypeKind::Decimal(): |
||
90 | $s = self::appendDecimalFacets($s, $type->asDecimal()); |
||
91 | break; |
||
92 | case PrimitiveTypeKind::String(): |
||
93 | $s = self::appendStringFacets($s, $type->asString()); |
||
94 | break; |
||
95 | case PrimitiveTypeKind::Time(): |
||
96 | case PrimitiveTypeKind::DateTime(): |
||
97 | case PrimitiveTypeKind::DateTimeOffset(): |
||
98 | $s = self::appendTemporalFacets($s, $type->asTemporal()); |
||
99 | break; |
||
100 | case PrimitiveTypeKind::Geography(): |
||
101 | case PrimitiveTypeKind::GeographyPoint(): |
||
102 | case PrimitiveTypeKind::GeographyLineString(): |
||
103 | case PrimitiveTypeKind::GeographyPolygon(): |
||
104 | case PrimitiveTypeKind::GeographyCollection(): |
||
105 | case PrimitiveTypeKind::GeographyMultiPoint(): |
||
106 | case PrimitiveTypeKind::GeographyMultiLineString(): |
||
107 | case PrimitiveTypeKind::GeographyMultiPolygon(): |
||
108 | case PrimitiveTypeKind::Geometry(): |
||
109 | case PrimitiveTypeKind::GeometryPoint(): |
||
110 | case PrimitiveTypeKind::GeometryLineString(): |
||
111 | case PrimitiveTypeKind::GeometryPolygon(): |
||
112 | case PrimitiveTypeKind::GeometryCollection(): |
||
113 | case PrimitiveTypeKind::GeometryMultiPolygon(): |
||
114 | case PrimitiveTypeKind::GeometryMultiLineString(): |
||
115 | case PrimitiveTypeKind::GeometryMultiPoint(): |
||
116 | $s = self::appendSpatialFacets($s, $type->asSpatial()); |
||
117 | break; |
||
118 | } |
||
119 | return $s; |
||
120 | } |
||
121 | |||
122 | |||
123 | private static function appendBinaryFacets(string $sb, IBinaryTypeReference $type): string |
||
124 | { |
||
125 | self::appendKeyValue($sb, EdmConstants::FacetName_FixedLength, $type->isFixedLength() ? 'TRUE' : 'FALSE'); |
||
126 | if ($type->isUnBounded() || null !== $type->getMaxLength()) { |
||
127 | $sb = self::appendKeyValue( |
||
128 | $sb, |
||
129 | EdmConstants::FacetName_MaxLength, |
||
130 | ($type->isUnBounded()) ? EdmConstants::Value_Max : strval($type->getMaxLength()) |
||
131 | ); |
||
132 | } |
||
133 | return $sb; |
||
134 | } |
||
135 | |||
136 | private static function appendStringFacets(string $sb, IStringTypeReference $type): string |
||
137 | { |
||
138 | if (true === $type->isFixedLength()) { |
||
139 | $sb = self::appendKeyValue( |
||
140 | $sb, |
||
141 | EdmConstants::FacetName_FixedLength, |
||
142 | 'TRUE' |
||
143 | ); |
||
144 | } |
||
145 | |||
146 | if (true === $type->isUnbounded() || null !== $type->getMaxLength()) { |
||
147 | $sb = self::appendKeyValue( |
||
148 | $sb, |
||
149 | EdmConstants::FacetName_MaxLength, |
||
150 | $type->isUnbounded() ? EdmConstants::Value_Max : $type->getMaxLength() |
||
151 | ); |
||
152 | } |
||
153 | |||
154 | if (true === $type->isUnicode()) { |
||
155 | $sb = self::appendKeyValue($sb, EdmConstants::FacetName_Unicode, 'TRUE'); |
||
156 | } |
||
157 | |||
158 | if (null !== $type->getCollation()) { |
||
159 | $sb = self::appendKeyValue($sb, EdmConstants::FacetName_Collation, $type->getCollation()); |
||
160 | } |
||
161 | return $sb; |
||
162 | } |
||
163 | |||
164 | private static function appendTemporalFacets(string $sb, ITemporalTypeReference $type): string |
||
165 | { |
||
166 | if (null !== $type->getPrecision()) { |
||
167 | $sb = self::appendKeyValue($sb, EdmConstants::FacetName_Precision, $type->getPrecision()); |
||
168 | } |
||
169 | return $sb; |
||
170 | } |
||
171 | |||
172 | private static function appendDecimalFacets(string $sb, IDecimalTypeReference $type): string |
||
173 | { |
||
174 | if (null !== $type->getPrecision()) { |
||
175 | $sb = self::appendKeyValue($sb, EdmConstants::FacetName_Precision, $type->getPrecision()); |
||
176 | } |
||
177 | |||
178 | if (null !== $type->getScale()) { |
||
179 | $sb = self::appendKeyValue($sb, EdmConstants::FacetName_Scale, $type->getScale()); |
||
180 | } |
||
181 | return $sb; |
||
182 | } |
||
183 | |||
184 | private static function appendSpatialFacets(string $sb, ISpatialTypeReference $type): string |
||
185 | { |
||
186 | $sb = self::appendKeyValue( |
||
187 | $sb, |
||
188 | EdmConstants::FacetName_Srid, |
||
189 | null !== $type->getSpatialReferenceIdentifier() |
||
190 | ? $type->getSpatialReferenceIdentifier() : EdmConstants::Value_SridVariable |
||
191 | ); |
||
192 | return $sb; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @param string $s |
||
197 | * @param string $key |
||
198 | * @param string|int|null $value |
||
199 | * @return string |
||
200 | */ |
||
201 | private static function appendKeyValue(string $s, string $key, $value) |
||
208 | } |
||
209 | } |
||
210 |