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

View File

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