Skip to content

Commit

Permalink
Trying to get to a point where psalm passes
Browse files Browse the repository at this point in the history
  • Loading branch information
M1ke committed Aug 28, 2024
1 parent fee6ef4 commit 0cd182f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
37 changes: 19 additions & 18 deletions src/GenerateRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,24 @@
class GenerateRequests extends ClassGenerator {

/**
* @param string $file_path
* @param bool $more_specificity
* @throws InvalidArgumentException
*/
public function generate(string $file_path, bool $more_specificity = false): void{
$api = Yaml::parseFile($file_path);
$base_uri = self::stringNotEndWith($api['basePath'], '/');
foreach ($api['paths'] as $path => $path_details){
$path_no_params = $adapted_path = $this->pathNoParams($path);
if ($more_specificity){
$adapted_path = $this->pathWithParamsNoPlaceholders($path);
}
else {
$adapted_path = $this->pathNoParams($path);
}
$path_camel = $this->pathToCamelCase($adapted_path);
foreach ($path_details as $method => $method_details){
$class_name = ucfirst($method).$path_camel;
$class = new ClassType($class_name);
$class->setExtends(self::REQUEST_CLASS_NAME);
$class->addComment($method_details['summary']);
$class->addConstant('METHOD', strtoupper($method));

if ($base_uri){
$path = self::stringNotBeginWith($path, '/');
$uri = "{$base_uri}/{$path}";
}
else {
$uri = $path;
}
$class->addConstant('URI', $uri);
$class->addConstant('URI', $base_uri ? "$base_uri/$path_no_params" : $path_no_params);

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

Expand Down Expand Up @@ -134,6 +122,7 @@ private function handlePathParams(array $path_params, ClassType $class): void{
$class->addProperty('path_params')
->setStatic()
->setValue($param_names)
->setType('array')
->setVisibility('protected');
}
}
Expand Down Expand Up @@ -177,12 +166,14 @@ private function handleQueryParams(array $query_params, ClassType $class): void{
catch (InvalidArgumentException $e) {
$query_params_property = $class->addProperty('query_params')
->setStatic()
->setType('array')
->setVisibility('protected');
$query_params_array = [];
}
$value = array_merge($query_params_array, $param_names);
$query_params_property->setStatic()
->setValue($value)
->setType('array')
->setVisibility('protected');
}
}
Expand Down Expand Up @@ -253,6 +244,7 @@ protected function handleResponse(array $method_details, ClassType $class): void

$model_ns = $this->namespaceModel();
$has_2xx = false;
$list_types = [];
foreach ($method_details['responses'] as $code_string => $method){
$get_type_from = isset($method['$ref']) ? $method : ($method['schema'] ?? null);
if (!is_null($get_type_from)){
Expand All @@ -263,7 +255,9 @@ protected function handleResponse(array $method_details, ClassType $class): void
$type = $this->typeFromRef($get_type_from);
}
if ($this->notScalarType($type)){
$type = "\\$model_ns\\$type::class";
$class_string = "\\$model_ns\\$type";
$type = "$class_string::class";
$list_types[] = $class_string;
}
else {
$type = "''";
Expand All @@ -284,13 +278,20 @@ protected function handleResponse(array $method_details, ClassType $class): void
throw new InvalidArgumentException('Response blocks must contain at least one positive response type');
}

$list_types = array_unique($list_types);
if (!$list_types){
$list_types = ['SwaggerModel'];
}
foreach ($list_types as $list_type){
$list_types[] = "list<$list_type>";
}

$response_models = implode(",\n\t", $response_models);
$response_models = "[\n\t$response_models\n]";
$class->addMethod('sendWith')
->addBody("return \$client->make(\$this, $response_models);")
->addComment('@param SwaggerClient $client')
->addComment('@return SwaggerModel')
->addComment('@return '.implode('|', $list_types))
->addParameter('client')
->setTypeHint('SwaggerClient');
->setType('SwaggerClient');
}
}
4 changes: 2 additions & 2 deletions src/SwaggerClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ interface SwaggerClient {
/**
* @template T of SwaggerModel
*
* @param array<array-key, class-string<T>> $response_models
* @param array<array-key, class-string<T>|''> $response_models
* @return T|list<T>
*/
public function make(SwaggerRequest $request, array $response_models);
public function make(SwaggerRequest $swagger, array $response_models);

public function messageFromRequest(SwaggerRequest $swagger):RequestInterface;
}
5 changes: 1 addition & 4 deletions src/SwaggerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ public function getQuery(): array{
public function getPath(): string{
$query = [];
foreach (static::$path_params as $param_name){
if (!isset($this->{$param_name})){
continue;
}
$query[] = $this->{$param_name};
$query[] = $this->{$param_name} ?? '';
}
if (count($query)>0){
return '/'.implode('/', $query);
Expand Down

0 comments on commit 0cd182f

Please sign in to comment.