Skip to content

Commit 25fab41

Browse files
authored
PHP Generator: address PSR-12 formatting violations (#23250)
* Fix PSR-12 formatting in PHP template * Generate samples
1 parent ed0e031 commit 25fab41

3 files changed

Lines changed: 114 additions & 102 deletions

File tree

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

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ class ObjectSerializer
9595
}
9696
}
9797
} else {
98-
foreach($data as $property => $value) {
98+
foreach ($data as $property => $value) {
9999
$values[$property] = self::sanitizeForSerialization($value);
100100
}
101101
}
102-
return (object)$values;
102+
return (object) $values;
103103
} else {
104104
return (string)$data;
105105
}
@@ -131,7 +131,9 @@ class ObjectSerializer
131131
*/
132132
public static function sanitizeTimestamp($timestamp)
133133
{
134-
if (!is_string($timestamp)) return $timestamp;
134+
if (!is_string($timestamp)) {
135+
return $timestamp;
136+
}
135137

136138
return preg_replace('/(:\d{2}.\d{6})\d*/', '$1', $timestamp);
137139
}
@@ -159,20 +161,20 @@ class ObjectSerializer
159161
*/
160162
private static function isEmptyValue($value, string $openApiType): bool
161163
{
162-
# If empty() returns false, it is not empty regardless of its type.
164+
// If empty() returns false, it is not empty regardless of its type.
163165
if (!empty($value)) {
164166
return false;
165167
}
166168

167-
# Null is always empty, as we cannot send a real "null" value in a query parameter.
169+
// Null is always empty, as we cannot send a real "null" value in a query parameter.
168170
if ($value === null) {
169171
return true;
170172
}
171173

172174
switch ($openApiType) {
173-
# For numeric values, false and '' are considered empty.
174-
# This comparison is safe for floating point values, since the previous call to empty() will
175-
# filter out values that don't match 0.
175+
// For numeric values, false and '' are considered empty.
176+
// This comparison is safe for floating point values, since the previous call to empty() will
177+
// filter out values that don't match 0.
176178
case 'int':
177179
case 'integer':
178180
return $value !== 0;
@@ -181,16 +183,16 @@ class ObjectSerializer
181183
case 'float':
182184
return $value !== 0 && $value !== 0.0;
183185
184-
# For boolean values, '' is considered empty
186+
// For boolean values, '' is considered empty
185187
case 'bool':
186188
case 'boolean':
187189
return !in_array($value, [false, 0], true);
188190
189-
# For string values, '' is considered empty.
191+
// For string values, '' is considered empty.
190192
case 'string':
191193
return $value === '';
192194
193-
# For all the other types, any value at this point can be considered empty.
195+
// For all the other types, any value at this point can be considered empty.
194196
default:
195197
return true;
196198
}
@@ -218,10 +220,10 @@ class ObjectSerializer
218220
bool $required = true
219221
): array {
220222
221-
# Check if we should omit this parameter from the query. This should only happen when:
222-
# - Parameter is NOT required; AND
223-
# - its value is set to a value that is equivalent to "empty", depending on its OpenAPI type. For
224-
# example, 0 as "int" or "boolean" is NOT an empty value.
223+
// Check if we should omit this parameter from the query. This should only happen when:
224+
// - Parameter is NOT required; AND
225+
// - its value is set to a value that is equivalent to "empty", depending on its OpenAPI type. For
226+
// example, 0 as "int" or "boolean" is NOT an empty value.
225227
if (self::isEmptyValue($value, $openApiType)) {
226228
if ($required) {
227229
return ["{$paramName}" => ''];
@@ -230,8 +232,8 @@ class ObjectSerializer
230232
}
231233
}
232234
233-
# Handle DateTime objects in query
234-
if($openApiType === "\\DateTime" && $value instanceof \DateTime) {
235+
// Handle DateTime objects in query
236+
if ($openApiType === "\\DateTime" && $value instanceof \DateTime) {
235237
return ["{$paramName}" => $value->format(self::$dateTimeFormat)];
236238
}
237239
@@ -241,10 +243,12 @@ class ObjectSerializer
241243
// since \GuzzleHttp\Psr7\Query::build fails with nested arrays
242244
// need to flatten array first
243245
$flattenArray = function ($arr, $name, &$result = []) use (&$flattenArray, $style, $explode) {
244-
if (!is_array($arr)) return $arr;
246+
if (!is_array($arr)) {
247+
return $arr;
248+
}
245249
246250
foreach ($arr as $k => $v) {
247-
$prop = ($style === 'deepObject') ? $prop = "{$name}[{$k}]" : $k;
251+
$prop = ($style === 'deepObject') ? "{$name}[{$k}]" : $k;
248252
249253
if (is_array($v)) {
250254
$flattenArray($v, $prop, $result);
@@ -489,7 +493,7 @@ class ObjectSerializer
489493
$data = is_string($data) ? json_decode($data) : $data;
490494
491495
if (is_array($data)) {
492-
$data = (object)$data;
496+
$data = (object) $data;
493497
}
494498

495499
// If a discriminator is defined and points to a valid subclass, use it.
@@ -528,20 +532,20 @@ class ObjectSerializer
528532
}
529533

530534
/**
531-
* Build a query string from an array of key value pairs.
532-
*
533-
* This function can use the return value of `parse()` to build a query
534-
* string. This function does not modify the provided keys when an array is
535-
* encountered (like `http_build_query()` would).
536-
*
537-
* The function is copied from https://github.com/guzzle/psr7/blob/a243f80a1ca7fe8ceed4deee17f12c1930efe662/src/Query.php#L59-L112
538-
* with a modification which is described in https://github.com/guzzle/psr7/pull/603
539-
*
540-
* @param array $params Query string parameters.
541-
* @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
542-
* to encode using RFC3986, or PHP_QUERY_RFC1738
543-
* to encode using RFC1738.
544-
*/
535+
* Build a query string from an array of key value pairs.
536+
*
537+
* This function can use the return value of `parse()` to build a query
538+
* string. This function does not modify the provided keys when an array is
539+
* encountered (like `http_build_query()` would).
540+
*
541+
* The function is copied from https://github.com/guzzle/psr7/blob/a243f80a1ca7fe8ceed4deee17f12c1930efe662/src/Query.php#L59-L112
542+
* with a modification which is described in https://github.com/guzzle/psr7/pull/603
543+
*
544+
* @param array $params Query string parameters.
545+
* @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
546+
* to encode using RFC3986, or PHP_QUERY_RFC1738
547+
* to encode using RFC1738.
548+
*/
545549
public static function buildQuery(array $params, $encoding = PHP_QUERY_RFC3986): string
546550
{
547551
if (!$params) {

samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ public static function sanitizeForSerialization($data, $type = null, $format = n
103103
}
104104
}
105105
} else {
106-
foreach($data as $property => $value) {
106+
foreach ($data as $property => $value) {
107107
$values[$property] = self::sanitizeForSerialization($value);
108108
}
109109
}
110-
return (object)$values;
110+
return (object) $values;
111111
} else {
112112
return (string)$data;
113113
}
@@ -139,7 +139,9 @@ public static function sanitizeFilename($filename)
139139
*/
140140
public static function sanitizeTimestamp($timestamp)
141141
{
142-
if (!is_string($timestamp)) return $timestamp;
142+
if (!is_string($timestamp)) {
143+
return $timestamp;
144+
}
143145

144146
return preg_replace('/(:\d{2}.\d{6})\d*/', '$1', $timestamp);
145147
}
@@ -167,20 +169,20 @@ public static function toPathValue($value)
167169
*/
168170
private static function isEmptyValue($value, string $openApiType): bool
169171
{
170-
# If empty() returns false, it is not empty regardless of its type.
172+
// If empty() returns false, it is not empty regardless of its type.
171173
if (!empty($value)) {
172174
return false;
173175
}
174176

175-
# Null is always empty, as we cannot send a real "null" value in a query parameter.
177+
// Null is always empty, as we cannot send a real "null" value in a query parameter.
176178
if ($value === null) {
177179
return true;
178180
}
179181

180182
switch ($openApiType) {
181-
# For numeric values, false and '' are considered empty.
182-
# This comparison is safe for floating point values, since the previous call to empty() will
183-
# filter out values that don't match 0.
183+
// For numeric values, false and '' are considered empty.
184+
// This comparison is safe for floating point values, since the previous call to empty() will
185+
// filter out values that don't match 0.
184186
case 'int':
185187
case 'integer':
186188
return $value !== 0;
@@ -189,16 +191,16 @@ private static function isEmptyValue($value, string $openApiType): bool
189191
case 'float':
190192
return $value !== 0 && $value !== 0.0;
191193

192-
# For boolean values, '' is considered empty
194+
// For boolean values, '' is considered empty
193195
case 'bool':
194196
case 'boolean':
195197
return !in_array($value, [false, 0], true);
196198

197-
# For string values, '' is considered empty.
199+
// For string values, '' is considered empty.
198200
case 'string':
199201
return $value === '';
200202

201-
# For all the other types, any value at this point can be considered empty.
203+
// For all the other types, any value at this point can be considered empty.
202204
default:
203205
return true;
204206
}
@@ -226,10 +228,10 @@ public static function toQueryValue(
226228
bool $required = true
227229
): array {
228230

229-
# Check if we should omit this parameter from the query. This should only happen when:
230-
# - Parameter is NOT required; AND
231-
# - its value is set to a value that is equivalent to "empty", depending on its OpenAPI type. For
232-
# example, 0 as "int" or "boolean" is NOT an empty value.
231+
// Check if we should omit this parameter from the query. This should only happen when:
232+
// - Parameter is NOT required; AND
233+
// - its value is set to a value that is equivalent to "empty", depending on its OpenAPI type. For
234+
// example, 0 as "int" or "boolean" is NOT an empty value.
233235
if (self::isEmptyValue($value, $openApiType)) {
234236
if ($required) {
235237
return ["{$paramName}" => ''];
@@ -238,8 +240,8 @@ public static function toQueryValue(
238240
}
239241
}
240242

241-
# Handle DateTime objects in query
242-
if($openApiType === "\\DateTime" && $value instanceof \DateTime) {
243+
// Handle DateTime objects in query
244+
if ($openApiType === "\\DateTime" && $value instanceof \DateTime) {
243245
return ["{$paramName}" => $value->format(self::$dateTimeFormat)];
244246
}
245247

@@ -249,10 +251,12 @@ public static function toQueryValue(
249251
// since \GuzzleHttp\Psr7\Query::build fails with nested arrays
250252
// need to flatten array first
251253
$flattenArray = function ($arr, $name, &$result = []) use (&$flattenArray, $style, $explode) {
252-
if (!is_array($arr)) return $arr;
254+
if (!is_array($arr)) {
255+
return $arr;
256+
}
253257

254258
foreach ($arr as $k => $v) {
255-
$prop = ($style === 'deepObject') ? $prop = "{$name}[{$k}]" : $k;
259+
$prop = ($style === 'deepObject') ? "{$name}[{$k}]" : $k;
256260

257261
if (is_array($v)) {
258262
$flattenArray($v, $prop, $result);
@@ -497,7 +501,7 @@ public static function deserialize($data, $class, $httpHeaders = null)
497501
$data = is_string($data) ? json_decode($data) : $data;
498502

499503
if (is_array($data)) {
500-
$data = (object)$data;
504+
$data = (object) $data;
501505
}
502506

503507
// If a discriminator is defined and points to a valid subclass, use it.
@@ -536,20 +540,20 @@ public static function deserialize($data, $class, $httpHeaders = null)
536540
}
537541

538542
/**
539-
* Build a query string from an array of key value pairs.
540-
*
541-
* This function can use the return value of `parse()` to build a query
542-
* string. This function does not modify the provided keys when an array is
543-
* encountered (like `http_build_query()` would).
544-
*
545-
* The function is copied from https://github.com/guzzle/psr7/blob/a243f80a1ca7fe8ceed4deee17f12c1930efe662/src/Query.php#L59-L112
546-
* with a modification which is described in https://github.com/guzzle/psr7/pull/603
547-
*
548-
* @param array $params Query string parameters.
549-
* @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
550-
* to encode using RFC3986, or PHP_QUERY_RFC1738
551-
* to encode using RFC1738.
552-
*/
543+
* Build a query string from an array of key value pairs.
544+
*
545+
* This function can use the return value of `parse()` to build a query
546+
* string. This function does not modify the provided keys when an array is
547+
* encountered (like `http_build_query()` would).
548+
*
549+
* The function is copied from https://github.com/guzzle/psr7/blob/a243f80a1ca7fe8ceed4deee17f12c1930efe662/src/Query.php#L59-L112
550+
* with a modification which is described in https://github.com/guzzle/psr7/pull/603
551+
*
552+
* @param array $params Query string parameters.
553+
* @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
554+
* to encode using RFC3986, or PHP_QUERY_RFC1738
555+
* to encode using RFC1738.
556+
*/
553557
public static function buildQuery(array $params, $encoding = PHP_QUERY_RFC3986): string
554558
{
555559
if (!$params) {

0 commit comments

Comments
 (0)