Skip to content

Commit ca02d95

Browse files
authored
PHP NextGen: address PSR-12 formatting violations (#23271)
* Fix PSR-12 formatting in PHP template * Fix PSR-12 formatting in PHP template
1 parent 1d3ef5c commit ca02d95

File tree

4 files changed

+124
-116
lines changed

4 files changed

+124
-116
lines changed

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

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ class ObjectSerializer
108108
}
109109
}
110110
} else {
111-
foreach($data as $property => $value) {
111+
foreach ($data as $property => $value) {
112112
$values[$property] = self::sanitizeForSerialization($value);
113113
}
114114
}
115-
return (object)$values;
115+
return (object) $values;
116116
} else {
117117
return (string)$data;
118118
}
@@ -170,30 +170,30 @@ class ObjectSerializer
170170
*/
171171
private static function isEmptyValue(mixed $value, string $openApiType): bool
172172
{
173-
# If empty() returns false, it is not empty regardless of its type.
173+
// If empty() returns false, it is not empty regardless of its type.
174174
if (!empty($value)) {
175175
return false;
176176
}
177177

178-
# Null is always empty, as we cannot send a real "null" value in a query parameter.
178+
// Null is always empty, as we cannot send a real "null" value in a query parameter.
179179
if ($value === null) {
180180
return true;
181181
}
182182

183183
return match ($openApiType) {
184-
# For numeric values, false and '' are considered empty.
185-
# This comparison is safe for floating point values, since the previous call to empty() will
186-
# filter out values that don't match 0.
184+
// For numeric values, false and '' are considered empty.
185+
// This comparison is safe for floating point values, since the previous call to empty() will
186+
// filter out values that don't match 0.
187187
'int','integer' => $value !== 0,
188188
'number'|'float' => $value !== 0 && $value !== 0.0,
189189
190-
# For boolean values, '' is considered empty
190+
// For boolean values, '' is considered empty
191191
'bool','boolean' => !in_array($value, [false, 0], true),
192192
193-
# For string values, '' is considered empty.
193+
// For string values, '' is considered empty.
194194
'string' => $value === '',
195195
196-
# For all the other types, any value at this point can be considered empty.
196+
// For all the other types, any value at this point can be considered empty.
197197
default => true
198198
};
199199
}
@@ -220,10 +220,10 @@ class ObjectSerializer
220220
bool $required = true
221221
): array {
222222
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.
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.
227227
if (self::isEmptyValue($value, $openApiType)) {
228228
if ($required) {
229229
return ["{$paramName}" => ''];
@@ -232,8 +232,8 @@ class ObjectSerializer
232232
}
233233
}
234234
235-
# Handle DateTime objects in query
236-
if($openApiType === "\DateTime" && $value instanceof DateTime) {
235+
// Handle DateTime objects in query
236+
if ($openApiType === "\\DateTime" && $value instanceof DateTime) {
237237
return ["{$paramName}" => $value->format(self::$dateTimeFormat)];
238238
}
239239
@@ -243,7 +243,9 @@ class ObjectSerializer
243243
// since \GuzzleHttp\Psr7\Query::build fails with nested arrays
244244
// need to flatten array first
245245
$flattenArray = function ($arr, $name, &$result = []) use (&$flattenArray, $style, $explode) {
246-
if (!is_array($arr)) return $arr;
246+
if (!is_array($arr)) {
247+
return $arr;
248+
}
247249
248250
foreach ($arr as $k => $v) {
249251
$prop = ($style === 'deepObject') ? "{$name}[{$k}]" : $k;
@@ -491,7 +493,7 @@ class ObjectSerializer
491493
$data = is_string($data) ? json_decode($data) : $data;
492494
493495
if (is_array($data)) {
494-
$data = (object)$data;
496+
$data = (object) $data;
495497
}
496498

497499
// If a discriminator is defined and points to a valid subclass, use it.
@@ -530,17 +532,17 @@ class ObjectSerializer
530532
}
531533

532534
/**
533-
* Build a query string from an array of key value pairs.
534-
*
535-
* This function can use the return value of `parse()` to build a query
536-
* string. This function does not modify the provided keys when an array is
537-
* encountered (like `http_build_query()` would).
538-
*
539-
* @param array $params Query string parameters.
540-
* @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
541-
* to encode using RFC3986, or PHP_QUERY_RFC1738
542-
* to encode using RFC1738.
543-
*/
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+
* @param array $params Query string parameters.
542+
* @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
543+
* to encode using RFC3986, or PHP_QUERY_RFC1738
544+
* to encode using RFC1738.
545+
*/
544546
public static function buildQuery(array $params, $encoding = PHP_QUERY_RFC3986): string
545547
{
546548
if (!$params) {

samples/client/echo_api/php-nextgen-streaming/src/ObjectSerializer.php

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ public static function sanitizeForSerialization(mixed $data, ?string $type = nul
117117
}
118118
}
119119
} else {
120-
foreach($data as $property => $value) {
120+
foreach ($data as $property => $value) {
121121
$values[$property] = self::sanitizeForSerialization($value);
122122
}
123123
}
124-
return (object)$values;
124+
return (object) $values;
125125
} else {
126126
return (string)$data;
127127
}
@@ -179,30 +179,30 @@ public static function toPathValue(string $value): string
179179
*/
180180
private static function isEmptyValue(mixed $value, string $openApiType): bool
181181
{
182-
# If empty() returns false, it is not empty regardless of its type.
182+
// If empty() returns false, it is not empty regardless of its type.
183183
if (!empty($value)) {
184184
return false;
185185
}
186186

187-
# Null is always empty, as we cannot send a real "null" value in a query parameter.
187+
// Null is always empty, as we cannot send a real "null" value in a query parameter.
188188
if ($value === null) {
189189
return true;
190190
}
191191

192192
return match ($openApiType) {
193-
# For numeric values, false and '' are considered empty.
194-
# This comparison is safe for floating point values, since the previous call to empty() will
195-
# filter out values that don't match 0.
193+
// For numeric values, false and '' are considered empty.
194+
// This comparison is safe for floating point values, since the previous call to empty() will
195+
// filter out values that don't match 0.
196196
'int','integer' => $value !== 0,
197197
'number'|'float' => $value !== 0 && $value !== 0.0,
198198

199-
# For boolean values, '' is considered empty
199+
// For boolean values, '' is considered empty
200200
'bool','boolean' => !in_array($value, [false, 0], true),
201201

202-
# For string values, '' is considered empty.
202+
// For string values, '' is considered empty.
203203
'string' => $value === '',
204204

205-
# For all the other types, any value at this point can be considered empty.
205+
// For all the other types, any value at this point can be considered empty.
206206
default => true
207207
};
208208
}
@@ -229,10 +229,10 @@ public static function toQueryValue(
229229
bool $required = true
230230
): array {
231231

232-
# Check if we should omit this parameter from the query. This should only happen when:
233-
# - Parameter is NOT required; AND
234-
# - its value is set to a value that is equivalent to "empty", depending on its OpenAPI type. For
235-
# example, 0 as "int" or "boolean" is NOT an empty value.
232+
// Check if we should omit this parameter from the query. This should only happen when:
233+
// - Parameter is NOT required; AND
234+
// - its value is set to a value that is equivalent to "empty", depending on its OpenAPI type. For
235+
// example, 0 as "int" or "boolean" is NOT an empty value.
236236
if (self::isEmptyValue($value, $openApiType)) {
237237
if ($required) {
238238
return ["{$paramName}" => ''];
@@ -241,8 +241,8 @@ public static function toQueryValue(
241241
}
242242
}
243243

244-
# Handle DateTime objects in query
245-
if($openApiType === "\DateTime" && $value instanceof DateTime) {
244+
// Handle DateTime objects in query
245+
if ($openApiType === "\\DateTime" && $value instanceof DateTime) {
246246
return ["{$paramName}" => $value->format(self::$dateTimeFormat)];
247247
}
248248

@@ -252,7 +252,9 @@ public static function toQueryValue(
252252
// since \GuzzleHttp\Psr7\Query::build fails with nested arrays
253253
// need to flatten array first
254254
$flattenArray = function ($arr, $name, &$result = []) use (&$flattenArray, $style, $explode) {
255-
if (!is_array($arr)) return $arr;
255+
if (!is_array($arr)) {
256+
return $arr;
257+
}
256258

257259
foreach ($arr as $k => $v) {
258260
$prop = ($style === 'deepObject') ? "{$name}[{$k}]" : $k;
@@ -500,7 +502,7 @@ public static function deserialize(mixed $data, string $class, ?array $httpHeade
500502
$data = is_string($data) ? json_decode($data) : $data;
501503

502504
if (is_array($data)) {
503-
$data = (object)$data;
505+
$data = (object) $data;
504506
}
505507

506508
// If a discriminator is defined and points to a valid subclass, use it.
@@ -539,17 +541,17 @@ public static function deserialize(mixed $data, string $class, ?array $httpHeade
539541
}
540542

541543
/**
542-
* Build a query string from an array of key value pairs.
543-
*
544-
* This function can use the return value of `parse()` to build a query
545-
* string. This function does not modify the provided keys when an array is
546-
* encountered (like `http_build_query()` would).
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-
*/
544+
* Build a query string from an array of key value pairs.
545+
*
546+
* This function can use the return value of `parse()` to build a query
547+
* string. This function does not modify the provided keys when an array is
548+
* encountered (like `http_build_query()` would).
549+
*
550+
* @param array $params Query string parameters.
551+
* @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
552+
* to encode using RFC3986, or PHP_QUERY_RFC1738
553+
* to encode using RFC1738.
554+
*/
553555
public static function buildQuery(array $params, $encoding = PHP_QUERY_RFC3986): string
554556
{
555557
if (!$params) {

samples/client/echo_api/php-nextgen/src/ObjectSerializer.php

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ public static function sanitizeForSerialization(mixed $data, ?string $type = nul
117117
}
118118
}
119119
} else {
120-
foreach($data as $property => $value) {
120+
foreach ($data as $property => $value) {
121121
$values[$property] = self::sanitizeForSerialization($value);
122122
}
123123
}
124-
return (object)$values;
124+
return (object) $values;
125125
} else {
126126
return (string)$data;
127127
}
@@ -179,30 +179,30 @@ public static function toPathValue(string $value): string
179179
*/
180180
private static function isEmptyValue(mixed $value, string $openApiType): bool
181181
{
182-
# If empty() returns false, it is not empty regardless of its type.
182+
// If empty() returns false, it is not empty regardless of its type.
183183
if (!empty($value)) {
184184
return false;
185185
}
186186

187-
# Null is always empty, as we cannot send a real "null" value in a query parameter.
187+
// Null is always empty, as we cannot send a real "null" value in a query parameter.
188188
if ($value === null) {
189189
return true;
190190
}
191191

192192
return match ($openApiType) {
193-
# For numeric values, false and '' are considered empty.
194-
# This comparison is safe for floating point values, since the previous call to empty() will
195-
# filter out values that don't match 0.
193+
// For numeric values, false and '' are considered empty.
194+
// This comparison is safe for floating point values, since the previous call to empty() will
195+
// filter out values that don't match 0.
196196
'int','integer' => $value !== 0,
197197
'number'|'float' => $value !== 0 && $value !== 0.0,
198198

199-
# For boolean values, '' is considered empty
199+
// For boolean values, '' is considered empty
200200
'bool','boolean' => !in_array($value, [false, 0], true),
201201

202-
# For string values, '' is considered empty.
202+
// For string values, '' is considered empty.
203203
'string' => $value === '',
204204

205-
# For all the other types, any value at this point can be considered empty.
205+
// For all the other types, any value at this point can be considered empty.
206206
default => true
207207
};
208208
}
@@ -229,10 +229,10 @@ public static function toQueryValue(
229229
bool $required = true
230230
): array {
231231

232-
# Check if we should omit this parameter from the query. This should only happen when:
233-
# - Parameter is NOT required; AND
234-
# - its value is set to a value that is equivalent to "empty", depending on its OpenAPI type. For
235-
# example, 0 as "int" or "boolean" is NOT an empty value.
232+
// Check if we should omit this parameter from the query. This should only happen when:
233+
// - Parameter is NOT required; AND
234+
// - its value is set to a value that is equivalent to "empty", depending on its OpenAPI type. For
235+
// example, 0 as "int" or "boolean" is NOT an empty value.
236236
if (self::isEmptyValue($value, $openApiType)) {
237237
if ($required) {
238238
return ["{$paramName}" => ''];
@@ -241,8 +241,8 @@ public static function toQueryValue(
241241
}
242242
}
243243

244-
# Handle DateTime objects in query
245-
if($openApiType === "\DateTime" && $value instanceof DateTime) {
244+
// Handle DateTime objects in query
245+
if ($openApiType === "\\DateTime" && $value instanceof DateTime) {
246246
return ["{$paramName}" => $value->format(self::$dateTimeFormat)];
247247
}
248248

@@ -252,7 +252,9 @@ public static function toQueryValue(
252252
// since \GuzzleHttp\Psr7\Query::build fails with nested arrays
253253
// need to flatten array first
254254
$flattenArray = function ($arr, $name, &$result = []) use (&$flattenArray, $style, $explode) {
255-
if (!is_array($arr)) return $arr;
255+
if (!is_array($arr)) {
256+
return $arr;
257+
}
256258

257259
foreach ($arr as $k => $v) {
258260
$prop = ($style === 'deepObject') ? "{$name}[{$k}]" : $k;
@@ -500,7 +502,7 @@ public static function deserialize(mixed $data, string $class, ?array $httpHeade
500502
$data = is_string($data) ? json_decode($data) : $data;
501503

502504
if (is_array($data)) {
503-
$data = (object)$data;
505+
$data = (object) $data;
504506
}
505507

506508
// If a discriminator is defined and points to a valid subclass, use it.
@@ -539,17 +541,17 @@ public static function deserialize(mixed $data, string $class, ?array $httpHeade
539541
}
540542

541543
/**
542-
* Build a query string from an array of key value pairs.
543-
*
544-
* This function can use the return value of `parse()` to build a query
545-
* string. This function does not modify the provided keys when an array is
546-
* encountered (like `http_build_query()` would).
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-
*/
544+
* Build a query string from an array of key value pairs.
545+
*
546+
* This function can use the return value of `parse()` to build a query
547+
* string. This function does not modify the provided keys when an array is
548+
* encountered (like `http_build_query()` would).
549+
*
550+
* @param array $params Query string parameters.
551+
* @param int|false $encoding Set to false to not encode, PHP_QUERY_RFC3986
552+
* to encode using RFC3986, or PHP_QUERY_RFC1738
553+
* to encode using RFC1738.
554+
*/
553555
public static function buildQuery(array $params, $encoding = PHP_QUERY_RFC3986): string
554556
{
555557
if (!$params) {

0 commit comments

Comments
 (0)