Small tweaks for php generator, PHPStan level 3 (#7616)

This commit is contained in:
Dalibor Karlović 2020-10-21 09:38:10 +02:00 committed by GitHub
parent ec74b06d2c
commit 240c046f35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 16 deletions

View File

@ -77,13 +77,17 @@ class ObjectSerializer
foreach ($data::openAPITypes() as $property => $openAPIType) {
$getter = $data::getters()[$property];
$value = $data->$getter();
if ($value !== null
&& !in_array($openAPIType, [{{&primitives}}], true)
&& method_exists($openAPIType, 'getAllowableEnumValues')
&& !in_array($value, $openAPIType::getAllowableEnumValues(), true)) {
$imploded = implode("', '", $openAPIType::getAllowableEnumValues());
if ($value !== null && !in_array($openAPIType, [{{&primitives}}], true)) {
$callable = [$openAPIType, 'getAllowableEnumValues'];
if (is_callable($callable)) {
/** array $callable */
$allowedEnumTypes = $callable();
if (!in_array($value, $allowedEnumTypes, true)) {
$imploded = implode("', '", $allowedEnumTypes);
throw new \InvalidArgumentException("Invalid value for enum '$openAPIType', must be one of: '$imploded'");
}
}
}
if ($value !== null) {
$values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value, $openAPIType, $formats[$property]);
}
@ -159,8 +163,9 @@ class ObjectSerializer
*/
public static function toHeaderValue($value)
{
if (method_exists($value, 'toHeaderValue')) {
return $value->toHeaderValue();
$callable = [$value, 'toHeaderValue'];
if (is_callable($callable)) {
return $callable();
}
return self::toString($value);
@ -307,6 +312,7 @@ class ObjectSerializer
}
}
/** @psalm-suppress ParadoxicalCondition */
if (in_array($class, [{{&primitives}}], true)) {
settype($data, $class);
return $data;
@ -346,6 +352,8 @@ class ObjectSerializer
$class = $subclass;
}
}
/** @var ModelInterface $instance */
$instance = new $class();
foreach ($instance::openAPITypes() as $property => $type) {
$propertySetter = $instance::setters()[$property];

View File

@ -87,13 +87,17 @@ class ObjectSerializer
foreach ($data::openAPITypes() as $property => $openAPIType) {
$getter = $data::getters()[$property];
$value = $data->$getter();
if ($value !== null
&& !in_array($openAPIType, ['DateTime', 'bool', 'boolean', 'byte', 'double', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)
&& method_exists($openAPIType, 'getAllowableEnumValues')
&& !in_array($value, $openAPIType::getAllowableEnumValues(), true)) {
$imploded = implode("', '", $openAPIType::getAllowableEnumValues());
if ($value !== null && !in_array($openAPIType, ['DateTime', 'bool', 'boolean', 'byte', 'double', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) {
$callable = [$openAPIType, 'getAllowableEnumValues'];
if (is_callable($callable)) {
/** array $callable */
$allowedEnumTypes = $callable();
if (!in_array($value, $allowedEnumTypes, true)) {
$imploded = implode("', '", $allowedEnumTypes);
throw new \InvalidArgumentException("Invalid value for enum '$openAPIType', must be one of: '$imploded'");
}
}
}
if ($value !== null) {
$values[$data::attributeMap()[$property]] = self::sanitizeForSerialization($value, $openAPIType, $formats[$property]);
}
@ -169,8 +173,9 @@ class ObjectSerializer
*/
public static function toHeaderValue($value)
{
if (method_exists($value, 'toHeaderValue')) {
return $value->toHeaderValue();
$callable = [$value, 'toHeaderValue'];
if (is_callable($callable)) {
return $callable();
}
return self::toString($value);
@ -317,6 +322,7 @@ class ObjectSerializer
}
}
/** @psalm-suppress ParadoxicalCondition */
if (in_array($class, ['DateTime', 'bool', 'boolean', 'byte', 'double', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) {
settype($data, $class);
return $data;
@ -356,6 +362,8 @@ class ObjectSerializer
$class = $subclass;
}
}
/** @var ModelInterface $instance */
$instance = new $class();
foreach ($instance::openAPITypes() as $property => $type) {
$propertySetter = $instance::setters()[$property];