From 659f086fa54b6abb1a7a43811fb8480b15b801a2 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 26 Jul 2024 21:25:46 +1200 Subject: [PATCH 01/10] Use header param --- templates/php/base/params.twig | 9 ++++++++- templates/php/base/requests/api.twig | 9 +-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/templates/php/base/params.twig b/templates/php/base/params.twig index 67a6fee93..f0e1e7321 100644 --- a/templates/php/base/params.twig +++ b/templates/php/base/params.twig @@ -22,4 +22,11 @@ $apiParams['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }}; } {% endfor %} -{% endif %} \ No newline at end of file +{% endif %} + $apiHeaders = []; + {%~ for parameter in method.parameters.header %} + $apiHeaders['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }}; + {%~ endfor %} + {%~ for key, header in method.headers %} + $apiHeaders['{{ key }}'] = '{{ header }}'; + {%~ endfor %} diff --git a/templates/php/base/requests/api.twig b/templates/php/base/requests/api.twig index acb6aadd5..473b79211 100644 --- a/templates/php/base/requests/api.twig +++ b/templates/php/base/requests/api.twig @@ -1,14 +1,7 @@ return $this->client->call( Client::METHOD_{{ method.method | caseUpper }}, $apiPath, - [ - {%~ for parameter in method.parameters.header %} - '{{ parameter.name }}' => ${{ parameter.name | caseCamel | escapeKeyword }}, - {%~ endfor %} - {%~ for key, header in method.headers %} - '{{ key }}' => '{{ header }}', - {%~ endfor %} - ], + $apiHeaders, $apiParams{% if method.type == 'webAuth' -%}, 'location'{% endif %} ); \ No newline at end of file From 4ce7c8f0dce1aaf79d31f7e53c7d0d7bd661b8e3 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 26 Jul 2024 21:27:06 +1200 Subject: [PATCH 02/10] Don't isset/null check unnecessarily --- templates/php/base/params.twig | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/templates/php/base/params.twig b/templates/php/base/params.twig index f0e1e7321..60aaabdb6 100644 --- a/templates/php/base/params.twig +++ b/templates/php/base/params.twig @@ -1,28 +1,17 @@ $apiParams = []; {% if method.parameters.all | length %} {% for parameter in method.parameters.all %} -{% if parameter.required and not parameter.nullable %} - if (!isset(${{ parameter.name | caseCamel | escapeKeyword }})) { - throw new {{spec.title | caseUcfirst}}Exception('Missing required parameter: "{{ parameter.name | caseCamel | escapeKeyword }}"'); - } -{% endif %} -{% endfor %} -{% for parameter in method.parameters.query %} - if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) { - $apiParams['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }}; - } -{% endfor %} -{% for parameter in method.parameters.body %} - if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) { - $apiParams['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }}; - } -{% endfor %} -{% for parameter in method.parameters.formData %} +{% if not parameter.required and not parameter.nullable %} + if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) { $apiParams['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }}; } +{% else %} + $apiParams['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }}; +{% endif %} {% endfor %} {% endif %} + $apiHeaders = []; {%~ for parameter in method.parameters.header %} $apiHeaders['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }}; From 350375c3bc5742309b26cc11d27e18bca86a4c7c Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 26 Jul 2024 21:27:21 +1200 Subject: [PATCH 03/10] Fix constructor call --- templates/php/src/Services/Service.php.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/php/src/Services/Service.php.twig b/templates/php/src/Services/Service.php.twig index 8d0b54201..52f2067c7 100644 --- a/templates/php/src/Services/Service.php.twig +++ b/templates/php/src/Services/Service.php.twig @@ -27,7 +27,7 @@ class {{ service.name | caseUcfirst }} extends Service { public function __construct(Client $client) { - $this->client = $client; + parent::__construct($client); } {% for method in service.methods %} From de6a88a920277d84c9043de32132a21b6866b2db Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 26 Jul 2024 21:27:58 +1200 Subject: [PATCH 04/10] Improve method param typing --- templates/php/src/Services/Service.php.twig | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/templates/php/src/Services/Service.php.twig b/templates/php/src/Services/Service.php.twig index 52f2067c7..b524cb6a4 100644 --- a/templates/php/src/Services/Service.php.twig +++ b/templates/php/src/Services/Service.php.twig @@ -39,20 +39,24 @@ class {{ service.name | caseUcfirst }} extends Service * {% endif %} {% for parameter in method.parameters.all %} - * @param {{ parameter | typeName }} ${{ parameter.name | caseCamel | escapeKeyword }} + * @param {% if not parameter.required or parameter.nullable %}?{% endif %}{{ parameter | typeName }} ${{ parameter.name | caseCamel | escapeKeyword }} {% endfor %} * @throws {{spec.title | caseUcfirst}}Exception * @return {{ method | getReturn }} - */ - public function {{ method.name | caseCamel }}({% for parameter in method.parameters.all %}{{ parameter | typeName }} ${{ parameter.name | caseCamel | escapeKeyword }}{% if not parameter.required %} = null{% endif %}{% if not loop.last %}, {% endif %}{% endfor %}{% if 'multipart/form-data' in method.consumes %}, callable $onProgress = null{% endif %}): {{ method | getReturn }} + public function {{ method.name | caseCamel }}({% for parameter in method.parameters.all %}{% if not parameter.required or parameter.nullable %}?{% endif %}{{ parameter | typeName }} ${{ parameter.name | caseCamel | escapeKeyword }}{% if not parameter.required %} = null{% endif %}{% if not loop.last %}, {% endif %}{% endfor %}{% if 'multipart/form-data' in method.consumes %}, callable $onProgress = null{% endif %}): {{ method | getReturn }} { - $apiPath = str_replace([{% for parameter in method.parameters.path %}'{{ '{' }}{{ parameter.name | caseCamel }}{{ '}' }}'{% if not loop.last %}, {% endif %}{% endfor %}], [{% for parameter in method.parameters.path %}${{ parameter.name | caseCamel | escapeKeyword }}{% if not loop.last %}, {% endif %}{% endfor %}], '{{ method.path }}'); + $apiPath = str_replace( + [{% for parameter in method.parameters.path %}'{{ '{' }}{{ parameter.name | caseCamel }}{{ '}' }}'{% if not loop.last %}, {% endif %}{% endfor %}], + [{% for parameter in method.parameters.path %}${{ parameter.name | caseCamel | escapeKeyword }}{% if not loop.last %}, {% endif %}{% endfor %}], + '{{ method.path }}' + ); {{~ include('php/base/params.twig') -}} {%~ if 'multipart/form-data' in method.consumes %} {{~ include('php/base/requests/file.twig') }} {%~ else %} + {{~ include('php/base/requests/api.twig') }} {%~ endif %} } @@ -60,4 +64,4 @@ class {{ service.name | caseUcfirst }} extends Service {%~ endif %} {%~ endfor %} -} +} \ No newline at end of file From 8cb0b980c337f3fe26bf690185fcfcbeeb80135c Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 26 Jul 2024 21:28:33 +1200 Subject: [PATCH 05/10] Fix exceptions --- templates/php/src/Client.php.twig | 12 +++++++++--- templates/php/src/Exception.php.twig | 23 ++++++++++++++--------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/templates/php/src/Client.php.twig b/templates/php/src/Client.php.twig index 540835c5e..d4109585f 100644 --- a/templates/php/src/Client.php.twig +++ b/templates/php/src/Client.php.twig @@ -120,7 +120,13 @@ class Client * @return array|string * @throws {{spec.title | caseUcfirst}}Exception */ - public function call($method, $path = '', $headers = array(), array $params = array(), ?string $responseType = null) + public function call( + string $method, + string $path = '', + array $headers = [], + array $params = [], + ?string $responseType = null + ) { $headers = array_merge($this->headers, $headers); $ch = curl_init($this->endpoint . $path . (($method == self::METHOD_GET && !empty($params)) ? '?' . http_build_query($params) : '')); @@ -191,14 +197,14 @@ class Client } if (curl_errno($ch)) { - throw new {{spec.title | caseUcfirst}}Exception(curl_error($ch), $responseStatus, $responseBody); + throw new {{spec.title | caseUcfirst}}Exception(curl_error($ch), $responseStatus, $responseBody['type'] ?? '', $responseBody); } curl_close($ch); if($responseStatus >= 400) { if(is_array($responseBody)) { - throw new {{spec.title | caseUcfirst}}Exception($responseBody['message'], $responseStatus, $responseBody['type'] ?? '', $responseBody); + throw new {{spec.title | caseUcfirst}}Exception($responseBody['message'], $responseStatus, $responseBody['type'] ?? '', json_encode($responseBody)); } else { throw new {{spec.title | caseUcfirst}}Exception($responseBody, $responseStatus); } diff --git a/templates/php/src/Exception.php.twig b/templates/php/src/Exception.php.twig index 0507a70a6..6339be6d8 100644 --- a/templates/php/src/Exception.php.twig +++ b/templates/php/src/Exception.php.twig @@ -9,20 +9,25 @@ class {{spec.title | caseUcfirst}}Exception extends Exception { /** * @var mixed */ - private $response; + private ?string $response; /** * @var string */ - private $type; + private string $type; /** - * @param String $message + * @param ?string $message * @param int $code - * @param mixed $response + * @param string $type + * @param ?string $response */ - public function __construct($message = null, $code = 0, $type = null, $response = null) - { + public function __construct( + ?string $message = null, + int $code = 0, + string $type = '', + ?string $response = null + ) { parent::__construct($message, $code); $this->response = $response; $this->type = $type; @@ -31,15 +36,15 @@ class {{spec.title | caseUcfirst}}Exception extends Exception { /** * @return string */ - public function getType() + public function getType(): string { return $this->type; } /** - * @return mixed + * @return ?string */ - final public function getResponse() + final public function getResponse(): ?string { return $this->response; } From e651a85dc350ec2a20ba628fc790b7ab32d339ed Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 26 Jul 2024 21:28:58 +1200 Subject: [PATCH 06/10] Fix deprecation warning --- templates/php/src/Query.php.twig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/php/src/Query.php.twig b/templates/php/src/Query.php.twig index f2fa1d7f0..98abc538e 100644 --- a/templates/php/src/Query.php.twig +++ b/templates/php/src/Query.php.twig @@ -23,10 +23,10 @@ class Query implements \JsonSerializable public function __toString(): string { - return json_encode($this->jsonSerialize()); + return json_encode($this); } - public function jsonSerialize(): array + public function jsonSerialize(): mixed { return array_filter([ 'method' => $this->method, From f88a344fb49eb3b4dfb8ba144f5a3f5e151901c4 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 26 Jul 2024 21:29:20 +1200 Subject: [PATCH 07/10] Improve typing --- templates/php/src/Client.php.twig | 20 ++++++++++---------- templates/php/src/InputFile.php.twig | 4 ++-- templates/php/src/Permission.php.twig | 4 ++++ templates/php/src/Query.php.twig | 14 +++++++------- templates/php/src/Service.php.twig | 8 +------- 5 files changed, 24 insertions(+), 26 deletions(-) diff --git a/templates/php/src/Client.php.twig b/templates/php/src/Client.php.twig index d4109585f..894866571 100644 --- a/templates/php/src/Client.php.twig +++ b/templates/php/src/Client.php.twig @@ -14,28 +14,28 @@ class Client const METHOD_CONNECT = 'CONNECT'; const METHOD_TRACE = 'TRACE'; - const CHUNK_SIZE = 5*1024*1024; + const CHUNK_SIZE = 5 * 1024 * 1024; /** * Is Self Signed Certificates Allowed? * * @var bool */ - protected $selfSigned = false; + protected bool $selfSigned = false; /** * Service host name * * @var string */ - protected $endpoint = '{{spec.endpoint}}'; + protected string $endpoint = '{{spec.endpoint}}'; /** * Global Headers * * @var array */ - protected $headers = [ + protected array $headers = [ 'content-type' => '', 'user-agent' => '{{spec.title | caseUcfirst}}{{ language.name | caseUcfirst }}SDK/{{ sdk.version }} ({{deviceInfo}})', 'x-sdk-name'=> '{{ sdk.name }}', @@ -45,7 +45,7 @@ class Client ]; /** - * SDK constructor. + * Client constructor. */ public function __construct() { @@ -66,7 +66,7 @@ class Client * * @return Client */ - public function set{{header.key | caseUcfirst}}($value) + public function set{{header.key | caseUcfirst}}(string $value): Client { $this->addHeader('{{header.name}}', $value); @@ -79,7 +79,7 @@ class Client * @param bool $status * @return $this */ - public function setSelfSigned($status = true) + public function setSelfSigned(bool $status = true): Client { $this->selfSigned = $status; @@ -90,7 +90,7 @@ class Client * @param $endpoint * @return $this */ - public function setEndpoint($endpoint) + public function setEndpoint(string $endpoint): Client { $this->endpoint = $endpoint; @@ -101,7 +101,7 @@ class Client * @param $key * @param $value */ - public function addHeader($key, $value) + public function addHeader(string $key, string $value): Client { $this->headers[strtolower($key)] = $value; @@ -224,7 +224,7 @@ class Client * @param string $prefix * @return array */ - protected function flatten(array $data, $prefix = '') { + protected function flatten(array $data, string $prefix = ''): array { $output = []; foreach($data as $key => $value) { diff --git a/templates/php/src/InputFile.php.twig b/templates/php/src/InputFile.php.twig index a7822196b..50844f27d 100644 --- a/templates/php/src/InputFile.php.twig +++ b/templates/php/src/InputFile.php.twig @@ -29,7 +29,7 @@ class InputFile { return $this->filename; } - public static function withPath(string $path, ?string $mimeType = null, ?string $filename = null) + public static function withPath(string $path, ?string $mimeType = null, ?string $filename = null): InputFile { $instance = new InputFile(); $instance->path = $path; @@ -39,7 +39,7 @@ class InputFile { return $instance; } - public static function withData(string $data, ?string $mimeType = null, ?string $filename = null) + public static function withData(string $data, ?string $mimeType = null, ?string $filename = null): InputFile { $instance = new InputFile(); $instance->path = null; diff --git a/templates/php/src/Permission.php.twig b/templates/php/src/Permission.php.twig index 941565e7c..d0fc94ce6 100644 --- a/templates/php/src/Permission.php.twig +++ b/templates/php/src/Permission.php.twig @@ -8,18 +8,22 @@ class Permission { return "read(\"$role\")"; } + public static function write(string $role): string { return "write(\"$role\")"; } + public static function create(string $role): string { return "create(\"$role\")"; } + public static function update(string $role): string { return "update(\"$role\")"; } + public static function delete(string $role): string { return "delete(\"$role\")"; diff --git a/templates/php/src/Query.php.twig b/templates/php/src/Query.php.twig index 98abc538e..3d2bd0e0f 100644 --- a/templates/php/src/Query.php.twig +++ b/templates/php/src/Query.php.twig @@ -42,7 +42,7 @@ class Query implements \JsonSerializable * @param mixed $value * @return string */ - public static function equal(string $attribute, $value): string + public static function equal(string $attribute, mixed $value): string { return (new Query('equal', $attribute, $value))->__toString(); } @@ -54,7 +54,7 @@ class Query implements \JsonSerializable * @param mixed $value * @return string */ - public static function notEqual(string $attribute, $value): string + public static function notEqual(string $attribute, mixed $value): string { return (new Query('notEqual', $attribute, $value))->__toString(); } @@ -66,7 +66,7 @@ class Query implements \JsonSerializable * @param mixed $value * @return string */ - public static function lessThan(string $attribute, $value): string + public static function lessThan(string $attribute, mixed $value): string { return (new Query('lessThan', $attribute, $value))->__toString(); } @@ -78,7 +78,7 @@ class Query implements \JsonSerializable * @param mixed $value * @return string */ - public static function lessThanEqual(string $attribute, $value): string + public static function lessThanEqual(string $attribute, mixed $value): string { return (new Query('lessThanEqual', $attribute, $value))->__toString(); } @@ -90,7 +90,7 @@ class Query implements \JsonSerializable * @param mixed $value * @return string */ - public static function greaterThan(string $attribute, $value): string + public static function greaterThan(string $attribute, mixed $value): string { return (new Query('greaterThan', $attribute, $value))->__toString(); } @@ -102,7 +102,7 @@ class Query implements \JsonSerializable * @param mixed $value * @return string */ - public static function greaterThanEqual(string $attribute, $value): string + public static function greaterThanEqual(string $attribute, mixed $value): string { return (new Query('greaterThanEqual', $attribute, $value))->__toString(); } @@ -149,7 +149,7 @@ class Query implements \JsonSerializable * @param string|int|float $end * @return string */ - public static function between(string $attribute, $start, $end): string + public static function between(string $attribute, mixed $start, mixed $end): string { return (new Query('between', $attribute, [$start, $end]))->__toString(); } diff --git a/templates/php/src/Service.php.twig b/templates/php/src/Service.php.twig index 7fed1dac5..8e5bcb0a2 100644 --- a/templates/php/src/Service.php.twig +++ b/templates/php/src/Service.php.twig @@ -4,14 +4,8 @@ namespace {{ spec.title | caseUcfirst }}; abstract class Service { - /** - * @var Client - */ - protected $client; + protected Client $client; - /** - * @param Client $client - */ public function __construct(Client $client) { $this->client = $client; From 87770315175ebd5c9fa729b8989ff9e49d4d3e20 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 26 Jul 2024 21:44:52 +1200 Subject: [PATCH 08/10] Update tests --- .github/workflows/tests.yml | 2 +- tests/{PHP74Test.php => PHP83Test.php} | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) rename tests/{PHP74Test.php => PHP83Test.php} (88%) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 31171c845..37d870a9b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -34,8 +34,8 @@ jobs: Node16, Node18, Node20, - PHP74, PHP80, + PHP83, Python38, Python39, Python310, diff --git a/tests/PHP74Test.php b/tests/PHP83Test.php similarity index 88% rename from tests/PHP74Test.php rename to tests/PHP83Test.php index ac53020b6..d04fb7c68 100644 --- a/tests/PHP74Test.php +++ b/tests/PHP83Test.php @@ -1,8 +1,9 @@ Date: Fri, 26 Jul 2024 21:47:56 +1200 Subject: [PATCH 09/10] Lint --- tests/PHP83Test.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/PHP83Test.php b/tests/PHP83Test.php index d04fb7c68..798e1db67 100644 --- a/tests/PHP83Test.php +++ b/tests/PHP83Test.php @@ -1,6 +1,5 @@ Date: Fri, 26 Jul 2024 23:35:08 +1200 Subject: [PATCH 10/10] Fix namespace --- tests/PHP83Test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PHP83Test.php b/tests/PHP83Test.php index 798e1db67..d453463d1 100644 --- a/tests/PHP83Test.php +++ b/tests/PHP83Test.php @@ -1,6 +1,6 @@