Failed Conditions
Push — master ( 2b713c...735d0a )
by Alex
16s queued 13s
created

ToTraceString::appendFacets()   D

Complexity

Conditions 23
Paths 23

Size

Total Lines 37
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 23
eloc 34
nc 23
nop 2
dl 0
loc 37
rs 4.1666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace AlgoWeb\ODataMetadata\Helpers;
7
8
use AlgoWeb\ODataMetadata\EdmConstants;
9
use AlgoWeb\ODataMetadata\Enums\PrimitiveTypeKind;
10
use AlgoWeb\ODataMetadata\Enums\TypeKind;
11
use AlgoWeb\ODataMetadata\Interfaces\IBinaryTypeReference;
12
use AlgoWeb\ODataMetadata\Interfaces\ICollectionType;
13
use AlgoWeb\ODataMetadata\Interfaces\IDecimalTypeReference;
14
use AlgoWeb\ODataMetadata\Interfaces\IEdmElement;
15
use AlgoWeb\ODataMetadata\Interfaces\IEntityReferenceType;
16
use AlgoWeb\ODataMetadata\Interfaces\IPrimitiveTypeReference;
17
use AlgoWeb\ODataMetadata\Interfaces\IProperty;
18
use AlgoWeb\ODataMetadata\Interfaces\IRowType;
19
use AlgoWeb\ODataMetadata\Interfaces\ISchemaElement;
20
use AlgoWeb\ODataMetadata\Interfaces\ISpatialTypeReference;
21
use AlgoWeb\ODataMetadata\Interfaces\IStringTypeReference;
22
use AlgoWeb\ODataMetadata\Interfaces\ITemporalTypeReference;
23
use AlgoWeb\ODataMetadata\Interfaces\ITypeReference;
24
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)
203
    {
204
        $s .= ' ';
205
        $s .= $key;
206
        $s .= '=';
207
        $s .= $value;
208
        return $s;
209
    }
210
}
211