From fb447960efc8384797bc6cfb8ce810be89ae98ff Mon Sep 17 00:00:00 2001 From: Tom Carrio Date: Sun, 3 Dec 2023 23:22:24 -0500 Subject: [PATCH] fix: updates to support 8.1 and errors --- src/OpenFeatureClient.php | 29 ++++++++----------- .../common/ValueTypeValidator.php | 1 - .../flags/EvaluationDetails.php | 13 ++++----- .../flags/EvaluationDetailsBuilder.php | 5 ++-- .../flags/EvaluationDetailsFactory.php | 5 ++-- src/implementation/flags/NoOpClient.php | 3 +- .../hooks/AbstractHookContext.php | 12 ++++---- .../hooks/HookContextBuilder.php | 5 ++-- .../hooks/HookContextFactory.php | 5 ++-- .../hooks/ImmutableHookContext.php | 5 ++-- .../hooks/MutableHookContext.php | 3 +- src/implementation/provider/Reason.php | 15 ---------- .../provider/ResolutionDetails.php | 13 ++++----- .../provider/ResolutionDetailsBuilder.php | 5 ++-- .../provider/ResolutionDetailsFactory.php | 5 ++-- src/interfaces/common/TypeValuePair.php | 17 ----------- src/interfaces/flags/EvaluationDetails.php | 5 ++-- src/interfaces/hooks/HookContext.php | 5 ++-- src/interfaces/hooks/MutableHookContext.php | 5 ++-- src/interfaces/provider/ResolutionDetails.php | 6 ++-- tests/unit/HookContextBuilderTest.php | 2 +- 21 files changed, 54 insertions(+), 110 deletions(-) delete mode 100644 src/implementation/provider/Reason.php delete mode 100644 src/interfaces/common/TypeValuePair.php diff --git a/src/OpenFeatureClient.php b/src/OpenFeatureClient.php index 4ddefbf..929eb5b 100644 --- a/src/OpenFeatureClient.php +++ b/src/OpenFeatureClient.php @@ -4,7 +4,6 @@ namespace OpenFeature; -use DateTime; use OpenFeature\implementation\common\Metadata; use OpenFeature\implementation\common\ValueTypeValidator; use OpenFeature\implementation\errors\InvalidResolutionValueError; @@ -16,7 +15,6 @@ use OpenFeature\implementation\hooks\HookContextFactory; use OpenFeature\implementation\hooks\HookExecutor; use OpenFeature\implementation\hooks\HookHints; -use OpenFeature\implementation\provider\Reason; use OpenFeature\implementation\provider\ResolutionError; use OpenFeature\interfaces\common\LoggerAwareTrait; use OpenFeature\interfaces\common\Metadata as MetadataInterface; @@ -30,6 +28,7 @@ use OpenFeature\interfaces\hooks\HooksAwareTrait; use OpenFeature\interfaces\provider\ErrorCode; use OpenFeature\interfaces\provider\Provider; +use OpenFeature\interfaces\provider\Reason; use OpenFeature\interfaces\provider\ResolutionDetails; use OpenFeature\interfaces\provider\ThrowableWithResolutionError; use Psr\Log\LoggerAwareInterface; @@ -282,12 +281,12 @@ public function getObjectDetails(string $flagKey, $defaultValue, ?EvaluationCont * ----------------- * Methods, functions, or operations on the client MUST NOT throw exceptions, or otherwise abnormally terminate. Flag evaluation calls must always return the default value in the event of abnormal execution. Exceptions include functions or methods for the purposes for configuration or setup. * - * @param bool|string|int|float|DateTime|mixed[]|null $defaultValue + * @param bool|string|int|float|mixed[]|null $defaultValue */ private function evaluateFlag( FlagValueType $flagValueType, string $flagKey, - bool | string | int | float | DateTime | array | null $defaultValue, + bool | string | int | float | array | null $defaultValue, ?EvaluationContextInterface $invocationContext = null, ?EvaluationOptionsInterface $options = null, ): EvaluationDetailsInterface { @@ -383,6 +382,9 @@ private function evaluateFlag( return $details; } + /** + * @param bool|string|int|float|mixed[]|null $defaultValue + */ private function createProviderEvaluation( FlagValueType $type, string $key, @@ -394,35 +396,28 @@ private function createProviderEvaluation( case FlagValueType::Boolean->value: /** @var bool $defaultValue */; $defaultValue = $defaultValue; - $resolver = $provider->resolveBooleanValue(...); - break; + return $provider->resolveBooleanValue($key, $defaultValue, $context); case FlagValueType::String->value: /** @var string $defaultValue */; $defaultValue = $defaultValue; - $resolver = $provider->resolveStringValue(...); - break; + return $provider->resolveStringValue($key, $defaultValue, $context); case FlagValueType::Integer->value: /** @var int $defaultValue */; $defaultValue = $defaultValue; - $resolver = $provider->resolveIntegerValue(...); - break; + return $provider->resolveIntegerValue($key, $defaultValue, $context); case FlagValueType::Float->value: /** @var float $defaultValue */; $defaultValue = $defaultValue; - $resolver = $provider->resolveFloatValue(...); - break; + return $provider->resolveFloatValue($key, $defaultValue, $context); case FlagValueType::Object->value: - /** @var object $defaultValue */; + /** @var mixed[] $defaultValue */; $defaultValue = $defaultValue; - $resolver = $provider->resolveObjectValue(...); - break; + return $provider->resolveObjectValue($key, $defaultValue, $context); } - - return $resolver($key, $defaultValue, $context); } } diff --git a/src/implementation/common/ValueTypeValidator.php b/src/implementation/common/ValueTypeValidator.php index f4f1353..3fd12e7 100644 --- a/src/implementation/common/ValueTypeValidator.php +++ b/src/implementation/common/ValueTypeValidator.php @@ -82,7 +82,6 @@ public static function is(FlagValueType $type, mixed $value): bool FlagValueType::Integer => self::isInteger($value), FlagValueType::String => self::isString($value), FlagValueType::Object => self::isStructure($value) || self::isArray($value), - default => false, }; } } diff --git a/src/implementation/flags/EvaluationDetails.php b/src/implementation/flags/EvaluationDetails.php index a0f81ca..31e66eb 100644 --- a/src/implementation/flags/EvaluationDetails.php +++ b/src/implementation/flags/EvaluationDetails.php @@ -4,7 +4,6 @@ namespace OpenFeature\implementation\flags; -use DateTime; use OpenFeature\interfaces\flags\EvaluationDetails as EvaluationDetailsInterface; use OpenFeature\interfaces\provider\ResolutionError; @@ -12,8 +11,8 @@ class EvaluationDetails implements EvaluationDetailsInterface { private string $flagKey = ''; - /** @var bool|string|int|float|DateTime|mixed[]|null $value */ - private bool | string | int | float | DateTime | array | null $value = null; + /** @var bool|string|int|float|mixed[]|null $value */ + private bool | string | int | float | array | null $value = null; private ?ResolutionError $error = null; private ?string $reason = null; private ?string $variant = null; @@ -38,17 +37,17 @@ public function setFlagKey(string $flagKey): void * ----------------- * The evaluation details structure's value field MUST contain the evaluated flag value. * - * @return bool|string|int|float|DateTime|mixed[]|null + * @return bool|string|int|float|mixed[]|null */ - public function getValue(): bool | string | int | float | DateTime | array | null + public function getValue(): bool | string | int | float | array | null { return $this->value; } /** - * @param bool|string|int|float|DateTime|mixed[]|null $value + * @param bool|string|int|float|mixed[]|null $value */ - public function setValue(bool | string | int | float | DateTime | array | null $value): void + public function setValue(bool | string | int | float | array | null $value): void { $this->value = $value; } diff --git a/src/implementation/flags/EvaluationDetailsBuilder.php b/src/implementation/flags/EvaluationDetailsBuilder.php index 7cb4dc6..61e8cf6 100644 --- a/src/implementation/flags/EvaluationDetailsBuilder.php +++ b/src/implementation/flags/EvaluationDetailsBuilder.php @@ -4,7 +4,6 @@ namespace OpenFeature\implementation\flags; -use DateTime; use OpenFeature\interfaces\flags\EvaluationDetails as EvaluationDetailsInterface; use OpenFeature\interfaces\provider\ResolutionError; @@ -25,9 +24,9 @@ public function withFlagKey(string $flagKey): EvaluationDetailsBuilder } /** - * @param bool|string|int|float|DateTime|mixed[]|null $value + * @param bool|string|int|float|mixed[]|null $value */ - public function withValue(bool | string | int | float | DateTime | array | null $value): EvaluationDetailsBuilder + public function withValue(bool | string | int | float | array | null $value): EvaluationDetailsBuilder { $this->details->setValue($value); diff --git a/src/implementation/flags/EvaluationDetailsFactory.php b/src/implementation/flags/EvaluationDetailsFactory.php index 6115799..b491a15 100644 --- a/src/implementation/flags/EvaluationDetailsFactory.php +++ b/src/implementation/flags/EvaluationDetailsFactory.php @@ -4,7 +4,6 @@ namespace OpenFeature\implementation\flags; -use DateTime; use OpenFeature\interfaces\flags\EvaluationDetails; use OpenFeature\interfaces\provider\ResolutionDetails; @@ -13,9 +12,9 @@ class EvaluationDetailsFactory /** * Provides a simple method for building EvaluationDetails from a given value\ * - * @param bool|string|int|float|DateTime|mixed[]|null $value + * @param bool|string|int|float|mixed[]|null $value */ - public static function from(string $flagKey, bool | string | int | float | DateTime | array | null $value): EvaluationDetails + public static function from(string $flagKey, bool | string | int | float | array | null $value): EvaluationDetails { return (new EvaluationDetailsBuilder()) ->withFlagKey($flagKey) diff --git a/src/implementation/flags/NoOpClient.php b/src/implementation/flags/NoOpClient.php index 792e262..c35fb44 100644 --- a/src/implementation/flags/NoOpClient.php +++ b/src/implementation/flags/NoOpClient.php @@ -4,7 +4,6 @@ namespace OpenFeature\implementation\flags; -use DateTime; use OpenFeature\implementation\common\Metadata; use OpenFeature\interfaces\flags\Client; use OpenFeature\interfaces\flags\EvaluationContext as EvaluationContextInterface; @@ -69,7 +68,7 @@ public function getObjectValue( return $defaultValue; } - public function getObjectDetails(string $flagKey, bool | string | int | float | DateTime | array | null $defaultValue, ?EvaluationContextInterface $context = null, ?EvaluationOptions $options = null): EvaluationDetails + public function getObjectDetails(string $flagKey, bool | string | int | float | array | null $defaultValue, ?EvaluationContextInterface $context = null, ?EvaluationOptions $options = null): EvaluationDetails { return EvaluationDetailsFactory::from($flagKey, $defaultValue); } diff --git a/src/implementation/hooks/AbstractHookContext.php b/src/implementation/hooks/AbstractHookContext.php index 0a1cefc..678238c 100644 --- a/src/implementation/hooks/AbstractHookContext.php +++ b/src/implementation/hooks/AbstractHookContext.php @@ -4,7 +4,6 @@ namespace OpenFeature\implementation\hooks; -use DateTime; use Exception; use OpenFeature\implementation\common\Metadata; use OpenFeature\implementation\flags\EvaluationContext; @@ -13,15 +12,14 @@ use OpenFeature\interfaces\flags\FlagValueType; use OpenFeature\interfaces\hooks\HookContext; -use function array_values; use function is_array; abstract class AbstractHookContext { - protected string $flagKey; - protected FlagValueType $type; - /** @var bool|string|int|float|DateTime|mixed[]|null $defaultValue */ - protected bool | string | int | float | DateTime | array | null $defaultValue = null; + protected string $flagKey = ''; + protected FlagValueType $type = FlagValueType::Boolean; + /** @var bool|string|int|float|mixed[]|null $defaultValue */ + protected bool | string | int | float | array | null $defaultValue = null; protected EvaluationContextInterface $evaluationContext; protected MetadataInterface $clientMetadata; protected MetadataInterface $providerMetadata; @@ -48,7 +46,7 @@ public function __construct(HookContext | array | null $hookContext = null) $this->clientMetadata = $hookContext->getClientMetadata(); $this->providerMetadata = $hookContext->getProviderMetadata(); } elseif (is_array($hookContext)) { - foreach (array_values(self::REQUIRED_PROPERTIES) as $requiredProperty) { + foreach (self::REQUIRED_PROPERTIES as $requiredProperty) { if (!isset($hookContext[$requiredProperty])) { throw new Exception('Required property missing from hook context'); } diff --git a/src/implementation/hooks/HookContextBuilder.php b/src/implementation/hooks/HookContextBuilder.php index 110d98a..88805dd 100644 --- a/src/implementation/hooks/HookContextBuilder.php +++ b/src/implementation/hooks/HookContextBuilder.php @@ -4,7 +4,6 @@ namespace OpenFeature\implementation\hooks; -use DateTime; use OpenFeature\interfaces\common\Metadata; use OpenFeature\interfaces\flags\EvaluationContext; use OpenFeature\interfaces\flags\FlagValueType; @@ -35,9 +34,9 @@ public function withType(FlagValueType $type): self } /** - * @param bool|string|int|float|DateTime|mixed[]|null $defaultValue + * @param bool|string|int|float|mixed[]|null $defaultValue */ - public function withDefaultValue(bool | string | int | float | DateTime | array | null $defaultValue): self + public function withDefaultValue(bool | string | int | float | array | null $defaultValue): self { $this->hookContext->setDefaultValue($defaultValue); diff --git a/src/implementation/hooks/HookContextFactory.php b/src/implementation/hooks/HookContextFactory.php index ce4be9a..d633661 100644 --- a/src/implementation/hooks/HookContextFactory.php +++ b/src/implementation/hooks/HookContextFactory.php @@ -4,7 +4,6 @@ namespace OpenFeature\implementation\hooks; -use DateTime; use OpenFeature\implementation\flags\EvaluationContext; use OpenFeature\interfaces\common\Metadata; use OpenFeature\interfaces\flags\EvaluationContext as EvaluationContextInterface; @@ -14,12 +13,12 @@ class HookContextFactory { /** - * @param bool|string|int|float|DateTime|mixed[]|null $defaultValue + * @param bool|string|int|float|mixed[]|null $defaultValue */ public static function from( string $flagKey, FlagValueType $type, - bool | string | int | float | DateTime | array | null $defaultValue, + bool | string | int | float | array | null $defaultValue, ?EvaluationContextInterface $evaluationContext, Metadata $clientMetadata, Metadata $providerMetadata, diff --git a/src/implementation/hooks/ImmutableHookContext.php b/src/implementation/hooks/ImmutableHookContext.php index b7f1baa..2cb6756 100644 --- a/src/implementation/hooks/ImmutableHookContext.php +++ b/src/implementation/hooks/ImmutableHookContext.php @@ -4,7 +4,6 @@ namespace OpenFeature\implementation\hooks; -use DateTime; use OpenFeature\interfaces\common\Metadata; use OpenFeature\interfaces\flags\EvaluationContext; use OpenFeature\interfaces\flags\FlagValueType; @@ -23,9 +22,9 @@ public function getType(): FlagValueType } /** - * @return bool|string|int|float|DateTime|mixed[]|null + * @return bool|string|int|float|mixed[]|null */ - public function getDefaultValue(): bool | string | int | float | DateTime | array | null + public function getDefaultValue(): bool | string | int | float | array | null { return $this->defaultValue; } diff --git a/src/implementation/hooks/MutableHookContext.php b/src/implementation/hooks/MutableHookContext.php index d7226f5..348e0b5 100644 --- a/src/implementation/hooks/MutableHookContext.php +++ b/src/implementation/hooks/MutableHookContext.php @@ -4,7 +4,6 @@ namespace OpenFeature\implementation\hooks; -use DateTime; use OpenFeature\interfaces\common\Metadata; use OpenFeature\interfaces\flags\EvaluationContext; use OpenFeature\interfaces\flags\FlagValueType; @@ -23,7 +22,7 @@ public function setType(FlagValueType $type): void $this->type = $type; } - public function setDefaultValue(bool | string | int | float | DateTime | array | null $defaultValue): void + public function setDefaultValue(bool | string | int | float | array | null $defaultValue): void { $this->defaultValue = $defaultValue; } diff --git a/src/implementation/provider/Reason.php b/src/implementation/provider/Reason.php deleted file mode 100644 index 2905b78..0000000 --- a/src/implementation/provider/Reason.php +++ /dev/null @@ -1,15 +0,0 @@ -value; } /** - * @param bool|string|int|float|DateTime|mixed[]|null $value + * @param bool|string|int|float|mixed[]|null $value */ - public function setValue(bool | string | int | float | DateTime | array | null $value): void + public function setValue(bool | string | int | float | array | null $value): void { $this->value = $value; } diff --git a/src/implementation/provider/ResolutionDetailsBuilder.php b/src/implementation/provider/ResolutionDetailsBuilder.php index 73ae2c7..4028f6b 100644 --- a/src/implementation/provider/ResolutionDetailsBuilder.php +++ b/src/implementation/provider/ResolutionDetailsBuilder.php @@ -4,7 +4,6 @@ namespace OpenFeature\implementation\provider; -use DateTime; use OpenFeature\interfaces\provider\ResolutionDetails as ResolutionDetailsInterface; use OpenFeature\interfaces\provider\ResolutionError; @@ -18,9 +17,9 @@ public function __construct() } /** - * @param bool|string|int|float|DateTime|mixed[]|null $value + * @param bool|string|int|float|mixed[]|null $value */ - public function withValue(bool | string | int | float | DateTime | array | null $value): ResolutionDetailsBuilder + public function withValue(bool | string | int | float | array | null $value): ResolutionDetailsBuilder { $this->details->setValue($value); diff --git a/src/implementation/provider/ResolutionDetailsFactory.php b/src/implementation/provider/ResolutionDetailsFactory.php index 79d2e1b..42289ea 100644 --- a/src/implementation/provider/ResolutionDetailsFactory.php +++ b/src/implementation/provider/ResolutionDetailsFactory.php @@ -4,15 +4,14 @@ namespace OpenFeature\implementation\provider; -use DateTime; use OpenFeature\interfaces\provider\ResolutionDetails as ResolutionDetailsInterface; class ResolutionDetailsFactory { /** - * @param bool|string|int|float|DateTime|mixed[]|null $value + * @param bool|string|int|float|mixed[]|null $value */ - public static function fromSuccess(bool | string | int | float | DateTime | array | null $value): ResolutionDetailsInterface + public static function fromSuccess(bool | string | int | float | array | null $value): ResolutionDetailsInterface { return (new ResolutionDetailsBuilder()) ->withValue($value) diff --git a/src/interfaces/common/TypeValuePair.php b/src/interfaces/common/TypeValuePair.php deleted file mode 100644 index 767ac57..0000000 --- a/src/interfaces/common/TypeValuePair.php +++ /dev/null @@ -1,17 +0,0 @@ - 'test-key']); + $expectedValue = new MutableHookContext(['flagKey' => 'test-key', 'type' => FlagValueType::Boolean]); $actualValue = (new HookContextBuilder())->withFlagKey('test-key')->asMutable()->build();