[PHP] Better PSR2 compatibility (#3863)

* feature(php-cs-fixer) add php-cs-fixer support

* feature(php-cs-fixer) tweak Mustache templates to fit PSR2

* feature(php-cs-fixer) bin/php-petstore.sh output
This commit is contained in:
Dalibor Karlović
2016-09-27 02:23:44 +02:00
committed by wing328
parent 0f25501746
commit 70fa2fb78e
48 changed files with 1079 additions and 1062 deletions

View File

@@ -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__)
);

View File

@@ -31,7 +31,6 @@ namespace {{invokerPackage}};
*/
class ApiClient
{
public static $PATCH = "PATCH";
public static $POST = "POST";
public static $GET = "GET";
@@ -61,7 +60,7 @@ class ApiClient
*/
public function __construct(\{{invokerPackage}}\Configuration $config = null)
{
if ($config == null) {
if ($config === null) {
$config = Configuration::getDefaultConfiguration();
}
@@ -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(
@@ -144,9 +142,9 @@ 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));
}
@@ -154,7 +152,7 @@ class ApiClient
$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
@@ -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);
}
@@ -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);
@@ -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().
@@ -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);
@@ -260,7 +258,7 @@ class ApiClient
$data
);
}
return array($data, $response_info['http_code'], $http_header);
return [$data, $response_info['http_code'], $http_header];
}
/**
@@ -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) {
@@ -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]);

View File

@@ -31,7 +31,6 @@ namespace {{invokerPackage}};
*/
class ObjectSerializer
{
/**
* Serialize data
*
@@ -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) {
@@ -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];
@@ -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);
}
@@ -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') {
@@ -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());
}

View File

@@ -19,9 +19,9 @@
namespace {{apiPackage}};
use \{{invokerPackage}}\Configuration;
use \{{invokerPackage}}\ApiClient;
use \{{invokerPackage}}\ApiException;
use \{{invokerPackage}}\Configuration;
use \{{invokerPackage}}\ObjectSerializer;
/**
@@ -35,7 +35,6 @@ use \{{invokerPackage}}\ObjectSerializer;
*/
{{#operations}}class {{classname}}
{
/**
* API Client
*
@@ -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}}');
}
@@ -80,8 +79,8 @@ use \{{invokerPackage}}\ObjectSerializer;
$this->apiClient = $apiClient;
return $this;
}
{{#operation}}
/**
* Operation {{{operationId}}}
*
@@ -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}})
{
@@ -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}})
{
@@ -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
@@ -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()) {
@@ -307,7 +306,6 @@ use \{{invokerPackage}}\ObjectSerializer;
throw $e;
}
}
{{/operation}}
}
{{/operations}}
{{/operations}}

View File

@@ -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}}/" }

View File

@@ -31,7 +31,6 @@ namespace {{invokerPackage}};
*/
class Configuration
{
private static $defaultConfiguration = null;
/**
@@ -39,14 +38,14 @@ class Configuration
*
* @var string[]
*/
protected $apiKeys = array();
protected $apiKeys = [];
/**
* Associate array to store API prefix (e.g. Bearer)
*
* @var string[]
*/
protected $apiKeyPrefixes = array();
protected $apiKeyPrefixes = [];
/**
* Access token for OAuth
@@ -74,7 +73,7 @@ class Configuration
*
* @var array
*/
protected $defaultHeaders = array();
protected $defaultHeaders = [];
/**
* The host
@@ -472,7 +471,7 @@ class Configuration
*/
public static function getDefaultConfiguration()
{
if (self::$defaultConfiguration == null) {
if (self::$defaultConfiguration === null) {
self::$defaultConfiguration = new Configuration();
}

View File

@@ -31,13 +31,11 @@ use \ArrayAccess;
{{#description}}
// @description {{description}}
{{/description}}
/**
/**
* @package {{invokerPackage}}
* @author http://github.com/swagger-api/swagger-codegen
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2
* @link https://github.com/swagger-api/swagger-codegen
*/
{{#isEnum}}{{>model_enum}}{{/isEnum}}{{^isEnum}}{{>model_generic}}{{/isEnum}}
{{/model}}
{{/models}}
{{/model}}{{/models}}

View File

@@ -15,4 +15,4 @@ class {{classname}} {
];
}
{{/isEnum}}{{/vars}}
}
}

View File

@@ -10,10 +10,10 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
* Array of property to type mappings. Used for (de)serialization
* @var string[]
*/
protected static $swaggerTypes = array(
protected static $swaggerTypes = [
{{#vars}}'{{name}}' => '{{{datatype}}}'{{#hasMore}},
{{/hasMore}}{{/vars}}
);
];
public static function swaggerTypes()
{
@@ -24,39 +24,41 @@ 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[]
*/
protected static $attributeMap = array(
protected static $attributeMap = [
{{#vars}}'{{name}}' => '{{baseName}}'{{#hasMore}},
{{/hasMore}}{{/vars}}
);
];
/**
* Array of attributes to setter functions (for deserialization of responses)
* @var string[]
*/
protected static $setters = [
{{#vars}}'{{name}}' => '{{setter}}'{{#hasMore}},
{{/hasMore}}{{/vars}}
];
/**
* Array of attributes to getter functions (for serialization of requests)
* @var string[]
*/
protected static $getters = [
{{#vars}}'{{name}}' => '{{getter}}'{{#hasMore}},
{{/hasMore}}{{/vars}}
];
public static function attributeMap()
{
return {{#parentSchema}}parent::attributeMap() + {{/parentSchema}}self::$attributeMap;
}
/**
* Array of attributes to setter functions (for deserialization of responses)
* @var string[]
*/
protected static $setters = array(
{{#vars}}'{{name}}' => '{{setter}}'{{#hasMore}},
{{/hasMore}}{{/vars}}
);
public static function setters()
{
return {{#parentSchema}}parent::setters() + {{/parentSchema}}self::$setters;
}
/**
* Array of attributes to getter functions (for serialization of requests)
* @var string[]
*/
protected static $getters = array(
{{#vars}}'{{name}}' => '{{getter}}'{{#hasMore}},
{{/hasMore}}{{/vars}}
);
public static function getters()
{
return {{#parentSchema}}parent::getters() + {{/parentSchema}}self::$getters;
@@ -83,7 +85,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
* Associative array for storing property values
* @var mixed[]
*/
protected $container = array();
protected $container = [];
/**
* Constructor
@@ -113,7 +115,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
*/
public function listInvalidProperties()
{
$invalid_properties = array();
$invalid_properties = [];
{{#vars}}
{{#required}}
if ($this->container['{{name}}'] === null) {
@@ -122,7 +124,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
{{/required}}
{{#isEnum}}
{{^isContainer}}
$allowed_values = array({{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}});
$allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}];
if (!in_array($this->container['{{name}}'], $allowed_values)) {
$invalid_properties[] = "invalid value for '{{name}}', must be one of #{allowed_values}.";
}
@@ -193,7 +195,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
{{/required}}
{{#isEnum}}
{{^isContainer}}
$allowed_values = array({{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}});
$allowed_values = [{{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}];
if (!in_array($this->container['{{name}}'], $allowed_values)) {
return false;
}
@@ -368,4 +370,4 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
return json_encode(\{{invokerPackage}}\ObjectSerializer::sanitizeForSerialization($this));
}
}
}