Skip to content

Commit

Permalink
Merge pull request #1119 from swagger-api/codegen-issue-10392
Browse files Browse the repository at this point in the history
PHP 7.4 Support
  • Loading branch information
HugoMario authored Mar 18, 2023
2 parents 8f1af14 + e55eef9 commit 47d6886
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public PhpClientCodegen() {

instantiationTypes.put("array", "array");
instantiationTypes.put("map", "map");

// provide primitives to mustache template
List<String> sortedLanguageSpecificPrimitives= new ArrayList<String>(languageSpecificPrimitives);
Collections.sort(sortedLanguageSpecificPrimitives);
Expand Down Expand Up @@ -295,6 +295,12 @@ public void processOpts() {
if (additionalProperties.containsKey(VARIABLE_NAMING_CONVENTION)) {
this.setParameterNamingConvention((String) additionalProperties.get(VARIABLE_NAMING_CONVENTION));
}
if (StringUtils.isBlank(composerVendorName) && additionalProperties.get(CodegenConstants.GIT_USER_ID) != null) {
additionalProperties.put(CodegenConstants.GIT_USER_ID, StringUtils.lowerCase(additionalProperties.get(CodegenConstants.GIT_USER_ID).toString()));
}
if (StringUtils.isBlank(composerProjectName) && additionalProperties.get(CodegenConstants.GIT_REPO_ID) != null) {
additionalProperties.put(CodegenConstants.GIT_REPO_ID, StringUtils.lowerCase(additionalProperties.get(CodegenConstants.GIT_REPO_ID).toString()));
}

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

Expand Down
33 changes: 19 additions & 14 deletions src/main/resources/handlebars/php/.php_cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
<?php

return Symfony\CS\Config::create()
->level(Symfony\CS\FixerInterface::PSR2_LEVEL)
return PhpCsFixer\Config::create()
->setUsingCache(true)
->fixers(
[
'ordered_use',
'phpdoc_order',
'short_array_syntax',
'strict',
'strict_param'
]
)
->finder(
Symfony\CS\Finder\DefaultFinder::create()
->in(__DIR__)
->setRules([
'@PSR2' => true,
'ordered_imports' => true,
'phpdoc_order' => true,
'array_syntax' => [ 'syntax' => 'short' ],
'strict_comparison' => true,
'strict_param' => true,
'no_trailing_whitespace' => false,
'no_trailing_whitespace_in_comment' => false,
'braces' => false,
'single_blank_line_at_eof' => false,
'blank_line_after_namespace' => false,
])
->setFinder(
PhpCsFixer\Finder::create()
->exclude('test')
->exclude('tests')
->in(__DIR__)
);
1 change: 0 additions & 1 deletion src/main/resources/handlebars/php/HeaderSelector.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,3 @@ class HeaderSelector
}
}
}

