Skip to content
Open
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
15 changes: 5 additions & 10 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -807,18 +807,21 @@
<file src="src/Internal/Marshaller/Type/ArrayType.php">
<MoreSpecificImplementedParamType>
<code><![CDATA[$current]]></code>
<code><![CDATA[$value]]></code>
</MoreSpecificImplementedParamType>
</file>
<file src="src/Internal/Marshaller/Type/DateIntervalType.php">
<ArgumentTypeCoercion>
<code><![CDATA[$this->format]]></code>
<code><![CDATA[$this->format]]></code>
</ArgumentTypeCoercion>
<DeprecatedConstant>
<code><![CDATA[DateInterval::FORMAT_MONTHS]]></code>
<code><![CDATA[DateInterval::FORMAT_YEARS]]></code>
</DeprecatedConstant>
<InvalidOperand>
<code><![CDATA[$interval->totalMicroseconds * 1000]]></code>
<code><![CDATA[$value * 1000000000]]></code>
<code><![CDATA[($value * 1000000000) % 1000000000]]></code>
<code><![CDATA[DateInterval::parse($value, $this->format)->totalMicroseconds * 1000]]></code>
</InvalidOperand>
</file>
<file src="src/Internal/Marshaller/Type/DurationJsonType.php">
Expand All @@ -828,9 +831,6 @@
</InvalidOperand>
</file>
<file src="src/Internal/Marshaller/Type/EnumType.php">
<PropertyTypeCoercion>
<code><![CDATA[$class]]></code>
</PropertyTypeCoercion>
<UndefinedMethod>
<code><![CDATA[$this->classFQCN::from($value)]]></code>
<code><![CDATA[$this->classFQCN::from($value['value'])]]></code>
Expand All @@ -849,11 +849,6 @@
<code><![CDATA[TClass]]></code>
</InvalidReturnType>
</file>
<file src="src/Internal/Marshaller/Type/OneOfType.php">
<InvalidReturnType>
<code><![CDATA[array]]></code>
</InvalidReturnType>
</file>
<file src="src/Internal/Marshaller/Type/Type.php">
<ArgumentTypeCoercion>
<code><![CDATA[$typeClass]]></code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Converts a boolean value to an activity cancellation policy.
*
* @see Policy
* @extends Type<bool>
* @extends Type<bool, int>
* @internal
*/
final class ActivityCancellationType extends Type
Expand Down
3 changes: 1 addition & 2 deletions src/Internal/Marshaller/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Temporal\Internal\Marshaller\MarshallingRule;

/**
* @extends Type<array>
* @extends Type<array, array>
*/
class ArrayType extends Type implements DetectableTypeInterface, RuleFactoryInterface
{
Expand Down Expand Up @@ -57,7 +57,6 @@ public static function makeRule(\ReflectionProperty $property): ?MarshallingRule
}

