Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public PhpClientCodegen() {
// clear import mapping (from default generator) as php does not use it
// at the moment
importMapping.clear();


supportsInheritance = true;
outputFolder = "generated-code" + File.separator + "php";
Expand Down Expand Up @@ -291,6 +291,7 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("autoload.mustache", getPackagePath(), "autoload.php"));
supportingFiles.add(new SupportingFile("README.mustache", getPackagePath(), "README.md"));
supportingFiles.add(new SupportingFile(".travis.yml", getPackagePath(), ".travis.yml"));
supportingFiles.add(new SupportingFile(".php_cs", getPackagePath(), ".php_cs"));
supportingFiles.add(new SupportingFile("git_push.sh.mustache", getPackagePath(), "git_push.sh"));
// apache v2 license
supportingFiles.add(new SupportingFile("LICENSE", getPackagePath(), "LICENSE"));
Expand Down Expand Up @@ -447,7 +448,7 @@ public String toParamName(String name) {

@Override
public String toModelName(String name) {
// remove [
// remove [
name = name.replaceAll("\\]", "");

// Note: backslash ("\\") is allowed for e.g. "\\DateTime"
Expand All @@ -472,7 +473,7 @@ public String toModelName(String name) {
if (!name.matches("^\\\\.*")) {
name = modelNamePrefix + name + modelNameSuffix;
}

// camelize the model name
// phone_number => PhoneNumber
return camelize(name);
Expand Down
18 changes: 18 additions & 0 deletions modules/swagger-codegen/src/main/resources/php/.php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

return Symfony\CS\Config::create()
->level(Symfony\CS\FixerInterface::PSR2_LEVEL)
->setUsingCache(true)
->fixers(
[
'ordered_use',
'phpdoc_order',
'short_array_syntax',
'strict',
'strict_param'
]
)
->finder(
Symfony\CS\Finder\DefaultFinder::create()
->in(__DIR__)
);
44 changes: 21 additions & 23 deletions modules/swagger-codegen/src/main/resources/php/ApiClient.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ namespace {{invokerPackage}};
*/
class ApiClient
{

public static $PATCH = "PATCH";
public static $POST = "POST";
public static $GET = "GET";
Expand Down Expand Up @@ -61,7 +60,7 @@ class ApiClient
*/
public function __construct(\{{invokerPackage}}\Configuration $config = null)
{
if ($config == null) {
if ($config === null) {
$config = Configuration::getDefaultConfiguration();
}

Expand Down Expand Up @@ -130,8 +129,7 @@ class ApiClient
*/
public function callApi($resourcePath, $method, $queryParams, $postData, $headerParams, $responseType = null, $endpointPath = null)
{

$headers = array();
$headers = [];

// construct the http header
$headerParams = array_merge(
Expand All @@ -144,17 +142,17 @@ class ApiClient
}

// form data
if ($postData and in_array('Content-Type: application/x-www-form-urlencoded', $headers)) {
if ($postData and in_array('Content-Type: application/x-www-form-urlencoded', $headers, true)) {
$postData = http_build_query($postData);
} elseif ((is_object($postData) or is_array($postData)) and !in_array('Content-Type: multipart/form-data', $headers)) { // json model
} elseif ((is_object($postData) or is_array($postData)) and !in_array('Content-Type: multipart/form-data', $headers, true)) { // json model
$postData = json_encode(\{{invokerPackage}}\ObjectSerializer::sanitizeForSerialization($postData));
}

$url = $this->config->getHost() . $resourcePath;

$curl = curl_init();
// set timeout, if needed
if ($this->config->getCurlTimeout() != 0) {
if ($this->config->getCurlTimeout() !== 0) {
curl_setopt($curl, CURLOPT_TIMEOUT, $this->config->getCurlTimeout());
}
// return the result on success, rather than just true
Expand All @@ -163,7 +161,7 @@ class ApiClient
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

// disable SSL verification, if needed
if ($this->config->getSSLVerification() == false) {
if ($this->config->getSSLVerification() === false) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
}
Expand All @@ -172,24 +170,24 @@ class ApiClient
$url = ($url . '?' . http_build_query($queryParams));
}

if ($method == self::$POST) {
if ($method === self::$POST) {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
} elseif ($method == self::$HEAD) {
} elseif ($method === self::$HEAD) {
curl_setopt($curl, CURLOPT_NOBODY, true);
} elseif ($method == self::$OPTIONS) {
} elseif ($method === self::$OPTIONS) {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "OPTIONS");
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
} elseif ($method == self::$PATCH) {
} elseif ($method === self::$PATCH) {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
} elseif ($method == self::$PUT) {
} elseif ($method === self::$PUT) {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
} elseif ($method == self::$DELETE) {
} elseif ($method === self::$DELETE) {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
} elseif ($method != self::$GET) {
} elseif ($method !== self::$GET) {
throw new ApiException('Method ' . $method . ' is not recognized.');
}
curl_setopt($curl, CURLOPT_URL, $url);
Expand Down Expand Up @@ -223,7 +221,7 @@ class ApiClient
}

// Handle the response
if ($response_info['http_code'] == 0) {
if ($response_info['http_code'] === 0) {
$curl_error_message = curl_error($curl);

// curl_exec can sometimes fail but still return a blank message from curl_error().
Expand All @@ -239,8 +237,8 @@ class ApiClient
throw $exception;
} 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);
if ($responseType === '\SplFileObject' || $responseType === 'string') {
return [$http_body, $response_info['http_code'], $http_header];
}

$data = json_decode($http_body);
Expand All @@ -260,7 +258,7 @@ class ApiClient
$data
);
}
return array($data, $response_info['http_code'], $http_header);
return [$data, $response_info['http_code'], $http_header];
}

/**
Expand Down Expand Up @@ -309,7 +307,7 @@ class ApiClient
protected function httpParseHeaders($raw_headers)
{
// ref/credit: http://php.net/manual/en/function.http-parse-headers.php#112986
$headers = array();
$headers = [];
$key = '';

foreach (explode("\n", $raw_headers) as $h) {
Expand All @@ -319,14 +317,14 @@ class ApiClient
if (!isset($headers[$h[0]])) {
$headers[$h[0]] = trim($h[1]);
} elseif (is_array($headers[$h[0]])) {
$headers[$h[0]] = array_merge($headers[$h[0]], array(trim($h[1])));
$headers[$h[0]] = array_merge($headers[$h[0]], [trim($h[1])]);
} else {
$headers[$h[0]] = array_merge(array($headers[$h[0]]), array(trim($h[1])));
$headers[$h[0]] = array_merge([$headers[$h[0]]], [trim($h[1])]);
}

$key = $h[0];
} else {
if (substr($h[0], 0, 1) == "\t") {
if (substr($h[0], 0, 1) === "\t") {
$headers[$key] .= "\r\n\t".trim($h[0]);
} elseif (!$key) {
$headers[0] = trim($h[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ namespace {{invokerPackage}};
*/
class ObjectSerializer
{

/**
* Serialize data
*
Expand All @@ -51,7 +50,7 @@ class ObjectSerializer
}
return $data;
} elseif (is_object($data)) {
$values = array();
$values = [];
foreach (array_keys($data::swaggerTypes()) as $property) {
$getter = $data::getters()[$property];
if ($data->$getter() !== null) {
Expand Down Expand Up @@ -213,7 +212,7 @@ class ObjectSerializer
return null;
} elseif (substr($class, 0, 4) === 'map[') { // for associative array e.g. map[string,int]
$inner = substr($class, 4, -1);
$deserialized = array();
$deserialized = [];
if (strrpos($inner, ",") !== false) {
$subClass_array = explode(',', $inner, 2);
$subClass = $subClass_array[1];
Expand All @@ -222,9 +221,9 @@ class ObjectSerializer
}
}
return $deserialized;
} elseif (strcasecmp(substr($class, -2), '[]') == 0) {
} elseif (strcasecmp(substr($class, -2), '[]') === 0) {
$subClass = substr($class, 0, -2);
$values = array();
$values = [];
foreach ($data as $key => $value) {
$values[] = self::deserialize($value, $subClass, null, $discriminator);
}
Expand All @@ -244,7 +243,7 @@ class ObjectSerializer
} else {
return null;
}
} elseif (in_array($class, array({{&primitives}}))) {
} elseif (in_array($class, [{{&primitives}}], true)) {
settype($data, $class);
return $data;
} elseif ($class === '\SplFileObject') {
Expand All @@ -257,7 +256,6 @@ class ObjectSerializer
}
$deserialized = new \SplFileObject($filename, "w");
$byte_written = $deserialized->fwrite($data);

if (Configuration::getDefaultConfiguration()->getDebug()) {
error_log("[DEBUG] Written $byte_written byte to $filename. Please move the file to a proper folder or delete the temp file after processing.".PHP_EOL, 3, Configuration::getDefaultConfiguration()->getDebugFile());
}
Expand Down
30 changes: 14 additions & 16 deletions modules/swagger-codegen/src/main/resources/php/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

namespace {{apiPackage}};

use \{{invokerPackage}}\Configuration;
use \{{invokerPackage}}\ApiClient;
use \{{invokerPackage}}\ApiException;
use \{{invokerPackage}}\Configuration;
use \{{invokerPackage}}\ObjectSerializer;

/**
Expand All @@ -35,7 +35,6 @@ use \{{invokerPackage}}\ObjectSerializer;
*/
{{#operations}}class {{classname}}
{

/**
* API Client
*
Expand All @@ -50,7 +49,7 @@ use \{{invokerPackage}}\ObjectSerializer;
*/
public function __construct(\{{invokerPackage}}\ApiClient $apiClient = null)
{
if ($apiClient == null) {
if ($apiClient === null) {
$apiClient = new ApiClient();
$apiClient->getConfig()->setHost('{{basePath}}');
}
Expand Down Expand Up @@ -80,8 +79,8 @@ use \{{invokerPackage}}\ObjectSerializer;
$this->apiClient = $apiClient;
return $this;
}

{{#operation}}

/**
* Operation {{{operationId}}}
*
Expand All @@ -94,8 +93,8 @@ use \{{invokerPackage}}\ObjectSerializer;
{{#allParams}}
* @param {{dataType}} ${{paramName}} {{description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}}
* @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}
* @throws \{{invokerPackage}}\ApiException on non-2xx response
* @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}
*/
public function {{operationId}}({{#allParams}}${{paramName}}{{^required}} = null{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
{
Expand All @@ -114,9 +113,9 @@ use \{{invokerPackage}}\ObjectSerializer;
{{/description}}
{{#allParams}}
* @param {{dataType}} ${{paramName}} {{description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
{{/allParams}}
* @return array of {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}null{{/returnType}}, HTTP status code, HTTP response headers (array of strings)
{{/allParams}}
* @throws \{{invokerPackage}}\ApiException on non-2xx response
* @return array of {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}null{{/returnType}}, HTTP status code, HTTP response headers (array of strings)
*/
public function {{operationId}}WithHttpInfo({{#allParams}}${{paramName}}{{^required}} = null{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
{
Expand Down Expand Up @@ -169,14 +168,14 @@ use \{{invokerPackage}}\ObjectSerializer;
// parse inputs
$resourcePath = "{{path}}";
$httpBody = '';
$queryParams = array();
$headerParams = array();
$formParams = array();
$_header_accept = $this->apiClient->selectHeaderAccept(array({{#produces}}'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}{{/produces}}));
$queryParams = [];
$headerParams = [];
$formParams = [];
$_header_accept = $this->apiClient->selectHeaderAccept([{{#produces}}'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}{{/produces}}]);
if (!is_null($_header_accept)) {
$headerParams['Accept'] = $_header_accept;
}
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType(array({{#consumes}}'{{{mediaType}}}'{{#hasMore}},{{/hasMore}}{{/consumes}}));
$headerParams['Content-Type'] = $this->apiClient->selectHeaderContentType([{{#consumes}}'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}{{/consumes}}]);

{{#queryParams}}
// query params
Expand Down Expand Up @@ -287,10 +286,10 @@ use \{{invokerPackage}}\ObjectSerializer;
);

{{#returnType}}
return array($this->apiClient->getSerializer()->deserialize($response, '{{returnType}}', $httpHeader{{#discriminator}}, '{{discriminator}}'{{/discriminator}}), $statusCode, $httpHeader);
return [$this->apiClient->getSerializer()->deserialize($response, '{{returnType}}', $httpHeader{{#discriminator}}, '{{discriminator}}'{{/discriminator}}), $statusCode, $httpHeader];
{{/returnType}}
{{^returnType}}
return array(null, $statusCode, $httpHeader);
return [null, $statusCode, $httpHeader];
{{/returnType}}
} catch (ApiException $e) {
switch ($e->getCode()) {
Expand All @@ -307,7 +306,6 @@ use \{{invokerPackage}}\ObjectSerializer;
throw $e;
}
}

{{/operation}}
}
{{/operations}}
{{/operations}}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"require-dev": {
"phpunit/phpunit": "~4.8",
"satooshi/php-coveralls": "~1.0",
"squizlabs/php_codesniffer": "~2.6"
"squizlabs/php_codesniffer": "~2.6",
"friendsofphp/php-cs-fixer": "~1.12"
},
"autoload": {
"psr-4": { "{{escapedInvokerPackage}}\\" : "{{srcBasePath}}/" }
Expand Down
Loading