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