Skip to content

Commit 5814f6d

Browse files
authored
Merge pull request #5 from openapi-php/remove-nullable-pathitem
Remove nullable PathItem
2 parents fa0ceaf + 77cc224 commit 5814f6d

File tree

2 files changed

+14
-26
lines changed

2 files changed

+14
-26
lines changed

src/spec/OpenApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* @property string $openapi
1717
* @property Info $info
1818
* @property array<Server> $servers
19-
* @property Paths|array<PathItem> $paths
19+
* @property Paths $paths
2020
* @property Components|null $components
2121
* @property array<PathItem>|null $webhooks
2222
* @property array<SecurityRequirement> $security

src/spec/Paths.php

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@
3434
*
3535
* @link https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#pathsObject
3636
*
37-
* @implements ArrayAccess<string, PathItem|null>
38-
* @implements IteratorAggregate<string, PathItem|null>
37+
* @implements ArrayAccess<string, PathItem>
38+
* @implements IteratorAggregate<string, PathItem>
3939
*/
4040
class Paths implements SpecObjectInterface, DocumentContextInterface, ArrayAccess, Countable, IteratorAggregate
4141
{
42-
/** @var array<string, PathItem|null> */
42+
/** @var array<string, PathItem> */
4343
private array $_paths = [];
4444
/** @var list<string> */
4545
private array $_errors = [];
@@ -57,11 +57,11 @@ public function __construct(array $data)
5757
{
5858
foreach ($data as $path => $object) {
5959
if ($object === null) {
60-
$this->_paths[$path] = null;
60+
$this->removePath($path);
6161
} elseif (is_array($object)) {
62-
$this->_paths[$path] = new PathItem($object);
62+
$this->addPath($path, new PathItem($object));
6363
} elseif ($object instanceof PathItem) {
64-
$this->_paths[$path] = $object;
64+
$this->addPath($path, $object);
6565
} else {
6666
$givenType = gettype($object);
6767
if (is_object($object)) {
@@ -81,7 +81,7 @@ public function getSerializableData(): object
8181
{
8282
$data = [];
8383
foreach ($this->_paths as $path => $pathItem) {
84-
$data[$path] = $pathItem?->getSerializableData();
84+
$data[$path] = $pathItem->getSerializableData();
8585
}
8686

8787
return (object) $data;
@@ -115,10 +115,14 @@ public function addPath(string $name, PathItem $pathItem): void
115115
/** @param string $name path name */
116116
public function removePath(string $name): void
117117
{
118+
if (! $this->hasPath($name)) {
119+
return;
120+
}
121+
118122
unset($this->_paths[$name]);
119123
}
120124

121-
/** @return array<string, PathItem|null> */
125+
/** @return array<string, PathItem> */
122126
public function getPaths(): array
123127
{
124128
return $this->_paths;
@@ -136,10 +140,6 @@ public function validate(): bool
136140
$valid = true;
137141
$this->_errors = [];
138142
foreach ($this->_paths as $key => $path) {
139-
if ($path === null) {
140-
continue;
141-
}
142-
143143
if (! $path->validate()) {
144144
$valid = false;
145145
}
@@ -169,10 +169,6 @@ public function getErrors(): array
169169
}
170170

171171
foreach ($this->_paths as $path) {
172-
if ($path === null) {
173-
continue;
174-
}
175-
176172
$errors[] = $path->getErrors();
177173
}
178174

@@ -267,7 +263,7 @@ public function count(): int
267263
*
268264
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php
269265
*
270-
* @return Traversable<PathItem|null> An instance of an object implementing <b>Iterator</b> or <b>Traversable</b>
266+
* @return Traversable<PathItem> An instance of an object implementing <b>Iterator</b> or <b>Traversable</b>
271267
*/
272268
public function getIterator(): Traversable
273269
{
@@ -282,10 +278,6 @@ public function getIterator(): Traversable
282278
public function resolveReferences(ReferenceContext|null $context = null): void
283279
{
284280
foreach ($this->_paths as $path) {
285-
if ($path === null) {
286-
continue;
287-
}
288-
289281
$path->resolveReferences($context);
290282
}
291283
}
@@ -296,10 +288,6 @@ public function resolveReferences(ReferenceContext|null $context = null): void
296288
public function setReferenceContext(ReferenceContext $context): void
297289
{
298290
foreach ($this->_paths as $path) {
299-
if ($path === null) {
300-
continue;
301-
}
302-
303291
$path->setReferenceContext($context);
304292
}
305293
}

0 commit comments

Comments
 (0)