-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tweaks for php generator, based on issues reported by PHP static analysis tools (PHPStan) #7376
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,9 @@ namespace {{invokerPackage}}; | |
*/ | ||
class Configuration | ||
{ | ||
/** | ||
* @var Configuration | ||
*/ | ||
private static $defaultConfiguration; | ||
|
||
/** | ||
|
@@ -128,7 +131,7 @@ class Configuration | |
* | ||
* @param string $apiKeyIdentifier API key identifier (authentication scheme) | ||
* | ||
* @return string API key or token | ||
* @return null|string API key or token | ||
*/ | ||
public function getApiKey($apiKeyIdentifier) | ||
{ | ||
|
@@ -154,7 +157,7 @@ class Configuration | |
* | ||
* @param string $apiKeyIdentifier API key identifier (authentication scheme) | ||
* | ||
* @return string | ||
* @return null|string | ||
*/ | ||
public function getApiKeyPrefix($apiKeyIdentifier) | ||
{ | ||
|
@@ -400,7 +403,7 @@ class Configuration | |
* | ||
* @param string $apiKeyIdentifier name of apikey | ||
* | ||
* @return string API key with the prefix | ||
* @return null|string API key with the prefix | ||
*/ | ||
public function getApiKeyWithPrefix($apiKeyIdentifier) | ||
{ | ||
|
@@ -423,7 +426,7 @@ class Configuration | |
/** | ||
* Returns an array of host settings | ||
* | ||
* @return an array of host settings | ||
* @return array an array of host settings | ||
*/ | ||
public function getHostSettings() | ||
{ | ||
|
@@ -461,9 +464,9 @@ class Configuration | |
/** | ||
* Returns URL based on the index and variables | ||
* | ||
* @param index array index of the host settings | ||
* @param variables hash of variable and the corresponding value (optional) | ||
* @return URL based on host settings | ||
* @param int $index index of the host settings | ||
* @param array|null $variables hash of variable and the corresponding value (optional) | ||
* @return string URL based on host settings | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type completely missing here. |
||
*/ | ||
public function getHostFromSettings($index, $variables = null) | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,20 +51,26 @@ class ObjectSerializer | |
* @param string $type the OpenAPIToolsType of the data | ||
* @param string $format the format of the OpenAPITools type of the data | ||
* | ||
* @return string|object serialized form of $data | ||
* @return scalar|object|array|null serialized form of $data | ||
*/ | ||
public static function sanitizeForSerialization($data, $type = null, $format = null) | ||
{ | ||
if (is_scalar($data) || null === $data) { | ||
return $data; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code simplified since it's returning, no need for |
||
} elseif ($data instanceof \DateTime) { | ||
} | ||
|
||
if ($data instanceof \DateTime) { | ||
return ($format === 'date') ? $data->format('Y-m-d') : $data->format(self::$dateTimeFormat); | ||
} elseif (is_array($data)) { | ||
} | ||
|
||
if (is_array($data)) { | ||
foreach ($data as $property => $value) { | ||
$data[$property] = self::sanitizeForSerialization($value); | ||
} | ||
return $data; | ||
} elseif (is_object($data)) { | ||
} | ||
|
||
if (is_object($data)) { | ||
$values = []; | ||
if ($data instanceof ModelInterface) { | ||
$formats = $data::openAPIFormats(); | ||
|
@@ -250,7 +256,9 @@ class ObjectSerializer | |
{ | ||
if (null === $data) { | ||
return null; | ||
} elseif (strcasecmp(substr($class, -2), '[]') === 0) { | ||
} | ||
|
||
if (strcasecmp(substr($class, -2), '[]') === 0) { | ||
$data = is_string($data) ? json_decode($data) : $data; | ||
|
||
if (!is_array($data)) { | ||
|
@@ -263,7 +271,9 @@ class ObjectSerializer | |
$values[] = self::deserialize($value, $subClass, null); | ||
} | ||
return $values; | ||
} elseif (substr($class, 0, 4) === 'map[') { // for associative array e.g. map[string,int] | ||
} | ||
|
||
if (substr($class, 0, 4) === 'map[') { // for associative array e.g. map[string,int] | ||
$data = is_string($data) ? json_decode($data) : $data; | ||
settype($data, 'array'); | ||
$inner = substr($class, 4, -1); | ||
|
@@ -276,10 +286,14 @@ class ObjectSerializer | |
} | ||
} | ||
return $deserialized; | ||
} elseif ($class === 'object') { | ||
} | ||
|
||
if ($class === 'object') { | ||
settype($data, 'array'); | ||
return $data; | ||
} elseif ($class === '\DateTime') { | ||
} | ||
|
||
if ($class === '\DateTime') { | ||
// Some API's return an invalid, empty string as a | ||
// date-time property. DateTime::__construct() will return | ||
// the current time for empty input which is probably not | ||
|
@@ -291,10 +305,14 @@ class ObjectSerializer | |
} else { | ||
return null; | ||
} | ||
} elseif (in_array($class, [{{&primitives}}], true)) { | ||
} | ||
|
||
if (in_array($class, [{{&primitives}}], true)) { | ||
settype($data, $class); | ||
return $data; | ||
} elseif ($class === '\SplFileObject') { | ||
} | ||
|
||
if ($class === '\SplFileObject') { | ||
/** @var \Psr\Http\Message\StreamInterface $data */ | ||
|
||
// determine file name | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,17 +80,17 @@ use {{invokerPackage}}\ObjectSerializer; | |
/** | ||
* Set the host index | ||
* | ||
* @param int Host index (required) | ||
* @param int $hostIndex Host index (required) | ||
*/ | ||
public function setHostIndex($host_index) | ||
public function setHostIndex($hostIndex) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CS change |
||
{ | ||
$this->hostIndex = $host_index; | ||
$this->hostIndex = $hostIndex; | ||
} | ||
|
||
/** | ||
* Get the host index | ||
* | ||
* @return Host index | ||
* @return int Host index | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing type. |
||
*/ | ||
public function getHostIndex() | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
* Please update the test case below to test the endpoint. | ||
*/ | ||
|
||
namespace {{invokerPackage}}; | ||
namespace {{invokerPackage}}\Test\Api; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests were placed in totally wrong namespace. |
||
|
||
use \{{invokerPackage}}\Configuration; | ||
use \{{invokerPackage}}\ApiException; | ||
|
@@ -71,6 +71,8 @@ use PHPUnit\Framework\TestCase; | |
*/ | ||
public function test{{vendorExtensions.x-test-operation-id}}() | ||
{ | ||
// TODO: implement | ||
$this->markTestIncomplete('Not implemented'); | ||
} | ||
{{/operation}} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,6 @@ | |
"psr-4": { "{{escapedInvokerPackage}}\\" : "{{srcBasePath}}/" } | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { "{{escapedInvokerPackage}}\\" : "{{testBasePath}}/" } | ||
"psr-4": { "{{escapedInvokerPackage}}\\Test\\" : "{{testBasePath}}/" } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests placed in the wrong namespace. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,11 @@ use \{{invokerPackage}}\ObjectSerializer; | |
* @package {{invokerPackage}} | ||
* @author OpenAPI Generator team | ||
* @link https://openapi-generator.tech | ||
{{^isEnum}} | ||
* @implements \ArrayAccess<TKey, TValue> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generics. |
||
* @template TKey int|null | ||
* @template TValue mixed|null | ||
{{/isEnum}} | ||
*/ | ||
{{#isEnum}}{{>model_enum}}{{/isEnum}}{{^isEnum}}{{>model_generic}}{{/isEnum}} | ||
{{/model}}{{/models}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type completely missing here.