Skip to content

Commit 47d6886

Browse files
authored
Merge pull request #1119 from swagger-api/codegen-issue-10392
PHP 7.4 Support
2 parents 8f1af14 + e55eef9 commit 47d6886

File tree

8 files changed

+70
-51
lines changed

8 files changed

+70
-51
lines changed

src/main/java/io/swagger/codegen/v3/generators/php/PhpClientCodegen.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public PhpClientCodegen() {
110110

111111
instantiationTypes.put("array", "array");
112112
instantiationTypes.put("map", "map");
113-
113+
114114
// provide primitives to mustache template
115115
List<String> sortedLanguageSpecificPrimitives= new ArrayList<String>(languageSpecificPrimitives);
116116
Collections.sort(sortedLanguageSpecificPrimitives);
@@ -295,6 +295,12 @@ public void processOpts() {
295295
if (additionalProperties.containsKey(VARIABLE_NAMING_CONVENTION)) {
296296
this.setParameterNamingConvention((String) additionalProperties.get(VARIABLE_NAMING_CONVENTION));
297297
}
298+
if (StringUtils.isBlank(composerVendorName) && additionalProperties.get(CodegenConstants.GIT_USER_ID) != null) {
299+
additionalProperties.put(CodegenConstants.GIT_USER_ID, StringUtils.lowerCase(additionalProperties.get(CodegenConstants.GIT_USER_ID).toString()));
300+
}
301+
if (StringUtils.isBlank(composerProjectName) && additionalProperties.get(CodegenConstants.GIT_REPO_ID) != null) {
302+
additionalProperties.put(CodegenConstants.GIT_REPO_ID, StringUtils.lowerCase(additionalProperties.get(CodegenConstants.GIT_REPO_ID).toString()));
303+
}
298304

299305
additionalProperties.put("escapedInvokerPackage", invokerPackage.replace("\\", "\\\\"));
300306

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
<?php
22

3-
return Symfony\CS\Config::create()
4-
->level(Symfony\CS\FixerInterface::PSR2_LEVEL)
3+
return PhpCsFixer\Config::create()
54
->setUsingCache(true)
6-
->fixers(
7-
[
8-
'ordered_use',
9-
'phpdoc_order',
10-
'short_array_syntax',
11-
'strict',
12-
'strict_param'
13-
]
14-
)
15-
->finder(
16-
Symfony\CS\Finder\DefaultFinder::create()
17-
->in(__DIR__)
5+
->setRules([
6+
'@PSR2' => true,
7+
'ordered_imports' => true,
8+
'phpdoc_order' => true,
9+
'array_syntax' => [ 'syntax' => 'short' ],
10+
'strict_comparison' => true,
11+
'strict_param' => true,
12+
'no_trailing_whitespace' => false,
13+
'no_trailing_whitespace_in_comment' => false,
14+
'braces' => false,
15+
'single_blank_line_at_eof' => false,
16+
'blank_line_after_namespace' => false,
17+
])
18+
->setFinder(
19+
PhpCsFixer\Finder::create()
20+
->exclude('test')
21+
->exclude('tests')
22+
->in(__DIR__)
1823
);

src/main/resources/handlebars/php/HeaderSelector.mustache

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,3 @@ class HeaderSelector
9797
}
9898
}
9999
}
100-

