1 | <?php |
||
2 | |||
3 | |||
4 | namespace Apie\OpenapiSchema\Spec; |
||
5 | |||
6 | use Apie\OpenapiSchema\ValueObjects\SpecificationExtension; |
||
7 | use Apie\TypeJuggling\PrimitiveArray; |
||
8 | use Apie\ValueObjects\ValueObjectInterface; |
||
9 | use ArrayIterator; |
||
10 | use Exception; |
||
11 | use IteratorAggregate; |
||
12 | use Traversable; |
||
13 | |||
14 | class Responses implements ValueObjectInterface, IteratorAggregate |
||
15 | { |
||
16 | /** |
||
17 | * @var Response|null |
||
18 | */ |
||
19 | private $default; |
||
20 | |||
21 | /** |
||
22 | * @var Response[] |
||
23 | */ |
||
24 | private $statusCodeResponses = []; |
||
25 | |||
26 | /** |
||
27 | * @var SpecificationExtension |
||
28 | */ |
||
29 | private $specificationExtension; |
||
30 | |||
31 | public function __construct() |
||
32 | { |
||
33 | $this->specificationExtension = new SpecificationExtension([]); |
||
34 | } |
||
35 | |||
36 | public static function fromNative($value) |
||
37 | { |
||
38 | $result = new Responses(); |
||
39 | $extension = []; |
||
40 | foreach ($value as $key => $itemValue) { |
||
41 | if ($key === 'default') { |
||
42 | $result->default = self::responseFromNative($itemValue); |
||
0 ignored issues
–
show
|
|||
43 | } elseif (preg_match('/^\d+$/', $key)) { |
||
44 | $result->statusCodeResponses[$key] = self::responseFromNative($itemValue, $key); |
||
45 | } else { |
||
46 | $extension[$key] = $itemValue; |
||
47 | } |
||
48 | } |
||
49 | $result->specificationExtension = new SpecificationExtension($extension); |
||
50 | return $result; |
||
51 | } |
||
52 | |||
53 | private static function responseFromNative($itemValue, string $key = 'default') |
||
54 | { |
||
55 | $itemValue = (new PrimitiveArray($key))->toNative($itemValue); |
||
56 | if (isset($itemValue['$ref'])) { |
||
57 | return Reference::fromNative($itemValue); |
||
58 | } |
||
59 | return Response::fromNative($itemValue); |
||
60 | } |
||
61 | |||
62 | public function toNative() |
||
63 | { |
||
64 | $result = $this->specificationExtension->toNative(); |
||
65 | if ($this->default) { |
||
66 | $result['default'] = $this->default->toNative(); |
||
67 | } |
||
68 | foreach ($this->statusCodeResponses as $statusCode => $response) { |
||
69 | $result[$statusCode] = $response->toNative(); |
||
70 | } |
||
71 | return $result; |
||
72 | } |
||
73 | |||
74 | public function getIterator() |
||
75 | { |
||
76 | $list = $this->default ? ['default' => $this->default] : []; |
||
77 | if ($this->statusCodeResponses) { |
||
0 ignored issues
–
show
The expression
$this->statusCodeResponses of type Apie\OpenapiSchema\Spec\Response[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using ![]() |
|||
78 | $list = array_merge($list, $this->statusCodeResponses); |
||
79 | } |
||
80 | return new ArrayIterator($list); |
||
81 | } |
||
82 | } |
||
83 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.