Skip to content

Commit b1da096

Browse files
dkarloviybelenko
andauthored
[PHP] Small tweaks for php generator, PHPStan level 1 (#7528)
* Small tweaks for php generator, PHPStan level 1 Co-authored-by: Yuriy Belenko <[email protected]>
1 parent 08a1bdc commit b1da096

File tree

10 files changed

+111
-411
lines changed

10 files changed

+111
-411
lines changed

modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,8 @@ class ObjectSerializer
354354
continue;
355355
}
356356

357-
$propertyValue = $data->{$instance::attributeMap()[$property]};
358-
if (isset($propertyValue)) {
357+
if (isset($data->{$instance::attributeMap()[$property]})) {
358+
$propertyValue = $data->{$instance::attributeMap()[$property]};
359359
$instance->$propertySetter(self::deserialize($propertyValue, $type, null));
360360
}
361361
}

modules/openapi-generator/src/main/resources/php/api.mustache

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,18 @@ use {{invokerPackage}}\ObjectSerializer;
6363
* @param ClientInterface $client
6464
* @param Configuration $config
6565
* @param HeaderSelector $selector
66-
* @param int $host_index (Optional) host index to select the list of hosts if defined in the OpenAPI spec
66+
* @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec
6767
*/
6868
public function __construct(
6969
ClientInterface $client = null,
7070
Configuration $config = null,
7171
HeaderSelector $selector = null,
72-
$host_index = 0
72+
$hostIndex = 0
7373
) {
7474
$this->client = $client ?: new Client();
7575
$this->config = $config ?: new Configuration();
7676
$this->headerSelector = $selector ?: new HeaderSelector();
77-
$this->hostIndex = $host_index;
77+
$this->hostIndex = $hostIndex;
7878
}
7979

8080
/**
@@ -547,13 +547,6 @@ use {{invokerPackage}}\ObjectSerializer;
547547
{{/isFile}}
548548
}
549549
{{/formParams}}
550-
// body params
551-
$_tempBody = null;
552-
{{#bodyParams}}
553-
if (isset(${{paramName}})) {
554-
$_tempBody = ${{paramName}};
555-
}
556-
{{/bodyParams}}
557550

558551
if ($multipart) {
559552
$headers = $this->headerSelector->selectHeadersForMultipart(
@@ -567,14 +560,18 @@ use {{invokerPackage}}\ObjectSerializer;
567560
}
568561

569562
// for model (json/xml)
570-
if (isset($_tempBody)) {
571-
// $_tempBody is the method argument, if present
563+
{{#bodyParams}}
564+
if (isset(${{paramName}})) {
572565
if ($headers['Content-Type'] === 'application/json') {
573-
$httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($_tempBody));
566+
$httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization(${{paramName}}));
574567
} else {
575-
$httpBody = $_tempBody;
568+
$httpBody = ${{paramName}};
576569
}
577570
} elseif (count($formParams) > 0) {
571+
{{/bodyParams}}
572+
{{^bodyParams}}
573+
if (count($formParams) > 0) {
574+
{{/bodyParams}}
578575
if ($multipart) {
579576
$multipartContents = [];
580577
foreach ($formParams as $formParamName => $formParamValue) {

samples/client/petstore/php/OpenAPIClient-php/lib/Api/AnotherFakeApi.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,18 @@ class AnotherFakeApi
7373
* @param ClientInterface $client
7474
* @param Configuration $config
7575
* @param HeaderSelector $selector
76-
* @param int $host_index (Optional) host index to select the list of hosts if defined in the OpenAPI spec
76+
* @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec
7777
*/
7878
public function __construct(
7979
ClientInterface $client = null,
8080
Configuration $config = null,
8181
HeaderSelector $selector = null,
82-
$host_index = 0
82+
$hostIndex = 0
8383
) {
8484
$this->client = $client ?: new Client();
8585
$this->config = $config ?: new Configuration();
8686
$this->headerSelector = $selector ?: new HeaderSelector();
87-
$this->hostIndex = $host_index;
87+
$this->hostIndex = $hostIndex;
8888
}
8989

9090
/**
@@ -316,11 +316,6 @@ protected function call123TestSpecialTagsRequest($client)
316316

317317

318318

319-
// body params
320-
$_tempBody = null;
321-
if (isset($client)) {
322-
$_tempBody = $client;
323-
}
324319

325320
if ($multipart) {
326321
$headers = $this->headerSelector->selectHeadersForMultipart(
@@ -334,12 +329,11 @@ protected function call123TestSpecialTagsRequest($client)
334329
}
335330

336331
// for model (json/xml)
337-
if (isset($_tempBody)) {
338-
// $_tempBody is the method argument, if present
332+
if (isset($client)) {
339333
if ($headers['Content-Type'] === 'application/json') {
340-
$httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($_tempBody));
334+
$httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($client));
341335
} else {
342-
$httpBody = $_tempBody;
336+
$httpBody = $client;
343337
}
344338
} elseif (count($formParams) > 0) {
345339
if ($multipart) {

samples/client/petstore/php/OpenAPIClient-php/lib/Api/DefaultApi.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,18 @@ class DefaultApi
7373
* @param ClientInterface $client
7474
* @param Configuration $config
7575
* @param HeaderSelector $selector
76-
* @param int $host_index (Optional) host index to select the list of hosts if defined in the OpenAPI spec
76+
* @param int $hostIndex (Optional) host index to select the list of hosts if defined in the OpenAPI spec
7777
*/
7878
public function __construct(
7979
ClientInterface $client = null,
8080
Configuration $config = null,
8181
HeaderSelector $selector = null,
82-
$host_index = 0
82+
$hostIndex = 0
8383
) {
8484
$this->client = $client ?: new Client();
8585
$this->config = $config ?: new Configuration();
8686
$this->headerSelector = $selector ?: new HeaderSelector();
87-
$this->hostIndex = $host_index;
87+
$this->hostIndex = $hostIndex;
8888
}
8989

9090
/**
@@ -301,8 +301,6 @@ protected function fooGetRequest()
301301

302302

303303

304-
// body params
305-
$_tempBody = null;
306304

307305
if ($multipart) {
308306
$headers = $this->headerSelector->selectHeadersForMultipart(
@@ -316,14 +314,7 @@ protected function fooGetRequest()
316314
}
317315

318316
// for model (json/xml)
319-
if (isset($_tempBody)) {
320-
// $_tempBody is the method argument, if present
321-
if ($headers['Content-Type'] === 'application/json') {
322-
$httpBody = \GuzzleHttp\json_encode(ObjectSerializer::sanitizeForSerialization($_tempBody));
323-
} else {
324-
$httpBody = $_tempBody;
325-
}
326-
} elseif (count($formParams) > 0) {
317+
if (count($formParams) > 0) {
327318
if ($multipart) {
328319
$multipartContents = [];
329320
foreach ($formParams as $formParamName => $formParamValue) {

0 commit comments

Comments
 (0)