Skip to content

Commit 0cd182f

Browse files
committed
Trying to get to a point where psalm passes
1 parent fee6ef4 commit 0cd182f

File tree

3 files changed

+22
-24
lines changed

3 files changed

+22
-24
lines changed

src/GenerateRequests.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,24 @@
2020
class GenerateRequests extends ClassGenerator {
2121

2222
/**
23-
* @param string $file_path
24-
* @param bool $more_specificity
2523
* @throws InvalidArgumentException
2624
*/
2725
public function generate(string $file_path, bool $more_specificity = false): void{
2826
$api = Yaml::parseFile($file_path);
2927
$base_uri = self::stringNotEndWith($api['basePath'], '/');
3028
foreach ($api['paths'] as $path => $path_details){
29+
$path_no_params = $adapted_path = $this->pathNoParams($path);
3130
if ($more_specificity){
3231
$adapted_path = $this->pathWithParamsNoPlaceholders($path);
3332
}
34-
else {
35-
$adapted_path = $this->pathNoParams($path);
36-
}
3733
$path_camel = $this->pathToCamelCase($adapted_path);
3834
foreach ($path_details as $method => $method_details){
3935
$class_name = ucfirst($method).$path_camel;
4036
$class = new ClassType($class_name);
4137
$class->setExtends(self::REQUEST_CLASS_NAME);
4238
$class->addComment($method_details['summary']);
4339
$class->addConstant('METHOD', strtoupper($method));
44-
45-
if ($base_uri){
46-
$path = self::stringNotBeginWith($path, '/');
47-
$uri = "{$base_uri}/{$path}";
48-
}
49-
else {
50-
$uri = $path;
51-
}
52-
$class->addConstant('URI', $uri);
40+
$class->addConstant('URI', $base_uri ? "$base_uri/$path_no_params" : $path_no_params);
5341

5442
$this->handleParams($method_details, $class);
5543

@@ -134,6 +122,7 @@ private function handlePathParams(array $path_params, ClassType $class): void{
134122
$class->addProperty('path_params')
135123
->setStatic()
136124
->setValue($param_names)
125+
->setType('array')
137126
->setVisibility('protected');
138127
}
139128
}
@@ -177,12 +166,14 @@ private function handleQueryParams(array $query_params, ClassType $class): void{
177166
catch (InvalidArgumentException $e) {
178167
$query_params_property = $class->addProperty('query_params')
179168
->setStatic()
169+
->setType('array')
180170
->setVisibility('protected');
181171
$query_params_array = [];
182172
}
183173
$value = array_merge($query_params_array, $param_names);
184174
$query_params_property->setStatic()
185175
->setValue($value)
176+
->setType('array')
186177
->setVisibility('protected');
187178
}
188179
}
@@ -253,6 +244,7 @@ protected function handleResponse(array $method_details, ClassType $class): void
253244

254245
$model_ns = $this->namespaceModel();
255246
$has_2xx = false;
247+
$list_types = [];
256248
foreach ($method_details['responses'] as $code_string => $method){
257249
$get_type_from = isset($method['$ref']) ? $method : ($method['schema'] ?? null);
258250
if (!is_null($get_type_from)){
@@ -263,7 +255,9 @@ protected function handleResponse(array $method_details, ClassType $class): void
263255
$type = $this->typeFromRef($get_type_from);
264256
}
265257
if ($this->notScalarType($type)){
266-
$type = "\\$model_ns\\$type::class";
258+
$class_string = "\\$model_ns\\$type";
259+
$type = "$class_string::class";
260+
$list_types[] = $class_string;
267261
}
268262
else {
269263
$type = "''";
@@ -284,13 +278,20 @@ protected function handleResponse(array $method_details, ClassType $class): void
284278
throw new InvalidArgumentException('Response blocks must contain at least one positive response type');
285279
}
286280

281+
$list_types = array_unique($list_types);
282+
if (!$list_types){
283+
$list_types = ['SwaggerModel'];
284+
}
285+
foreach ($list_types as $list_type){
286+
$list_types[] = "list<$list_type>";
287+
}
288+
287289
$response_models = implode(",\n\t", $response_models);
288290
$response_models = "[\n\t$response_models\n]";
289291
$class->addMethod('sendWith')
290292
->addBody("return \$client->make(\$this, $response_models);")
291-
->addComment('@param SwaggerClient $client')
292-
->addComment('@return SwaggerModel')
293+
->addComment('@return '.implode('|', $list_types))
293294
->addParameter('client')
294-
->setTypeHint('SwaggerClient');
295+
->setType('SwaggerClient');
295296
}
296297
}

src/SwaggerClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ interface SwaggerClient {
99
/**
1010
* @template T of SwaggerModel
1111
*
12-
* @param array<array-key, class-string<T>> $response_models
12+
* @param array<array-key, class-string<T>|''> $response_models
1313
* @return T|list<T>
1414
*/
15-
public function make(SwaggerRequest $request, array $response_models);
15+
public function make(SwaggerRequest $swagger, array $response_models);
1616

1717
public function messageFromRequest(SwaggerRequest $swagger):RequestInterface;
1818
}

src/SwaggerRequest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ public function getQuery(): array{
5151
public function getPath(): string{
5252
$query = [];
5353
foreach (static::$path_params as $param_name){
54-
if (!isset($this->{$param_name})){
55-
continue;
56-
}
57-
$query[] = $this->{$param_name};
54+
$query[] = $this->{$param_name} ?? '';
5855
}
5956
if (count($query)>0){
6057
return '/'.implode('/', $query);

0 commit comments

Comments
 (0)