/**
* @psalm-assert array $value
* @param mixed $value
* @param array $current
*/
Expand Down
16 changes: 6 additions & 10 deletions src/Internal/Marshaller/Type/AssocArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Force the value to be an associative array (object) on serialization.
*
* @extends Type<object>
* @extends Type<object, mixed>
*/
class AssocArrayType extends Type
{
Expand All @@ -40,17 +40,13 @@ public function __construct(MarshallerInterface $marshaller, MarshallingRule|str
parent::__construct($marshaller);
}

/**
* @psalm-assert array $value
* @psalm-assert array $current
* @param mixed $value
* @param mixed $current
*/
public function parse($value, $current): array
{
\is_array($value) or throw new \InvalidArgumentException(
\sprintf(self::ERROR_INVALID_TYPE, \get_debug_type($value)),
);
if (!\is_array($value)) {
throw new \InvalidArgumentException(
\sprintf(self::ERROR_INVALID_TYPE, \get_debug_type($value)),
);
}

if ($this->type) {
$result = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* @see Policy
*
* @extends Type<bool>
* @extends Type<bool, int>
* @internal
*/
final class ChildWorkflowCancellationType extends Type
Expand Down
3 changes: 2 additions & 1 deletion src/Internal/Marshaller/Type/CronType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
namespace Temporal\Internal\Marshaller\Type;

/**
* @extends Type<string>
* @psalm-type TValue = null|string|\Stringable
* @extends Type<string, TValue>
*/
class CronType extends Type
{
Expand Down
30 changes: 23 additions & 7 deletions src/Internal/Marshaller/Type/DateIntervalType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

/**
* @psalm-import-type DateIntervalFormat from DateInterval
* @extends Type<int|Duration>
* @psalm-import-type DateIntervalValue from DateInterval
* @extends Type<int|Duration, DateIntervalValue>
*/
class DateIntervalType extends Type implements DetectableTypeInterface, RuleFactoryInterface
{
Expand Down Expand Up @@ -53,10 +54,6 @@ public static function makeRule(\ReflectionProperty $property): ?MarshallingRule

public function serialize($value): int|Duration
{
if ($this->format === DateInterval::FORMAT_NANOSECONDS) {
return (int) (DateInterval::parse($value, $this->format)->totalMicroseconds * 1000);
}

if ($this->format === Duration::class) {
return match (true) {
$value instanceof \DateInterval => DateInterval::toDuration($value),
Expand All @@ -69,8 +66,27 @@ public function serialize($value): int|Duration
};
}

$method = 'total' . \ucfirst($this->format);
return (int) (DateInterval::parse($value, $this->format)->$method);
$interval = DateInterval::parse($value, $this->format);

return (int) match ($this->format) {
DateInterval::FORMAT_YEARS => $interval->totalYears,
DateInterval::FORMAT_MONTHS => $interval->totalMonths,
DateInterval::FORMAT_WEEKS => $interval->totalWeeks,
DateInterval::FORMAT_DAYS => $interval->totalDays,
DateInterval::FORMAT_HOURS => $interval->totalHours,
DateInterval::FORMAT_MINUTES => $interval->totalMinutes,
DateInterval::FORMAT_SECONDS => $interval->totalSeconds,
DateInterval::FORMAT_MILLISECONDS => $interval->totalMilliseconds,
DateInterval::FORMAT_MICROSECONDS => $interval->totalMicroseconds,
DateInterval::FORMAT_NANOSECONDS => (int) \round($interval->totalMicroseconds * 1000),
default => throw new \InvalidArgumentException(
\sprintf(
'Unsupported format: "%s". See %s for available formats.',
$this->format,
DateInterval::class,
),
),
};
}

public function parse($value, $current): CarbonInterval
Expand Down
2 changes: 1 addition & 1 deletion src/Internal/Marshaller/Type/DateTimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Temporal\Internal\Support\Inheritance;

/**
* @extends Type<Timestamp|non-empty-string>
* @extends Type<Timestamp|string, mixed>
*/
class DateTimeType extends Type implements DetectableTypeInterface, RuleFactoryInterface
{
Expand Down
3 changes: 2 additions & 1 deletion src/Internal/Marshaller/Type/DurationJsonType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

/**
* @psalm-import-type DateIntervalFormat from DateInterval
* @extends Type<int|Duration>
* @psalm-import-type DateIntervalValue from DateInterval
* @extends Type<int|Duration, DateIntervalValue>
*/
class DurationJsonType extends Type implements DetectableTypeInterface, RuleFactoryInterface
{
Expand Down
5 changes: 1 addition & 4 deletions src/Internal/Marshaller/Type/EncodedCollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/**
* Read only type.
* @extends Type<array|Message>
* @extends Type<array|Message, EncodedCollection>
*/
final class EncodedCollectionType extends Type implements DetectableTypeInterface, RuleFactoryInterface
{
Expand Down Expand Up @@ -52,9 +52,6 @@ public static function makeRule(\ReflectionProperty $property): ?MarshallingRule
return new MarshallingRule($property->getName(), self::class, $type->getName());
}

/**
* @psalm-assert string $value
*/
public function parse(mixed $value, mixed $current): EncodedCollection
{
return match (true) {
Expand Down
14 changes: 7 additions & 7 deletions src/Internal/Marshaller/Type/EnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@
use Temporal\Internal\Marshaller\MarshallingRule;

/**
* @extends Type<array>
* @template TClass of \UnitEnum
* @extends Type<array, TClass>
*/
class EnumType extends Type implements RuleFactoryInterface
{
private const ERROR_INVALID_TYPE = 'Invalid Enum value. Expected: int or string scalar value for BackedEnum; '
. 'array with `name` or `value` keys; a case of the Enum. %s given.';

/** @var class-string<\UnitEnum> */
/** @var class-string<TClass> */
private string $classFQCN;

/**
* @param class-string<TClass>|null $class
*/
public function __construct(MarshallerInterface $marshaller, ?string $class = null)
{
if ($class === null) {
Expand Down Expand Up @@ -54,7 +58,7 @@ public static function makeRule(\ReflectionProperty $property): ?MarshallingRule

public function parse($value, $current)
{
if (\is_object($value)) {
if ($value instanceof $this->classFQCN) {
return $value;
}

Expand All @@ -78,10 +82,6 @@ public function parse($value, $current)
throw new \InvalidArgumentException(\sprintf(self::ERROR_INVALID_TYPE, \ucfirst(\get_debug_type($value))));
}

/**
* @psalm-suppress UndefinedDocblockClass
* @param mixed $value
*/
public function serialize($value): array
{
return $value instanceof \BackedEnum
Expand Down
4 changes: 2 additions & 2 deletions src/Internal/Marshaller/Type/EnumValueType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Temporal\Internal\Marshaller\MarshallingRule;

/**
* @extends Type<int|string>
* @extends Type<int|string, \BackedEnum>
*/
class EnumValueType extends Type implements RuleFactoryInterface
{
Expand Down Expand Up @@ -55,7 +55,7 @@ public static function makeRule(\ReflectionProperty $property): ?MarshallingRule

public function parse($value, $current)
{
if (\is_object($value)) {
if ($value instanceof \BackedEnum) {
return $value;
}

Expand Down
7 changes: 1 addition & 6 deletions src/Internal/Marshaller/Type/NullableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Temporal\Internal\Marshaller\MarshallingRule;

/**
* @extends Type<mixed>
* @extends Type<mixed, mixed>
*/
class NullableType extends Type
{
Expand All @@ -33,11 +33,6 @@ public function __construct(MarshallerInterface $marshaller, MarshallingRule|str
parent::__construct($marshaller);
}

/**
* @param mixed $value
* @param mixed $current
* @return mixed
*/
public function parse($value, $current)
{
if ($value === null) {
Expand Down
8 changes: 3 additions & 5 deletions src/Internal/Marshaller/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

/**
* @template TClass of object
* @extends Type<array>
* @extends Type<array, mixed>
*/
class ObjectType extends Type implements DetectableTypeInterface, RuleFactoryInterface
{
Expand Down Expand Up @@ -60,13 +60,11 @@ public static function makeRule(\ReflectionProperty $property): ?MarshallingRule

public function parse($value, $current): object
{
if (\is_object($value)) {
if (\is_object($value) && $this->reflection->isInstance($value)) {
return $value;
}

if ($current === null) {
$current = $this->emptyInstance();
}
$current ??= $this->emptyInstance();

if ($current::class === \stdClass::class && $this->reflection->getName() === \stdClass::class) {
foreach ($value as $key => $val) {
Expand Down
29 changes: 19 additions & 10 deletions src/Internal/Marshaller/Type/OneOfType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

/**
* @template TClass of object
* @extends Type<array>
* @extends Type<array, mixed>
*/
class OneOfType extends Type
{
Expand All @@ -34,7 +34,7 @@ public function __construct(

public function parse(mixed $value, mixed $current): ?object
{
if (\is_object($value)) {
if ($this->parentClass !== null && $value instanceof $this->parentClass) {
return $value;
}

Expand All @@ -58,10 +58,12 @@ public function parse(mixed $value, mixed $current): ?object
}

if ($dtoClass === null) {
$this->nullable or throw new \InvalidArgumentException(\sprintf(
'Unable to detect OneOf case for non-nullable type%s.',
$this->parentClass ? " `{$this->parentClass}`" : '',
));
if (!$this->nullable) {
throw new \InvalidArgumentException(\sprintf(
'Unable to detect OneOf case for non-nullable type%s.',
$this->parentClass ? " `{$this->parentClass}`" : '',
));
}

return null;
}
Expand All @@ -87,16 +89,23 @@ public function serialize(mixed $value): array
return [];
}

\is_object($value) or throw new \InvalidArgumentException(\sprintf(
'Passed value must be a type of object, but %s given.',
\get_debug_type($value),
));
if (!\is_object($value)) {
throw new \InvalidArgumentException(\sprintf(
'Passed value must be a type of object, but %s given.',
\get_debug_type($value),
));
}

foreach ($this->cases as $field => $class) {
if ($value::class === $class) {
return [$field => $this->marshaller->marshal($value)];
}
}

throw new \InvalidArgumentException(\sprintf(
'Passed value must be a type of one of the allowed classes, but %s given.',
\get_debug_type($value),
));
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Internal/Marshaller/Type/RuleFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@

/**
* The type can detect the property type information from its reflection.
*
* @extends TypeInterface<mixed>
*/
interface RuleFactoryInterface extends TypeInterface
interface RuleFactoryInterface
{
/**
* Make a marshalling rule for the given property.
Expand Down
Loading
Loading