Converter   B
last analyzed

Complexity

Total Complexity 52

Size/Duplication

Total Lines 268
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 98.5%

Importance

Changes 0
Metric Value
dl 0
loc 268
ccs 131
cts 133
cp 0.985
rs 7.9487
c 0
b 0
f 0
wmc 52
lcom 1
cbo 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A convert() 0 7 1
D convertSchema() 0 40 12
B convertProperties() 0 21 5
C convertTypes() 0 69 18
A cleanRequired() 0 10 3
A convertPatternProperties() 0 7 1
A patternPropertiesHandler() 0 17 4
B createOptions() 0 24 3
A resolveNotSupported() 0 4 1
A removeFromSchema() 0 12 2

How to fix   Complexity   

Complex Class

Complex classes like Converter 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 Converter, and based on these observations, apply Extract Interface, too.

1
<?php namespace HSkrasek\OpenAPI;
2
3
class Converter
4
{
5
    private const STRUCTS = [
6
        'allOf',
7
        'anyOf',
8
        'oneOf',
9
        'not',
10
        'items',
11
        'additionalProperties',
12
    ];
13
14
    private const NOT_SUPPORTED = [
15
        'nullable',
16
        'discriminator',
17
        'readOnly',
18
        'writeOnly',
19
        'xml',
20
        'externalDocs',
21
        'example',
22
        'deprecated',
23
    ];
24
25
    /**
26
     * @var array
27
     */
28
    private $options;
29
30 138
    public function __construct(?array $options = null)
31
    {
32 138
        $this->options = $this->createOptions($options ?: []);
33 138
    }
34
35
    /**
36
     * @param mixed $schema
37
     *
38
     * @return mixed
39
     */
40 138
    public function convert($schema)
41
    {
42 138
        $schema = $this->convertSchema($schema);
43 138
        data_set($schema, '$schema', 'http://json-schema.org/draft-04/schema#');
44
45 138
        return $schema;
46
    }
47
48
    /**
49
     * @param mixed $schema
50
     *
51
     * @return mixed
52
     */
53 138
    private function convertSchema($schema)
54
    {
55 138
        foreach (self::STRUCTS as $i => $struct) {
56 138
            if (\is_array(data_get($schema, $struct))) {
57 24
                foreach ($schema->$struct as $j => $nestedStruct) {
58 24
                    $schema->$struct[$j] = $this->convertSchema($nestedStruct);
59
                }
60 138
            } elseif (\is_object(data_get($schema, $struct))) {
61 138
                $schema->$struct = $this->convertSchema($schema->$struct);
62
            }
63
        }
64
65 138
        if (\is_object($properties = data_get($schema, 'properties'))) {
66 69
            data_set($schema, 'properties', $this->convertProperties($properties));
67
68 69
            if (\is_array($required = data_get($schema, 'required'))) {
69 27
                data_set($schema, 'required', $required = $this->cleanRequired($required, $properties));
70
71 27
                if (\count($required) === 0) {
72 3
                    $this->removeFromSchema($schema, 'required');
73
                }
74
            }
75
76 69
            if (\count(get_object_vars($properties)) === 0) {
77 6
                $this->removeFromSchema($schema, 'properties');
78
            }
79
        }
80
81 138
        $schema = $this->convertTypes($schema);
82
83 138
        if (\is_object(data_get($schema, 'x-patternProperties')) && $this->options['support_pattern_properties']) {
84 27
            $schema = $this->convertPatternProperties($schema, $this->options['pattern_properties_handler']);
85
        }
86
87 138
        foreach ($this->options['not_supported'] as $notSupported) {
88 138
            $this->removeFromSchema($schema, $notSupported);
89
        }
90
91 138
        return $schema;
92
    }
93
94 69
    private function convertProperties($properties)
95
    {
96 69
        foreach ($properties as $key => $property) {
97 69
            $removeProperty = false;
98
99 69
            foreach ($this->options['remove_properties'] as $prop) {
100 21
                if (data_get($property, $prop) === true) {
101 21
                    $removeProperty = true;
102
                }
103
            }
104
105 69
            if ($removeProperty) {
106 21
                $this->removeFromSchema($properties, $key);
107 21
                continue;
108
            }
109
110 66
            data_set($properties, $key, $this->convertSchema($property));
111
        }
112
113 69
        return $properties;
114
    }
115
116
    /**
117
     * @param mixed $schema
118
     *
119
     * @return mixed
120
     */
121 138
    private function convertTypes($schema)
122
    {
123 138
        if (null === data_get($schema, 'type')) {
124 24
            return $schema;
125
        }
126
127 138
        if (data_get($schema, 'type') === 'string' && data_get(
128 87
            $schema,
129 87
            'format'
130 138
        ) === 'date' && $this->options['convert_date'] === true) {
131 3
            data_set($schema, 'format', 'date-time');
132
        }
133
134 138
        $newType   = null;
0 ignored issues
show
Unused Code introduced by
$newType is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
135 138
        $newFormat = null;
136
137 138
        switch (data_get($schema, 'type')) {
138 92
            case 'integer':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
139 12
                $newType = 'integer';
140 12
                break;
141 92
            case 'long':
142 12
                $newType   = 'integer';
143 12
                $newFormat = 'int64';
144 12
                break;
145 90
            case 'float':
146 3
                $newType   = 'number';
147 3
                $newFormat = 'float';
148 3
                break;
149 90
            case 'double':
150 12
                $newType   = 'number';
151 12
                $newFormat = 'double';
152 12
                break;
153 90
            case 'byte':
154 3
                $newType   = 'string';
155 3
                $newFormat = 'byte';
156 3
                break;
157 88
            case 'binary':
158 3
                $newType   = 'string';
159 3
                $newFormat = 'binary';
160 3
                break;
161 86
            case 'date':
162 3
                $newType   = 'string';
163 3
                $newFormat = $this->options['convert_date'] ? 'date-time' : 'date';
164 3
                break;
165 86
            case 'dateTime':
166 6
                $newType   = 'string';
167 6
                $newFormat = 'date-time';
168 6
                break;
169 84
            case 'password':
170 9
                $newType   = 'string';
171 9
                $newFormat = 'password';
172 9
                break;
173
            default:
174 123
                $newType = data_get($schema, 'type');
175
        }
176
177 138
        data_set($schema, 'type', $newType);
178 138
        data_set($schema, 'format', \is_string($newFormat) ? $newFormat : data_get($schema, 'format'));
179
180 138
        if (null === data_get($schema, 'format')) {
181 120
            $this->removeFromSchema($schema, 'format');
182
        }
183
184 138
        if (data_get($schema, 'nullable', false) === true) {
185 12
            data_set($schema, 'type', [data_get($schema, 'type'), 'null']);
186
        }
187
188 138
        return $schema;
189
    }
190
191 27
    private function cleanRequired(?array $required = [], $properties = null): array
192
    {
193 27
        foreach ($required as $key => $requiredProperty) {
194 27
            if (!isset($properties->{$requiredProperty}, $properties)) {
195 27
                unset($required[$key]);
196
            }
197
        }
198
199 27
        return array_values($required);
200
    }
201
202 27
    private function convertPatternProperties($schema, callable $handler)
203
    {
204 27
        data_set($schema, 'patternProperties', data_get($schema, 'x-patternProperties'));
205 27
        $this->removeFromSchema($schema, 'x-patternProperties');
206
207 27
        return call_user_func($handler, $schema);
208
    }
209
210 24
    private function patternPropertiesHandler($schema)
211
    {
212 24
        $patternProperties = data_get($schema, 'patternProperties');
213
214 24
        if (!\is_object($additionalProperties = data_get($schema, 'additionalProperties'))) {
215 3
            return $schema;
216
        }
217
218 21
        foreach ($patternProperties as $patternProperty) {
219 21
            if ($patternProperty == $additionalProperties) {
220 18
                data_set($schema, 'additionalProperties', false);
221 21
                break;
222
            }
223
        }
224
225 21
        return $schema;
226
    }
227
228 138
    private function createOptions(array $options): array
229
    {
230 138
        $options['convert_date']               = $options['convert_date'] ?? false;
231 138
        $options['support_pattern_properties'] = $options['support_pattern_properties'] ?? false;
232 138
        $options['keep_not_supported']         = $options['keep_not_supported'] ?? [];
233 138
        $options['pattern_properties_handler'] = $options['pattern_properties_handler'] ?? [
234 135
                $this,
235 135
                'patternPropertiesHandler',
236
            ];
237
238 138
        $options['remove_properties'] = [];
239
240 138
        if ($options['remove_read_only'] ?? false) {
241 18
            $options['remove_properties'][] = 'readOnly';
242
        }
243
244 138
        if ($options['remove_write_only'] ?? false) {
245 3
            $options['remove_properties'][] = 'writeOnly';
246
        }
247
248 138
        $options['not_supported'] = $this->resolveNotSupported(self::NOT_SUPPORTED, $options['keep_not_supported']);
249
250 138
        return $options;
251
    }
252
253 138
    private function resolveNotSupported(array $notSupported, array $toRetain): array
254
    {
255 138
        return array_values(array_diff($notSupported, $toRetain));
256
    }
257
258 138
    private function removeFromSchema($schema, string $key): void
259
    {
260 138
        if (\is_object($schema)) {
261 138
            unset($schema->{$key});
262
263 138
            return;
264
        }
265
266
        unset($schema[$key]);
267
268
        return;
269
    }
270
}
271