31 changes: 17 additions & 14 deletions src/main/resources/handlebars/php/ObjectSerializer.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ class ObjectSerializer
* Serialize data
*
* @param mixed $data the data to serialize
* @param string $type the SwaggerType of the data
* @param string $format the format of the Swagger type of the data
*
* @return string|object serialized form of $data
*/
public static function sanitizeForSerialization($data, $format = null)
public static function sanitizeForSerialization($data, $type = null, $format = null)
{
if (is_scalar($data) || null === $data) {
return $data;
Expand All @@ -48,6 +49,11 @@ class ObjectSerializer
$data[$property] = self::sanitizeForSerialization($value);
}
return $data;
} elseif ($data instanceof \stdClass) {
foreach ($data as $property => $value) {
$data->$property = self::sanitizeForSerialization($value);
}
return $data;
} elseif (is_object($data)) {
$values = [];
$formats = $data::swaggerFormats();
Expand All @@ -57,7 +63,7 @@ class ObjectSerializer
if ($value !== null
&& !in_array($swaggerType, [{{&primitives}}], true)
&& method_exists($swaggerType, 'getAllowableEnumValues')
&& !in_array($value, $swaggerType::getAllowableEnumValues())) {
&& !in_array($value, $swaggerType::getAllowableEnumValues(), true)) {
$imploded = implode("', '", $swaggerType::getAllowableEnumValues());
throw new \InvalidArgumentException("Invalid value for enum '$swaggerType', must be one of: '$imploded'");
}
Expand Down Expand Up @@ -108,23 +114,22 @@ class ObjectSerializer
* later.
*
* @param string[]|string|\DateTime $object an object to be serialized to a string
* @param string|null $format the format of the parameter
*
* @return string the serialized object
*/
public static function toQueryValue($object, $format = null)
public static function toQueryValue($object)
{
if (is_array($object)) {
return implode(',', $object);
} else {
return self::toString($object, $format);
return self::toString($object);
}
}

/**
* Take value and turn it into a string suitable for inclusion in
* the header. If it's a string, pass through unchanged
* If it's a datetime object, format it in RFC3339
* If it's a datetime object, format it in ISO8601
*
* @param string $value a string which will be part of the header
*
Expand All @@ -138,7 +143,7 @@ class ObjectSerializer
/**
* Take value and turn it into a string suitable for inclusion in
* the http body (form parameter). If it's a string, pass through unchanged
* If it's a datetime object, format it in RFC3339
* If it's a datetime object, format it in ISO8601
*
* @param string|\SplFileObject $value the value of the form parameter
*
Expand All @@ -156,18 +161,16 @@ class ObjectSerializer
/**
* Take value and turn it into a string suitable for inclusion in
* the parameter. If it's a string, pass through unchanged
* If it's a datetime object, format it in RFC3339
* If it's a date, format it in Y-m-d
* If it's a datetime object, format it in ISO8601
*
* @param string|\DateTime $value the value of the parameter
* @param string|null $format the format of the parameter
*
* @return string the header string
*/
public static function toString($value, $format = null)
public static function toString($value)
{
if ($value instanceof \DateTime) {
return ($format === 'date') ? $value->format('Y-m-d') : $value->format(\DateTime::ATOM);
if ($value instanceof \DateTime) { // datetime in ISO8601 format
return $value->format(\DateTime::ATOM);
} else {
return $value;
}
Expand Down Expand Up @@ -276,7 +279,7 @@ class ObjectSerializer

return new \SplFileObject($filename, 'r');
} elseif (method_exists($class, 'getAllowableEnumValues')) {
if (!in_array($data, $class::getAllowableEnumValues())) {
if (!in_array($data, $class::getAllowableEnumValues(), true)) {
$imploded = implode("', '", $class::getAllowableEnumValues());
throw new \InvalidArgumentException("Invalid value for enum '$class', must be one of: '$imploded'");
}
Expand Down
15 changes: 9 additions & 6 deletions src/main/resources/handlebars/php/api_test.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace {{invokerPackage}};
use {{backslash}}{{invokerPackage}}\Configuration;
use {{backslash}}{{invokerPackage}}\ApiException;
use {{backslash}}{{invokerPackage}}\ObjectSerializer;
use PHPUnit\Framework\TestCase;

/**
* {{classname}}Test Class Doc Comment
Expand All @@ -30,47 +31,49 @@ use {{backslash}}{{invokerPackage}}\ObjectSerializer;
* @author Swagger Codegen team
* @link https://github.com/swagger-api/swagger-codegen
*/
{{#operations}}class {{classname}}Test extends \PHPUnit_Framework_TestCase
{{#operations}}class {{classname}}Test extends TestCase
{
/**
* Setup before running any test cases
*/
public static function setUpBeforeClass()
public static function setUpBeforeClass(): void
{
}

/**
* Setup before running each test case
*/
public function setUp()
public function setUp(): void
{
}

/**
* Clean up after running each test case
*/
public function tearDown()
public function tearDown(): void
{
}

/**
* Clean up after running all test cases
*/
public static function tearDownAfterClass()
public static function tearDownAfterClass(): void
{
}
{{#operation}}
{{#contents}}

/**
* Test case for {{{operationId}}}
*
* {{{summary}}}.
*
*/
public function test{{vendorExtensions.x-testOperationId}}()
public function test{{vendorExtensions.x-testOperationId}}{{#isForm}}Form{{/isForm}}()
{
}
{{/contents}}
{{/operation}}
}
{{/operations}}
11 changes: 6 additions & 5 deletions src/main/resources/handlebars/php/composer.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"swagger",
"php",
"sdk",
"rest",
"api"
],
"homepage": "http://swagger.io",
Expand All @@ -19,16 +20,16 @@
}
],
"require": {
"php": ">=5.5",
"php": "^7.4 || ^8.0",
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"guzzlehttp/guzzle": "^6.2"
"guzzlehttp/guzzle": "^7.3",
"guzzlehttp/psr7": "^1.7 || ^2.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8",
"squizlabs/php_codesniffer": "~2.6",
"friendsofphp/php-cs-fixer": "~1.12"
"phpunit/phpunit": "^8.0 || ^9.0",
"friendsofphp/php-cs-fixer": "^3.5"
},
"autoload": {
"psr-4": { "{{escapedInvokerPackage}}\\" : "{{srcBasePath}}/" }
Expand Down
10 changes: 5 additions & 5 deletions src/main/resources/handlebars/php/model_generic.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
{{#discriminator}}

// Initialize discriminator property with the model name.
$discriminator = array_search('{{discriminator.propertyName}}', self::$attributeMap);
$discriminator = array_search('{{discriminator.propertyName}}', self::$attributeMap, true);
$this->container[$discriminator] = static::$swaggerModelName;
{{/discriminator}}
}
Expand Down Expand Up @@ -353,7 +353,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
*
* @return boolean
*/
#[\ReturnTypeWillChange]
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return isset($this->container[$offset]);
Expand All @@ -366,7 +366,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
*
* @return mixed
*/
#[\ReturnTypeWillChange]
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return isset($this->container[$offset]) ? $this->container[$offset] : null;
Expand All @@ -380,7 +380,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
*
* @return void
*/
#[\ReturnTypeWillChange]
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
if (is_null($offset)) {
Expand All @@ -397,7 +397,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
*
* @return void
*/
#[\ReturnTypeWillChange]
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
unset($this->container[$offset]);
Expand Down
12 changes: 7 additions & 5 deletions src/main/resources/handlebars/php/model_test.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

namespace {{invokerPackage}};

use PHPUnit\Framework\TestCase;

/**
* {{classname}}Test Class Doc Comment
*
Expand All @@ -30,34 +32,34 @@ namespace {{invokerPackage}};
* @author Swagger Codegen team
* @link https://github.com/swagger-api/swagger-codegen
*/
class {{classname}}Test extends \PHPUnit_Framework_TestCase
class {{classname}}Test extends TestCase
{
/**
* Setup before running any test case
*/
public static function setUpBeforeClass()
public static function setUpBeforeClass(): void
{
}

/**
* Setup before running each test case
*/
public function setUp()
public function setUp(): void
{
}

/**
* Clean up after running each test case
*/
public function tearDown()
public function tearDown(): void
{
}

/**
* Clean up after running all test cases
*/
public static function tearDownAfterClass()
public static function tearDownAfterClass(): void
{
}

Expand Down

0 comments on commit 47d6886

Please sign in to comment.