Skip to content

Commit 4b3068a

Browse files
CR and static analisys fixes
1 parent 1a30348 commit 4b3068a

18 files changed

+304
-158
lines changed

example/gen/src/Serializer/BodySerializer.php

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use InvalidArgumentException;
1616
use OpenApi\PetStoreClient\Request\RequestInterface;
1717
use OpenApi\PetStoreClient\Serializer\ContentType\ContentTypeSerializerInterface;
18-
use OpenApi\PetStoreClient\Serializer\ContentType\Json\Json;
1918
use Psr\Http\Message\ResponseInterface;
2019
use Throwable;
2120

example/gen/src/Serializer/ContentType/ContentTypeSerializerInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
interface ContentTypeSerializerInterface
1717
{
18-
public const LITERAL_VALUE_KEY = '__literalResponseValue';
18+
const LITERAL_VALUE_KEY = '__literalResponseValue';
1919

2020
public function encode(SerializableInterface $body): string;
2121

example/gen/src/Serializer/ContentType/XmlContentTypeSerializer.php

+99-52
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,47 @@ class XmlContentTypeSerializer implements ContentTypeSerializerInterface
2424

2525
const ATTRIBUTE_NAMESPACE_SEPARATOR = ':';
2626

27-
protected array $config = ['version' => '1.0', 'encoding' => 'UTF-8', 'attributesKey' => '@attributes', 'cdataKey' => '@cdata', 'valueKey' => '@value', 'namespacesOnRoot' => true];
27+
/** @var array */
28+
private $config = ['version' => '1.0', 'encoding' => 'UTF-8', 'attributesKey' => '@attributes', 'cdataKey' => '@cdata', 'valueKey' => '@value', 'namespacesOnRoot' => true];
2829

29-
protected DOMDocument $xml;
30+
private $xml;
3031

31-
protected array $namespaces = [];
32+
/** @var array */
33+
private $namespaces = [];
3234

33-
protected array $array = [];
35+
/** @var array */
36+
private $items = [];
3437

3538
public function decode(StreamInterface $body): array
3639
{
3740
$body->rewind();
3841
$this->loadXml($body->getContents());
42+
if ($this->xml->documentElement === null) {
43+
return [];
44+
}
3945
// Convert the XML to an array, omitting the root node, as it is the name of the entity
40-
$child = $this->xml->documentElement->firstChild;
46+
$child = $this->xml->documentElement->firstChild;
47+
if ($child === null) {
48+
return [];
49+
}
4150
$childValue = $this->parseNode($child);
4251
$childNodeName = $child->nodeName;
43-
$this->array[$childNodeName] = $childValue;
52+
$this->items[$childNodeName] = $childValue;
4453
// Add namespacing information to the root node
4554
if (! empty($this->namespaces) && $this->config['namespacesOnRoot']) {
46-
if (! isset($this->array[$childNodeName][$this->config['attributesKey']])) {
47-
$this->array[$childNodeName][$this->config['attributesKey']] = [];
55+
if (! isset($this->items[$childNodeName][$this->config['attributesKey']])) {
56+
$this->items[$childNodeName][$this->config['attributesKey']] = [];
4857
}
4958
foreach ($this->namespaces as $uri => $prefix) {
50-
if ($prefix) {
51-
$prefix = self::ATTRIBUTE_NAMESPACE_SEPARATOR . $prefix;
59+
if (! \is_string($prefix)) {
60+
continue;
5261
}
53-
$this->array[$childNodeName][$this->config['attributesKey']][self::ATTRIBUTE_NAMESPACE . $prefix] = $uri;
62+
$prefix = \sprintf('%s%s%s', self::ATTRIBUTE_NAMESPACE, self::ATTRIBUTE_NAMESPACE_SEPARATOR, $prefix);
63+
$this->items[$childNodeName][$this->config['attributesKey']][$prefix] = $uri;
5464
}
5565
}
5666

57-
return $this->array;
67+
return $this->items;
5868
}
5969

6070
public function encode(SerializableInterface $body): string
@@ -66,16 +76,25 @@ public function encode(SerializableInterface $body): string
6676
$rootKey = \substr(\get_class($body), \strrpos(\get_class($body), '\\') + 1);
6777
}
6878
$this->xml->appendChild($this->buildNode($rootKey, $body->toArray()));
79+
$result = $this->xml->saveXML();
80+
if ($result === false) {
81+
throw new SerializeException('Failed to save xml during serialization');
82+
}
6983

70-
return $this->xml->saveXML();
84+
return $result;
7185
}
7286

7387
public function getMimeType(): string
7488
{
7589
return self::MIME_TYPE;
7690
}
7791

78-
protected function loadXml(string $inputXml)
92+
/**
93+
* @throws SerializeException
94+
*
95+
* @return void
96+
*/
97+
private function loadXml(string $inputXml)
7998
{
8099
$this->xml = new DOMDocument($this->config['version'], $this->config['encoding']);
81100
$parse = @$this->xml->loadXML($inputXml);
@@ -84,10 +103,12 @@ protected function loadXml(string $inputXml)
84103
}
85104
}
86105

