Conditions | 27 |
Paths | 254 |
Total Lines | 67 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
49 | public function normalize($object, $format = null, array $context = []) |
||
50 | { |
||
51 | $data = $this->documentationNormalizer->normalize($object, $format, $context); |
||
52 | if (!\is_array($data)) { |
||
53 | throw new UnexpectedValueException('Expected data to be an array'); |
||
54 | } |
||
55 | |||
56 | if (!($context[self::API_GATEWAY] ?? $this->defaultContext[self::API_GATEWAY])) { |
||
57 | return $data; |
||
58 | } |
||
59 | |||
60 | if (empty($data['basePath'])) { |
||
61 | $data['basePath'] = '/'; |
||
62 | } |
||
63 | |||
64 | foreach ($data['paths'] as $path => $operations) { |
||
65 | foreach ($operations as $operation => $options) { |
||
66 | if (isset($options['parameters'])) { |
||
67 | foreach ($options['parameters'] as $key => $parameter) { |
||
68 | if (!preg_match('/^[a-zA-Z0-9._$-]+$/', $parameter['name'])) { |
||
69 | unset($data['paths'][$path][$operation]['parameters'][$key]); |
||
70 | } |
||
71 | if (isset($parameter['schema']['$ref']) && $this->isLocalRef($parameter['schema']['$ref'])) { |
||
72 | $data['paths'][$path][$operation]['parameters'][$key]['schema']['$ref'] = $this->normalizeRef($parameter['schema']['$ref']); |
||
73 | } |
||
74 | } |
||
75 | $data['paths'][$path][$operation]['parameters'] = array_values($data['paths'][$path][$operation]['parameters']); |
||
76 | } |
||
77 | if (isset($options['responses'])) { |
||
78 | foreach ($options['responses'] as $statusCode => $response) { |
||
79 | if (isset($response['schema']['items']['$ref']) && $this->isLocalRef($response['schema']['items']['$ref'])) { |
||
80 | $data['paths'][$path][$operation]['responses'][$statusCode]['schema']['items']['$ref'] = $this->normalizeRef($response['schema']['items']['$ref']); |
||
81 | } |
||
82 | if (isset($response['schema']['$ref']) && $this->isLocalRef($response['schema']['$ref'])) { |
||
83 | $data['paths'][$path][$operation]['responses'][$statusCode]['schema']['$ref'] = $this->normalizeRef($response['schema']['$ref']); |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | } |
||
89 | |||
90 | foreach ($data['definitions'] as $definition => $options) { |
||
91 | if (!isset($options['properties'])) { |
||
92 | continue; |
||
93 | } |
||
94 | foreach ($options['properties'] as $property => $propertyOptions) { |
||
95 | if (isset($propertyOptions['readOnly'])) { |
||
96 | unset($data['definitions'][$definition]['properties'][$property]['readOnly']); |
||
97 | } |
||
98 | if (isset($propertyOptions['$ref']) && $this->isLocalRef($propertyOptions['$ref'])) { |
||
99 | $data['definitions'][$definition]['properties'][$property]['$ref'] = $this->normalizeRef($propertyOptions['$ref']); |
||
100 | } |
||
101 | if (isset($propertyOptions['items']['$ref']) && $this->isLocalRef($propertyOptions['items']['$ref'])) { |
||
102 | $data['definitions'][$definition]['properties'][$property]['items']['$ref'] = $this->normalizeRef($propertyOptions['items']['$ref']); |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | |||
107 | // $data['definitions'] is an instance of \ArrayObject |
||
108 | foreach (array_keys($data['definitions']->getArrayCopy()) as $definition) { |
||
109 | if (!preg_match('/^[0-9A-Za-z]+$/', (string) $definition)) { |
||
110 | $data['definitions'][preg_replace('/[^0-9A-Za-z]/', '', (string) $definition)] = $data['definitions'][$definition]; |
||
111 | unset($data['definitions'][$definition]); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | return $data; |
||
116 | } |
||
150 |