src/main/resources/handlebars/php/ObjectSerializer.mustache

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ class ObjectSerializer
3333
* Serialize data
3434
*
3535
* @param mixed $data the data to serialize
36+
* @param string $type the SwaggerType of the data
3637
* @param string $format the format of the Swagger type of the data
3738
*
3839
* @return string|object serialized form of $data
3940
*/
40-
public static function sanitizeForSerialization($data, $format = null)
41+
public static function sanitizeForSerialization($data, $type = null, $format = null)
4142
{
4243
if (is_scalar($data) || null === $data) {
4344
return $data;
@@ -48,6 +49,11 @@ class ObjectSerializer
4849
$data[$property] = self::sanitizeForSerialization($value);
4950
}
5051
return $data;
52+
} elseif ($data instanceof \stdClass) {
53+
foreach ($data as $property => $value) {
54+
$data->$property = self::sanitizeForSerialization($value);
55+
}
56+
return $data;
5157
} elseif (is_object($data)) {
5258
$values = [];
5359
$formats = $data::swaggerFormats();
@@ -57,7 +63,7 @@ class ObjectSerializer
5763
if ($value !== null
5864
&& !in_array($swaggerType, [{{&primitives}}], true)
5965
&& method_exists($swaggerType, 'getAllowableEnumValues')
60-
&& !in_array($value, $swaggerType::getAllowableEnumValues())) {
66+
&& !in_array($value, $swaggerType::getAllowableEnumValues(), true)) {
6167
$imploded = implode("', '", $swaggerType::getAllowableEnumValues());
6268
throw new \InvalidArgumentException("Invalid value for enum '$swaggerType', must be one of: '$imploded'");
6369
}
@@ -108,23 +114,22 @@ class ObjectSerializer
108114
* later.
109115
*
110116
* @param string[]|string|\DateTime $object an object to be serialized to a string
111-
* @param string|null $format the format of the parameter
112117
*
113118
* @return string the serialized object
114119
*/
115-
public static function toQueryValue($object, $format = null)
120+
public static function toQueryValue($object)
116121
{
117122
if (is_array($object)) {
118123
return implode(',', $object);
119124
} else {
120-
return self::toString($object, $format);
125+
return self::toString($object);
121126
}
122127
}
123128

124129
/**
125130
* Take value and turn it into a string suitable for inclusion in
126131
* the header. If it's a string, pass through unchanged
127-
* If it's a datetime object, format it in RFC3339
132+
* If it's a datetime object, format it in ISO8601
128133
*
129134
* @param string $value a string which will be part of the header
130135
*
@@ -138,7 +143,7 @@ class ObjectSerializer
138143
/**
139144
* Take value and turn it into a string suitable for inclusion in
140145
* the http body (form parameter). If it's a string, pass through unchanged
141-
* If it's a datetime object, format it in RFC3339
146+
* If it's a datetime object, format it in ISO8601
142147
*
143148
* @param string|\SplFileObject $value the value of the form parameter
144149
*
@@ -156,18 +161,16 @@ class ObjectSerializer
156161
/**
157162
* Take value and turn it into a string suitable for inclusion in
158163
* the parameter. If it's a string, pass through unchanged
159-
* If it's a datetime object, format it in RFC3339
160-
* If it's a date, format it in Y-m-d
164+
* If it's a datetime object, format it in ISO8601
161165
*
162166
* @param string|\DateTime $value the value of the parameter
163-
* @param string|null $format the format of the parameter
164167
*
165168
* @return string the header string
166169
*/
167-
public static function toString($value, $format = null)
170+
public static function toString($value)
168171
{
169-
if ($value instanceof \DateTime) {
170-
return ($format === 'date') ? $value->format('Y-m-d') : $value->format(\DateTime::ATOM);
172+
if ($value instanceof \DateTime) { // datetime in ISO8601 format
173+
return $value->format(\DateTime::ATOM);
171174
} else {
172175
return $value;
173176
}
@@ -276,7 +279,7 @@ class ObjectSerializer
276279

277280
return new \SplFileObject($filename, 'r');
278281
} elseif (method_exists($class, 'getAllowableEnumValues')) {
279-
if (!in_array($data, $class::getAllowableEnumValues())) {
282+
if (!in_array($data, $class::getAllowableEnumValues(), true)) {
280283
$imploded = implode("', '", $class::getAllowableEnumValues());
281284
throw new \InvalidArgumentException("Invalid value for enum '$class', must be one of: '$imploded'");
282285
}

src/main/resources/handlebars/php/api_test.mustache

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace {{invokerPackage}};
2121
use {{backslash}}{{invokerPackage}}\Configuration;
2222
use {{backslash}}{{invokerPackage}}\ApiException;
2323
use {{backslash}}{{invokerPackage}}\ObjectSerializer;
24+
use PHPUnit\Framework\TestCase;
2425

2526
/**
2627
* {{classname}}Test Class Doc Comment
@@ -30,47 +31,49 @@ use {{backslash}}{{invokerPackage}}\ObjectSerializer;
3031
* @author Swagger Codegen team
3132
* @link https://github.com/swagger-api/swagger-codegen
3233
*/
33-
{{#operations}}class {{classname}}Test extends \PHPUnit_Framework_TestCase
34+
{{#operations}}class {{classname}}Test extends TestCase
3435
{
3536
3637
/**
3738
* Setup before running any test cases
3839
*/
39-
public static function setUpBeforeClass()
40+
public static function setUpBeforeClass(): void
4041
{
4142
}
4243

4344
/**
4445
* Setup before running each test case
4546
*/
46-
public function setUp()
47+
public function setUp(): void
4748
{
4849
}
4950

5051
/**
5152
* Clean up after running each test case
5253
*/
53-
public function tearDown()
54+
public function tearDown(): void
5455
{
5556
}
5657

5758
/**
5859
* Clean up after running all test cases
5960
*/
60-
public static function tearDownAfterClass()
61+
public static function tearDownAfterClass(): void
6162
{
6263
}
6364
{{#operation}}
65+
{{#contents}}
6466

6567
/**
6668
* Test case for {{{operationId}}}
6769
*
6870
* {{{summary}}}.
6971
*
7072
*/
71-
public function test{{vendorExtensions.x-testOperationId}}()
73+
public function test{{vendorExtensions.x-testOperationId}}{{#isForm}}Form{{/isForm}}()
7274
{
7375
}
76+
{{/contents}}
7477
{{/operation}}
7578
}
7679
{{/operations}}

src/main/resources/handlebars/php/composer.mustache

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"swagger",
99
"php",
1010
"sdk",
11+
"rest",
1112
"api"
1213
],
1314
"homepage": "http://swagger.io",
@@ -19,16 +20,16 @@
1920
}
2021
],
2122
"require": {
22-
"php": ">=5.5",
23+
"php": "^7.4 || ^8.0",
2324
"ext-curl": "*",
2425
"ext-json": "*",
2526
"ext-mbstring": "*",
26-
"guzzlehttp/guzzle": "^6.2"
27+
"guzzlehttp/guzzle": "^7.3",
28+
"guzzlehttp/psr7": "^1.7 || ^2.0"
2729
},
2830
"require-dev": {
29-
"phpunit/phpunit": "^4.8",
30-
"squizlabs/php_codesniffer": "~2.6",
31-
"friendsofphp/php-cs-fixer": "~1.12"
31+
"phpunit/phpunit": "^8.0 || ^9.0",
32+
"friendsofphp/php-cs-fixer": "^3.5"
3233
},
3334
"autoload": {
3435
"psr-4": { "{{escapedInvokerPackage}}\\" : "{{srcBasePath}}/" }

src/main/resources/handlebars/php/model_generic.mustache

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
166166
{{#discriminator}}
167167

168168
// Initialize discriminator property with the model name.
169-
$discriminator = array_search('{{discriminator.propertyName}}', self::$attributeMap);
169+
$discriminator = array_search('{{discriminator.propertyName}}', self::$attributeMap, true);
170170
$this->container[$discriminator] = static::$swaggerModelName;
171171
{{/discriminator}}
172172
}
@@ -353,7 +353,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
353353
*
354354
* @return boolean
355355
*/
356-
#[\ReturnTypeWillChange]
356+
#[\ReturnTypeWillChange]
357357
public function offsetExists($offset)
358358
{
359359
return isset($this->container[$offset]);
@@ -366,7 +366,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
366366
*
367367
* @return mixed
368368
*/
369-
#[\ReturnTypeWillChange]
369+
#[\ReturnTypeWillChange]
370370
public function offsetGet($offset)
371371
{
372372
return isset($this->container[$offset]) ? $this->container[$offset] : null;
@@ -380,7 +380,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
380380
*
381381
* @return void
382382
*/
383-
#[\ReturnTypeWillChange]
383+
#[\ReturnTypeWillChange]
384384
public function offsetSet($offset, $value)
385385
{
386386
if (is_null($offset)) {
@@ -397,7 +397,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
397397
*
398398
* @return void
399399
*/
400-
#[\ReturnTypeWillChange]
400+
#[\ReturnTypeWillChange]
401401
public function offsetUnset($offset)
402402
{
403403
unset($this->container[$offset]);

src/main/resources/handlebars/php/model_test.mustache

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
namespace {{invokerPackage}};
2323

24+
use PHPUnit\Framework\TestCase;
25+
2426
/**
2527
* {{classname}}Test Class Doc Comment
2628
*
@@ -30,34 +32,34 @@ namespace {{invokerPackage}};
3032
* @author Swagger Codegen team
3133
* @link https://github.com/swagger-api/swagger-codegen
3234
*/
33-
class {{classname}}Test extends \PHPUnit_Framework_TestCase
35+
class {{classname}}Test extends TestCase
3436
{
3537
3638
/**
3739
* Setup before running any test case
3840
*/
39-
public static function setUpBeforeClass()
41+
public static function setUpBeforeClass(): void
4042
{
4143
}
4244

4345
/**
4446
* Setup before running each test case
4547
*/
46-
public function setUp()
48+
public function setUp(): void
4749
{
4850
}
4951

5052
/**
5153
* Clean up after running each test case
5254
*/
53-
public function tearDown()
55+
public function tearDown(): void
5456
{
5557
}
5658

5759
/**
5860
* Clean up after running all test cases
5961
*/
60-
public static function tearDownAfterClass()
62+
public static function tearDownAfterClass(): void
6163
{
6264
}
6365

0 commit comments

Comments
 (0)