Skip to content

Commit fb31437

Browse files
fix: phpstan error
1 parent 734f4d8 commit fb31437

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

src/Hydra/Serializer/DocumentationNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ private function getHydraOperation(HttpOperation $operation, string $prefixedSho
313313

314314
if (null !== $inputClass) {
315315
$possibleValue = [];
316-
foreach ($operation->getInputFormats() as $mimeTypes) {
316+
foreach ($operation->getInputFormats() ?? [] as $mimeTypes) {
317317
foreach ($mimeTypes as $mimeType) {
318318
$possibleValue[] = $mimeType;
319319
}

src/Laravel/Exception/ErrorHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function render($request, \Throwable $exception)
7777
$operation = null;
7878
foreach ($resourceCollection as $resource) {
7979
foreach ($resource->getOperations() as $op) {
80-
foreach ($op->getOutputFormats() as $key => $value) {
80+
foreach ($op->getOutputFormats() ?? [] as $key => $value) {
8181
if ($key === $format) {
8282
$operation = $op;
8383
break 3;

src/Metadata/HttpOperation.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ class HttpOperation extends Operation
2929
public const METHOD_HEAD = 'HEAD';
3030
public const METHOD_OPTIONS = 'OPTIONS';
3131

32+
/** @var array<int|string, string|string[]>|null */
33+
protected ?array $formats;
34+
/** @var array<int|string, string|string[]>|null */
35+
protected ?array $inputFormats;
36+
/** @var array<int|string, string|string[]>|null */
37+
protected ?array $outputFormats;
38+
3239
/**
3340
* @param string[]|null $types the RDF types of this property
3441
* @param array<int|string, string|string[]>|string|null $formats {@see https://api-platform.com/docs/core/content-negotiation/#configuring-formats-for-a-specific-resource-or-operation}
@@ -90,9 +97,9 @@ public function __construct(
9097
protected string $method = 'GET',
9198
protected ?string $uriTemplate = null,
9299
protected ?array $types = null,
93-
protected $formats = null,
94-
protected $inputFormats = null,
95-
protected $outputFormats = null,
100+
$formats = null,
101+
$inputFormats = null,
102+
$outputFormats = null,
96103
protected $uriVariables = null,
97104
protected ?string $routePrefix = null,
98105
protected ?string $routeName = null,
@@ -214,6 +221,10 @@ public function __construct(
214221
?bool $queryParameterValidationEnabled = null,
215222
array $extraProperties = [],
216223
) {
224+
$this->formats = (null === $formats || \is_array($formats)) ? $formats : [$formats];
225+
$this->inputFormats = (null === $inputFormats || \is_array($inputFormats)) ? $inputFormats : [$inputFormats];
226+
$this->outputFormats = (null === $outputFormats || \is_array($outputFormats)) ? $outputFormats : [$outputFormats];
227+
217228
parent::__construct(
218229
shortName: $shortName,
219230
class: $class,
@@ -312,7 +323,7 @@ public function withTypes($types): static
312323
}
313324

314325
/**
315-
* @return array<int|string, string|string[]>|string|null
326+
* @return array<int|string, string|string[]>|null
316327
*/
317328
public function getFormats()
318329
{
@@ -325,13 +336,13 @@ public function getFormats()
325336
public function withFormats($formats = null): static
326337
{
327338
$self = clone $this;
328-
$self->formats = $formats;
339+
$self->formats = (null === $formats || \is_array($formats)) ? $formats : [$formats];
329340

330341
return $self;
331342
}
332343

333344
/**
334-
* @return array<int|string, string|string[]>|string|null
345+
* @return array<int|string, string|string[]>|null
335346
*/
336347
public function getInputFormats()
337348
{
@@ -344,13 +355,13 @@ public function getInputFormats()
344355
public function withInputFormats($inputFormats = null): static
345356
{
346357
$self = clone $this;
347-
$self->inputFormats = $inputFormats;
358+
$self->inputFormats = (null === $inputFormats || \is_array($inputFormats)) ? $inputFormats : [$inputFormats];
348359

349360
return $self;
350361
}
351362

352363
/**
353-
* @return array<int|string, string|string[]>|string|null
364+
* @return array<int|string, string|string[]>|null
354365
*/
355366
public function getOutputFormats()
356367
{
@@ -363,7 +374,7 @@ public function getOutputFormats()
363374
public function withOutputFormats($outputFormats = null): static
364375
{
365376
$self = clone $this;
366-
$self->outputFormats = $outputFormats;
377+
$self->outputFormats = (null === $outputFormats || \is_array($outputFormats)) ? $outputFormats : [$outputFormats];
367378

368379
return $self;
369380
}

src/Symfony/EventListener/ErrorListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ class: Error::class,
227227
// TODO: move this to ResourceMetadataCollection?
228228
foreach ($resourceCollection as $resource) {
229229
foreach ($resource->getOperations() as $op) {
230-
foreach ($op->getOutputFormats() as $key => $value) {
230+
foreach ($op->getOutputFormats() ?? [] as $key => $value) {
231231
if ($key === $format) {
232232
$operation = $op;
233233
break 3;

0 commit comments

Comments
 (0)