Skip to content

Commit a4e80bf

Browse files
authored
Add signature for locators in the code generator (#1498)
This also provides better failure mode in case of invalid metadata instead of failing with a TypeError.
1 parent d59dc0f commit a4e80bf

File tree

7 files changed

+56
-20
lines changed

7 files changed

+56
-20
lines changed

src/Definition/ErrorWaiterAcceptor.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ class ErrorWaiterAcceptor extends WaiterAcceptor
1111
{
1212
public function getError(): ExceptionShape
1313
{
14-
return ($this->shapeLocator)($this->data['expected']);
14+
$shape = ($this->shapeLocator)($this->data['expected']);
15+
16+
if (!$shape instanceof ExceptionShape) {
17+
throw new \InvalidArgumentException(sprintf('The error "%s" of the waiter acceptor should have an Exception shape.', $this->data['expected']));
18+
}
19+
20+
return $shape;
1521
}
1622
}

src/Definition/Member.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ class Member
1515
protected $data;
1616

1717
/**
18-
* @var \Closure
18+
* @var \Closure(string, Member|null=, array<string, mixed>=): Shape
1919
*/
2020
protected $shapeLocator;
2121

22+
/**
23+
* @param \Closure(string, Member|null=, array<string, mixed>=): Shape $shapeLocator
24+
*/
2225
public function __construct(array $data, \Closure $shapeLocator)
2326
{
2427
if (isset($data['endpointdiscoveryid'])) {

src/Definition/Operation.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,17 @@ class Operation
3535
private $example;
3636

3737
/**
38-
* @var \Closure
38+
* @var \Closure(string, Member|null=, array<string, mixed>=): Shape
3939
*/
4040
private $shapeLocator;
4141

4242
private function __construct()
4343
{
4444
}
4545

46+
/**
47+
* @param \Closure(string, Member|null=, array<string, mixed>=): Shape $shapeLocator
48+
*/
4649
public static function create(string $name, array $data, ServiceDefinition $service, ?Pagination $pagination, Example $example, \Closure $shapeLocator): self
4750
{
4851
$operation = new self();
@@ -97,7 +100,13 @@ public function getExample(): Example
97100
public function getOutput(): ?StructureShape
98101
{
99102
if (isset($this->data['output']['shape'])) {
100-
return ($this->shapeLocator)($this->data['output']['shape'], null, ['resultWrapper' => $this->data['output']['resultWrapper'] ?? null]);
103+
$shape = ($this->shapeLocator)($this->data['output']['shape'], null, ['resultWrapper' => $this->data['output']['resultWrapper'] ?? null]);
104+
105+
if (!$shape instanceof StructureShape) {
106+
throw new \InvalidArgumentException(sprintf('The operation "%s" should have an Structure output.', $this->getName()));
107+
}
108+
109+
return $shape;
101110
}
102111

103112
return null;
@@ -113,8 +122,11 @@ public function getErrors(): array
113122
if (isset($errors[$error['shape']])) {
114123
continue;
115124
}
116-
/** @var ExceptionShape $shape */
117125
$shape = ($this->shapeLocator)($error['shape']);
126+
if (!$shape instanceof ExceptionShape) {
127+
throw new \InvalidArgumentException(sprintf('The error "%s" of the operation "%s" should have an Exception shape.', $error['shape'], $this->getName()));
128+
}
129+
118130
$errors[$error['shape']] = $shape;
119131
}
120132

@@ -200,11 +212,9 @@ private function getInputShape(): Shape
200212
return Shape::create(
201213
sprintf('%sRequest', $this->getName()),
202214
['type' => 'structure', 'required' => [], 'members' => []],
215+
$this->shapeLocator,
203216
function () {
204-
return null;
205-
},
206-
function () {
207-
return null;
217+
return $this->service;
208218
}
209219
);
210220
}

src/Definition/ServiceDefinition.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ private function getExample(string $name): Example
169169
/**
170170
* @param array<string, mixed> $extra
171171
*/
172-
private function getShape(string $name, ?Member $member, array $extra): ?Shape
172+
private function getShape(string $name, ?Member $member, array $extra): Shape
173173
{
174174
if (isset($this->definition['shapes'][$name])) {
175175
$documentationMember = null;
@@ -181,11 +181,11 @@ private function getShape(string $name, ?Member $member, array $extra): ?Shape
181181
return Shape::create($name, $this->definition['shapes'][$name] + ['_documentation_main' => $documentationMain, '_documentation_member' => $documentationMember] + $extra, $this->createClosureToFindShape(), $this->createClosureToService());
182182
}
183183

184-
return null;
184+
throw new \InvalidArgumentException(sprintf('The shape "%s" does not exist.', $name));
185185
}
186186

187187
/**
188-
* @return \Closure(string, ?Member=, array<string, mixed>=): ?Shape
188+
* @return \Closure(string, ?Member=, array<string, mixed>=): Shape
189189
*/
190190
private function createClosureToFindShape(): \Closure
191191
{
@@ -209,14 +209,20 @@ private function createClosureToService(): \Closure
209209
}
210210

211211
/**
212-
* @return \Closure(string): ?Operation
212+
* @return \Closure(string): Operation
213213
*/
214214
private function createClosureToFindOperation(): \Closure
215215
{
216216
$definition = $this;
217217

218-
return \Closure::fromCallable(function (string $name) use ($definition): ?Operation {
219-
return $definition->getOperation($name);
218+
return \Closure::fromCallable(function (string $name) use ($definition): Operation {
219+
$operation = $definition->getOperation($name);
220+
221+
if (null === $operation) {
222+
throw new \InvalidArgumentException(sprintf('The operation "%s" is not defined.', $name));
223+
}
224+
225+
return $operation;
220226
});
221227
}
222228
}

src/Definition/Shape.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ class Shape
1515
protected $data;
1616

1717
/**
18-
* @var \Closure
18+
* @var \Closure(string, Member|null=, array<string, mixed>=): Shape
1919
*/
2020
protected $shapeLocator;
2121

2222
/**
23-
* @var \Closure
23+
* @var \Closure(): ServiceDefinition
2424
*/
2525
protected $serviceLocator;
2626

@@ -33,6 +33,10 @@ private function __construct()
3333
{
3434
}
3535

36+
/**
37+
* @param \Closure(string, Member|null=, array<string, mixed>=): Shape $shapeLocator
38+
* @param \Closure(): ServiceDefinition $serviceLocator
39+
*/
3640
public static function create(string $name, array $data, \Closure $shapeLocator, \Closure $serviceLocator): Shape
3741
{
3842
switch ($data['type']) {

src/Definition/Waiter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@ class Waiter
1515
private $data;
1616

1717
/**
18-
* @var \Closure
18+
* @var \Closure(string): Operation
1919
*/
2020
private $operationLocator;
2121

2222
/**
23-
* @var \Closure
23+
* @var \Closure(string, Member|null=, array<string, mixed>=): Shape
2424
*/
2525
private $shapeLocator;
2626

27+
/**
28+
* @param \Closure(string): Operation $operationLocator
29+
* @param \Closure(string, Member|null=, array<string, mixed>=): Shape $shapeLocator
30+
*/
2731
public function __construct(array $data, \Closure $operationLocator, \Closure $shapeLocator)
2832
{
2933
$this->data = $data;

src/Definition/WaiterAcceptor.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ class WaiterAcceptor
2323
protected $data;
2424

2525
/**
26-
* @var \Closure
26+
* @var \Closure(string, Member|null=, array<string, mixed>=): Shape
2727
*/
2828
protected $shapeLocator;
2929

3030
private function __construct()
3131
{
3232
}
3333

34+
/**
35+
* @param \Closure(string, Member|null=, array<string, mixed>=): Shape $shapeLocator
36+
*/
3437
public static function create(array $data, \Closure $shapeLocator): self
3538
{
3639
switch ($data['matcher']) {

0 commit comments

Comments
 (0)