87-
protected function parseNode(DOMNode $node)
106+
/**
107+
* @throws SerializeException
108+
*/
109+
private function parseNode(DOMNode $node)
88110
{
89-
$output = [];
90-
$output = $this->collectNodeNamespaces($node, $output);
111+
$output = $this->collectNodeNamespaces($node, []);
91112
switch ($node->nodeType) {
92113
case XML_CDATA_SECTION_NODE:
93114
$output[$this->config['cdataKey']] = $this->normalizeTextContent($node->textContent);
@@ -108,7 +129,10 @@ protected function parseNode(DOMNode $node)
108129
return $output;
109130
}
110131

111-
protected function parseChildNodes(DOMNode $node, $output)
132+
/**
133+
* @throws SerializeException
134+
*/
135+
private function parseChildNodes(DOMNode $node, $output)
112136
{
113137
foreach ($node->childNodes as $child) {
114138
if ($child->nodeType === XML_CDATA_SECTION_NODE) {
@@ -122,8 +146,8 @@ protected function parseChildNodes(DOMNode $node, $output)
122146
$output[$this->config['cdataKey']] = $this->normalizeTextContent($child->textContent);
123147
} else {
124148
$value = $this->parseNode($child);
125-
if ($child->nodeType == XML_TEXT_NODE) {
126-
if ($value != '') {
149+
if ($child->nodeType === XML_TEXT_NODE) {
150+
if ($value !== '') {
127151
if (! empty($output)) {
128152
$output[$this->config['valueKey']] = $value;
129153
} else {
@@ -143,34 +167,45 @@ protected function parseChildNodes(DOMNode $node, $output)
143167
return $output;
144168
}
145169

146-
protected function normalizeTextContent($textContent): string
170+
/**
171+
* @param string|string[] $textContent
172+
*
173+
* @throws SerializeException
174+
*/
175+
private function normalizeTextContent($textContent): string
147176
{
148-
return \trim(\preg_replace(['/\\n+\\s+/', '/\\r+\\s+/', '/\\n+\\t+/', '/\\r+\\t+/'], ' ', $textContent));
177+
$normalized = \preg_replace(['/\\n+\\s+/', '/\\r+\\s+/', '/\\n+\\t+/', '/\\r+\\t+/'], ' ', $textContent);
178+
if (! \is_string($normalized)) {
179+
throw new SerializeException(\sprintf('Normalization of %s failed', \json_encode($textContent)));
180+
}
181+
182+
return \trim($normalized);
149183
}
150184

151-
protected function normalizeNodeValues($values)
185+
private function normalizeNodeValues($values)
152186
{
153-
if (\is_array($values)) {
154-
// if only one node of its kind, assign it directly instead if array($value);
155-
foreach ($values as $key => $value) {
156-
if (\is_array($value) && \count($value) === 1) {
157-
$keyName = \array_keys($value)[0];
158-
if (\is_numeric($keyName)) {
159-
$values[$key] = $value[$keyName];
160-
}
187+
if (! \is_array($values)) {
188+
return $values;
189+
}
190+
if (empty($values)) {
191+
return '';
192+
}
193+
// if there is only one node of its kind, assign it directly instead of array($value);
194+
foreach ($values as $key => $value) {
195+
if (\is_array($value) && \count($value) === 1) {
196+
$keyName = \array_keys($value)[0];
197+
if (\is_numeric($keyName)) {
198+
$values[$key] = $value[$keyName];
161199
}
162200
}
163-
if (empty($values)) {
164-
$values = '';
165-
}
166201
}
167202

168203
return $values;
169204
}
170205

171-
protected function collectAttributes(DOMNode $node, $output)
206+
private function collectAttributes(DOMNode $node, $output)
172207
{
173-
if (! $node->attributes->length) {
208+
if ($node->attributes === null || ! $node->attributes->length) {
174209
return $output;
175210
}
176211
$attributes = [];
@@ -179,12 +214,10 @@ protected function collectAttributes(DOMNode $node, $output)
179214
$attributeName = $attributeNode->nodeName;
180215
$attributes[$attributeName] = (string) $attributeNode->value;
181216
if ($attributeNode->namespaceURI) {
182-
$nsUri = $attributeNode->namespaceURI;
183-
$nsPrefix = $attributeNode->lookupPrefix($nsUri);
184217
$namespaces = $this->collectNamespaces($attributeNode);
185218
}
186219
}
187-
// if its a leaf node, store the value in @value instead of directly it.
220+
// if it is a leaf node, store the value in @value
188221
if (! \is_array($output)) {
189222
if (! empty($output)) {
190223
$output = [$this->config['valueKey'] => $output];
@@ -199,7 +232,7 @@ protected function collectAttributes(DOMNode $node, $output)
199232
return $output;
200233
}
201234

202-
protected function collectNodeNamespaces(DOMNode $node, array $output): array
235+
private function collectNodeNamespaces(DOMNode $node, array $output): array
203236
{
204237
$namespaces = $this->collectNamespaces($node);
205238
if (! empty($namespaces)) {
@@ -209,7 +242,7 @@ protected function collectNodeNamespaces(DOMNode $node, array $output): array
209242
return $output;
210243
}
211244

212-
protected function collectNamespaces(DOMNode $node): array
245+
private function collectNamespaces(DOMNode $node): array
213246
{
214247
$namespaces = [];
215248
if ($node->namespaceURI) {
@@ -229,25 +262,36 @@ protected function collectNamespaces(DOMNode $node): array
229262
return $namespaces;
230263
}
231264

232-
protected function buildNode($nodeName, $data)
265+
/**
266+
* @throws SerializeException
267+
*/
268+
private function buildNode(string $nodeName, $data): DOMElement
233269
{
234270
if (! $this->isValidTagName($nodeName)) {
235271
throw new SerializeException('Invalid character in the tag name being generated: ' . $nodeName);
236272
}
237273
$node = $this->xml->createElement($nodeName);
274+
if ($data === false) {
275+
throw new SerializeException('Failed to create a node for: ' . $nodeName);
276+
}
238277
if (\is_array($data)) {
239-
$this->parseArray($node, $nodeName, $data);
278+
$this->parseArray($node, $data);
240279
} else {
241280
$node->appendChild($this->xml->createTextNode($this->normalizeValues($data)));
242281
}
243282

244283
return $node;
245284
}
246285

247-
protected function parseArray(DOMElement $node, $nodeName, array $array)
286+
/**
287+
* @throws SerializeException
288+
*
289+
* @return void
290+
*/
291+
private function parseArray(DOMElement $node, array $array)
248292
{
249-
// get the attributes first.;
250-
$array = $this->parseAttributes($node, $nodeName, $array);
293+
// get the attributes first
294+
$array = $this->parseAttributes($node, $array);
251295
// get value stored in @value
252296
$array = $this->parseValue($node, $array);
253297
// get value stored in @cdata
@@ -258,7 +302,7 @@ protected function parseArray(DOMElement $node, $nodeName, array $array)
258302
throw new SerializeException('Invalid character in the tag name being generated: ' . $key);
259303
}
260304
if (\is_array($value) && \is_numeric(\key($value))) {
261-
// MORE THAN ONE NODE OF ITS KIND;
305+
// MORE THAN ONE NODE OF ITS KIND
262306
// if the new array is numeric index, means it is array of nodes of the same kind
263307
// it should follow the parent key name
264308
foreach ($value as $v) {
@@ -272,7 +316,10 @@ protected function parseArray(DOMElement $node, $nodeName, array $array)
272316
}
273317
}
274318

275-
protected function parseAttributes(DOMElement $node, $nodeName, array $array)
319+
/**
320+
* @throws SerializeException
321+
*/
322+
private function parseAttributes(DOMElement $node, array $array): array
276323
{
277324
$attributesKey = $this->config['attributesKey'];
278325
if (\array_key_exists($attributesKey, $array) && \is_array($array[$attributesKey])) {
@@ -288,7 +335,7 @@ protected function parseAttributes(DOMElement $node, $nodeName, array $array)
288335
return $array;
289336
}
290337

291-
protected function parseValue(DOMElement $node, array $array)
338+
private function parseValue(DOMElement $node, array $array): array
292339
{
293340
$valueKey = $this->config['valueKey'];
294341
if (\array_key_exists($valueKey, $array)) {
@@ -299,7 +346,7 @@ protected function parseValue(DOMElement $node, array $array)
299346
return $array;
300347
}
301348

302-
protected function parseCdata(DOMElement $node, array $array)
349+
private function parseCdata(DOMElement $node, array $array): array
303350
{
304351
$cdataKey = $this->config['cdataKey'];
305352
if (\array_key_exists($cdataKey, $array)) {
@@ -310,7 +357,7 @@ protected function parseCdata(DOMElement $node, array $array)
310357
return $array;
311358
}
312359

313-
protected function normalizeValues($value)
360+
private function normalizeValues($value): string
314361
{
315362
$value = $value === true ? 'true' : $value;
316363
$value = $value === false ? 'false' : $value;
@@ -319,7 +366,7 @@ protected function normalizeValues($value)
319366
return (string) $value;
320367
}
321368

322-
protected function isValidTagName(string $tag): bool
369+
private function isValidTagName(string $tag): bool
323370
{
324371
$pattern = '/^[a-zA-Z_][\\w\\:\\-\\.]*$/';
325372

example/gen/src/Serializer/QuerySerializer.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public function serializeRequest(RequestInterface $request): string
2323
foreach ($request->getRawQueryParameters() as $name => $value) {
2424
if ($value === null) {
2525
continue;
26-
} elseif ($value instanceof DateTimeInterface) {
26+
}
27+
if ($value instanceof DateTimeInterface) {
2728
$value = $value->format(DateTimeInterface::RFC3339);
2829
} elseif ($value instanceof SerializableInterface) {
2930
$value = $value->toArray();

src/Entity/Request.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,22 @@ public function __construct(
4343
RequestFieldRegistry $fieldCollection,
4444
array $bodyContentTypes
4545
) {
46-
$this->path = $this->toRelativePath($path);
4746
if (!in_array($method, self::ALLOWED_METHODS, true)) {
4847
throw new InvalidSpecificationException(
4948
sprintf('Unsupported request method `%s` in `%s`.', $method, $path)
5049
);
5150
}
52-
$this->method = $method;
53-
$this->fields = $fieldCollection;
51+
5452
$unsupportedContentTypes = array_diff($bodyContentTypes, Request::ALLOWED_CONTENT_TYPES);
5553
if (!empty($unsupportedContentTypes)) {
5654
throw new InvalidSpecificationException(
5755
sprintf('Request content-type %s is not currently supported.', json_encode($unsupportedContentTypes))
5856
);
5957
}
58+
59+
$this->path = $this->toRelativePath($path);
60+
$this->method = $method;
61+
$this->fields = $fieldCollection;
6062
$this->bodyContentTypes = $bodyContentTypes;
6163
}
6264

0 commit comments

Comments
 (0)