From cc395fdf6326ab1742351a9c5652898b0614cbbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20J=C3=B8rgensen?= Date: Sat, 14 May 2016 12:56:11 +0200 Subject: [PATCH] [PHP] Improve generated codestyle --- .../codegen/languages/PhpClientCodegen.java | 12 +++++++ .../src/main/resources/php/ApiClient.mustache | 33 ++++++++---------- .../main/resources/php/ApiException.mustache | 2 +- .../resources/php/ObjectSerializer.mustache | 8 ++--- .../src/main/resources/php/api.mustache | 34 ++++++++++++------- .../src/main/resources/php/api_test.mustache | 4 +-- .../main/resources/php/configuration.mustache | 11 +++--- .../src/main/resources/php/model.mustache | 25 ++++++++------ .../main/resources/php/model_test.mustache | 5 ++- .../phpcs-generated-code.xml | 10 ++++++ 10 files changed, 87 insertions(+), 57 deletions(-) create mode 100644 samples/client/petstore/php/SwaggerClient-php/phpcs-generated-code.xml diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java index c434bbe7a04..7bb9714bc0a 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java @@ -3,6 +3,7 @@ package io.swagger.codegen.languages; import io.swagger.codegen.CliOption; import io.swagger.codegen.CodegenConfig; import io.swagger.codegen.CodegenConstants; +import io.swagger.codegen.CodegenOperation; import io.swagger.codegen.CodegenParameter; import io.swagger.codegen.CodegenProperty; import io.swagger.codegen.CodegenType; @@ -13,6 +14,7 @@ import io.swagger.models.properties.*; import java.io.File; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.HashSet; import java.util.regex.Matcher; @@ -626,4 +628,14 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig { // process enum in models return postProcessModelsEnum(objs); } + + @Override + public Map postProcessOperations(Map objs) { + Map operations = (Map) objs.get("operations"); + List operationList = (List) operations.get("operation"); + for (CodegenOperation op : operationList) { + op.vendorExtensions.put("x-testOperationId", camelize(op.operationId)); + } + return objs; + } } diff --git a/modules/swagger-codegen/src/main/resources/php/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/php/ApiClient.mustache index 252c5d4a950..bfd811b89f0 100644 --- a/modules/swagger-codegen/src/main/resources/php/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/php/ApiClient.mustache @@ -237,7 +237,7 @@ class ApiClient // Handle the response if ($response_info['http_code'] == 0) { throw new ApiException("API call to $url timed out: ".serialize($response_info), 0, null, null); - } elseif ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) { + } elseif ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299) { // return raw body if response is a file if ($responseType == '\SplFileObject' || $responseType == 'string') { return array($http_body, $response_info['http_code'], $http_header); @@ -255,7 +255,9 @@ class ApiClient throw new ApiException( "[".$response_info['http_code']."] Error connecting to the API ($url)", - $response_info['http_code'], $http_header, $data + $response_info['http_code'], + $http_header, + $data ); } return array($data, $response_info['http_code'], $http_header); @@ -310,31 +312,26 @@ class ApiClient $headers = array(); $key = ''; - foreach(explode("\n", $raw_headers) as $h) - { + foreach (explode("\n", $raw_headers) as $h) { $h = explode(':', $h, 2); - if (isset($h[1])) - { - if (!isset($headers[$h[0]])) + if (isset($h[1])) { + if (!isset($headers[$h[0]])) { $headers[$h[0]] = trim($h[1]); - elseif (is_array($headers[$h[0]])) - { + } elseif (is_array($headers[$h[0]])) { $headers[$h[0]] = array_merge($headers[$h[0]], array(trim($h[1]))); - } - else - { + } else { $headers[$h[0]] = array_merge(array($headers[$h[0]]), array(trim($h[1]))); } $key = $h[0]; - } - else - { - if (substr($h[0], 0, 1) == "\t") + } else { + if (substr($h[0], 0, 1) == "\t") { $headers[$key] .= "\r\n\t".trim($h[0]); - elseif (!$key) - $headers[0] = trim($h[0]);trim($h[0]); + } elseif (!$key) { + $headers[0] = trim($h[0]); + } + trim($h[0]); } } diff --git a/modules/swagger-codegen/src/main/resources/php/ApiException.mustache b/modules/swagger-codegen/src/main/resources/php/ApiException.mustache index 64dec1572ab..031e8cd995e 100644 --- a/modules/swagger-codegen/src/main/resources/php/ApiException.mustache +++ b/modules/swagger-codegen/src/main/resources/php/ApiException.mustache @@ -75,7 +75,7 @@ class ApiException extends Exception * @param string $responseHeaders HTTP response header * @param mixed $responseBody HTTP body of the server response either as Json or string */ - public function __construct($message="", $code=0, $responseHeaders=null, $responseBody=null) + public function __construct($message = "", $code = 0, $responseHeaders = null, $responseBody = null) { parent::__construct($message, $code); $this->responseHeaders = $responseHeaders; diff --git a/modules/swagger-codegen/src/main/resources/php/ObjectSerializer.mustache b/modules/swagger-codegen/src/main/resources/php/ObjectSerializer.mustache index 62a128cbb36..7de8d9d085e 100644 --- a/modules/swagger-codegen/src/main/resources/php/ObjectSerializer.mustache +++ b/modules/swagger-codegen/src/main/resources/php/ObjectSerializer.mustache @@ -185,7 +185,7 @@ class ObjectSerializer * * @return string */ - public function serializeCollection(array $collection, $collectionFormat, $allowCollectionFormatMulti=false) + public function serializeCollection(array $collection, $collectionFormat, $allowCollectionFormatMulti = false) { if ($allowCollectionFormatMulti && ('multi' === $collectionFormat)) { // http_build_query() almost does the job for us. We just @@ -219,7 +219,7 @@ class ObjectSerializer * * @return object an instance of $class */ - public static function deserialize($data, $class, $httpHeaders=null, $discriminator=null) + public static function deserialize($data, $class, $httpHeaders = null, $discriminator = null) { if (null === $data) { return null; @@ -261,7 +261,8 @@ class ObjectSerializer return $data; } elseif ($class === '\SplFileObject') { // determine file name - if (array_key_exists('Content-Disposition', $httpHeaders) && preg_match('/inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeaders['Content-Disposition'], $match)) { + if (array_key_exists('Content-Disposition', $httpHeaders) && + preg_match('/inline; filename=[\'"]?([^\'"\s]+)[\'"]?$/i', $httpHeaders['Content-Disposition'], $match)) { $filename = Configuration::getDefaultConfiguration()->getTempFolderPath() . sanitizeFilename($match[1]); } else { $filename = tempnam(Configuration::getDefaultConfiguration()->getTempFolderPath(), ''); @@ -274,7 +275,6 @@ class ObjectSerializer } return $deserialized; - } else { // If a discriminator is defined and points to a valid subclass, use it. if (!empty($discriminator) && isset($data->{$discriminator}) && is_string($data->{$discriminator})) { diff --git a/modules/swagger-codegen/src/main/resources/php/api.mustache b/modules/swagger-codegen/src/main/resources/php/api.mustache index 34827ed0117..7a9b2afa993 100644 --- a/modules/swagger-codegen/src/main/resources/php/api.mustache +++ b/modules/swagger-codegen/src/main/resources/php/api.mustache @@ -62,7 +62,7 @@ use \{{invokerPackage}}\ObjectSerializer; * * @param \{{invokerPackage}}\ApiClient|null $apiClient The api client to use */ - function __construct(\{{invokerPackage}}\ApiClient $apiClient = null) + public function __construct(\{{invokerPackage}}\ApiClient $apiClient = null) { if ($apiClient == null) { $apiClient = new ApiClient(); @@ -99,7 +99,7 @@ use \{{invokerPackage}}\ObjectSerializer; /** * Operation {{{operationId}}} * - * {{{summary}}} + * {{{summary}}}. * {{#allParams}} * @param {{dataType}} ${{paramName}} {{description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}} {{/allParams}} * @@ -116,7 +116,7 @@ use \{{invokerPackage}}\ObjectSerializer; /** * Operation {{{operationId}}}WithHttpInfo * - * {{{summary}}} + * {{{summary}}}. * {{#allParams}} * @param {{dataType}} ${{paramName}} {{description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}} {{/allParams}} * @@ -215,7 +215,7 @@ use \{{invokerPackage}}\ObjectSerializer; if (function_exists('curl_file_create')) { $formParams['{{baseName}}'] = curl_file_create($this->apiClient->getSerializer()->toFormValue(${{paramName}})); } else { - $formParams['{{baseName}}'] = '@' . $this->apiClient->getSerializer()->toFormValue(${{paramName}}); + $formParams['{{baseName}}'] = '@' . $this->apiClient->getSerializer()->toFormValue(${{paramName}}); } {{/isFile}} {{^isFile}} @@ -251,9 +251,12 @@ use \{{invokerPackage}}\ObjectSerializer; // make the API Call try { list($response, $statusCode, $httpHeader) = $this->apiClient->callApi( - $resourcePath, '{{httpMethod}}', - $queryParams, $httpBody, - $headerParams{{#returnType}}, '{{returnType}}'{{/returnType}} + $resourcePath, + '{{httpMethod}}', + $queryParams, + $httpBody, + $headerParams{{#returnType}}, + '{{returnType}}'{{/returnType}} ); {{#returnType}} if (!$response) { @@ -261,15 +264,20 @@ use \{{invokerPackage}}\ObjectSerializer; } return array($this->apiClient->getSerializer()->deserialize($response, '{{returnType}}', $httpHeader{{#discriminator}}, '{{discriminator}}'{{/discriminator}}), $statusCode, $httpHeader); - {{/returnType}}{{^returnType}} + {{/returnType}} + {{^returnType}} return array(null, $statusCode, $httpHeader); {{/returnType}} } catch (ApiException $e) { - switch ($e->getCode()) { {{#responses}}{{#dataType}} - {{^isWildcard}}case {{code}}:{{/isWildcard}}{{#isWildcard}}default:{{/isWildcard}} - $data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '{{dataType}}', $e->getResponseHeaders()); - $e->setResponseObject($data); - break;{{/dataType}}{{/responses}} + switch ($e->getCode()) { + {{#responses}} + {{#dataType}} + {{^isWildcard}}case {{code}}:{{/isWildcard}}{{#isWildcard}}default:{{/isWildcard}} + $data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), '{{dataType}}', $e->getResponseHeaders()); + $e->setResponseObject($data); + break; + {{/dataType}} + {{/responses}} } throw $e; diff --git a/modules/swagger-codegen/src/main/resources/php/api_test.mustache b/modules/swagger-codegen/src/main/resources/php/api_test.mustache index d41e76d040d..6a525223584 100644 --- a/modules/swagger-codegen/src/main/resources/php/api_test.mustache +++ b/modules/swagger-codegen/src/main/resources/php/api_test.mustache @@ -70,10 +70,10 @@ use \{{invokerPackage}}\ObjectSerializer; /** * Test case for {{{operationId}}} * - * {{{summary}}} + * {{{summary}}}. * */ - public function test_{{operationId}}() + public function test{{vendorExtensions.x-testOperationId}}() { } diff --git a/modules/swagger-codegen/src/main/resources/php/configuration.mustache b/modules/swagger-codegen/src/main/resources/php/configuration.mustache index 0062fa8c25f..1b920e48357 100644 --- a/modules/swagger-codegen/src/main/resources/php/configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/php/configuration.mustache @@ -47,7 +47,7 @@ namespace {{invokerPackage}}; class Configuration { - private static $_defaultConfiguration = null; + private static $defaultConfiguration = null; /** * Associate array to store API key(s) @@ -487,11 +487,11 @@ class Configuration */ public static function getDefaultConfiguration() { - if (self::$_defaultConfiguration == null) { - self::$_defaultConfiguration = new Configuration(); + if (self::$defaultConfiguration == null) { + self::$defaultConfiguration = new Configuration(); } - return self::$_defaultConfiguration; + return self::$defaultConfiguration; } /** @@ -503,7 +503,7 @@ class Configuration */ public static function setDefaultConfiguration(Configuration $config) { - self::$_defaultConfiguration = $config; + self::$defaultConfiguration = $config; } /** @@ -522,5 +522,4 @@ class Configuration return $report; } - } diff --git a/modules/swagger-codegen/src/main/resources/php/model.mustache b/modules/swagger-codegen/src/main/resources/php/model.mustache index ac08ed699b2..488b82433d9 100644 --- a/modules/swagger-codegen/src/main/resources/php/model.mustache +++ b/modules/swagger-codegen/src/main/resources/php/model.mustache @@ -41,7 +41,9 @@ use \ArrayAccess; * {{classname}} Class Doc Comment * * @category Class +{{#description}} * @description {{description}} +{{/description}} * @package {{invokerPackage}} * @author http://github.com/swagger-api/swagger-codegen * @license http://www.apache.org/licenses/LICENSE-2.0 Apache Licene v2 @@ -53,18 +55,19 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple * The original name of the model. * @var string */ - static $swaggerModelName = '{{name}}'; + protected static $swaggerModelName = '{{name}}'; /** * Array of property to type mappings. Used for (de)serialization * @var string[] */ - static $swaggerTypes = array( + protected static $swaggerTypes = array( {{#vars}}'{{name}}' => '{{{datatype}}}'{{#hasMore}}, {{/hasMore}}{{/vars}} ); - static function swaggerTypes() { + public static function swaggerTypes() + { return self::$swaggerTypes{{#parentSchema}} + parent::swaggerTypes(){{/parentSchema}}; } @@ -72,12 +75,13 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple * Array of attributes where the key is the local name, and the value is the original name * @var string[] */ - static $attributeMap = array( + protected static $attributeMap = array( {{#vars}}'{{name}}' => '{{baseName}}'{{#hasMore}}, {{/hasMore}}{{/vars}} ); - static function attributeMap() { + public static function attributeMap() + { return {{#parentSchema}}parent::attributeMap() + {{/parentSchema}}self::$attributeMap; } @@ -85,12 +89,13 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple * Array of attributes to setter functions (for deserialization of responses) * @var string[] */ - static $setters = array( + protected static $setters = array( {{#vars}}'{{name}}' => '{{setter}}'{{#hasMore}}, {{/hasMore}}{{/vars}} ); - static function setters() { + public static function setters() + { return {{#parentSchema}}parent::setters() + {{/parentSchema}}self::$setters; } @@ -98,12 +103,12 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple * Array of attributes to getter functions (for serialization of requests) * @var string[] */ - static $getters = array( + protected static $getters = array( {{#vars}}'{{name}}' => '{{getter}}'{{#hasMore}}, {{/hasMore}}{{/vars}} ); - static function getters() + public static function getters() { return {{#parentSchema}}parent::getters() + {{/parentSchema}}self::$getters; } @@ -267,7 +272,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple /** * Sets {{name}} - * @param {{datatype}} ${{name}} {{#description}}{{{description}}}{{/description}} + * @param {{datatype}} ${{name}}{{#description}} {{{description}}}{{/description}} * @return $this */ public function {{setter}}(${{name}}) diff --git a/modules/swagger-codegen/src/main/resources/php/model_test.mustache b/modules/swagger-codegen/src/main/resources/php/model_test.mustache index b79b5840e30..3482629f917 100644 --- a/modules/swagger-codegen/src/main/resources/php/model_test.mustache +++ b/modules/swagger-codegen/src/main/resources/php/model_test.mustache @@ -39,7 +39,7 @@ namespace {{modelPackage}}; * {{classname}}Test Class Doc Comment * * @category Class - * @description {{description}} + * @description {{#description}}{{description}}{{/description}}{{^description}}{{classname}}{{/description}} * @package {{invokerPackage}} * @author http://github.com/swagger-api/swagger-codegen * @license http://www.apache.org/licenses/LICENSE-2.0 Apache Licene v2 @@ -65,13 +65,12 @@ class {{classname}}Test extends \PHPUnit_Framework_TestCase } /** - * Test {{classname}} + * Test "{{classname}}" */ public function test{{classname}}() { } - } {{/model}} {{/models}} diff --git a/samples/client/petstore/php/SwaggerClient-php/phpcs-generated-code.xml b/samples/client/petstore/php/SwaggerClient-php/phpcs-generated-code.xml new file mode 100644 index 00000000000..a0173077aa9 --- /dev/null +++ b/samples/client/petstore/php/SwaggerClient-php/phpcs-generated-code.xml @@ -0,0 +1,10 @@ + + + + Arnes Drupal code checker + + + + * + +