Skip to content

Commit

Permalink
fix: updates to support 8.1 and errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tcarrio committed Dec 4, 2023
1 parent a8a160b commit fb44796
Show file tree
Hide file tree
Showing 21 changed files with 54 additions and 110 deletions.
29 changes: 12 additions & 17 deletions src/OpenFeatureClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace OpenFeature;

use DateTime;
use OpenFeature\implementation\common\Metadata;
use OpenFeature\implementation\common\ValueTypeValidator;
use OpenFeature\implementation\errors\InvalidResolutionValueError;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -383,6 +382,9 @@ private function evaluateFlag(
return $details;
}

/**
* @param bool|string|int|float|mixed[]|null $defaultValue
*/
private function createProviderEvaluation(
FlagValueType $type,
string $key,
Expand All @@ -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);
}
}
1 change: 0 additions & 1 deletion src/implementation/common/ValueTypeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
}
13 changes: 6 additions & 7 deletions src/implementation/flags/EvaluationDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

namespace OpenFeature\implementation\flags;

use DateTime;
use OpenFeature\interfaces\flags\EvaluationDetails as EvaluationDetailsInterface;
use OpenFeature\interfaces\provider\ResolutionError;

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;
Expand All @@ -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;
}
Expand Down
5 changes: 2 additions & 3 deletions src/implementation/flags/EvaluationDetailsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace OpenFeature\implementation\flags;

use DateTime;
use OpenFeature\interfaces\flags\EvaluationDetails as EvaluationDetailsInterface;
use OpenFeature\interfaces\provider\ResolutionError;

Expand All @@ -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);

Expand Down
5 changes: 2 additions & 3 deletions src/implementation/flags/EvaluationDetailsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace OpenFeature\implementation\flags;

use DateTime;
use OpenFeature\interfaces\flags\EvaluationDetails;
use OpenFeature\interfaces\provider\ResolutionDetails;

Expand All @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions src/implementation/flags/NoOpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
12 changes: 5 additions & 7 deletions src/implementation/hooks/AbstractHookContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace OpenFeature\implementation\hooks;

use DateTime;
use Exception;
use OpenFeature\implementation\common\Metadata;
use OpenFeature\implementation\flags\EvaluationContext;
Expand All @@ -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;
Expand All @@ -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');

Check warning on line 51 in src/implementation/hooks/AbstractHookContext.php

View check run for this annotation

Codecov / codecov/patch

src/implementation/hooks/AbstractHookContext.php#L51

Added line #L51 was not covered by tests
}
Expand Down
5 changes: 2 additions & 3 deletions src/implementation/hooks/HookContextBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
5 changes: 2 additions & 3 deletions src/implementation/hooks/HookContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions src/implementation/hooks/ImmutableHookContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions src/implementation/hooks/MutableHookContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
15 changes: 0 additions & 15 deletions src/implementation/provider/Reason.php

This file was deleted.

13 changes: 6 additions & 7 deletions src/implementation/provider/ResolutionDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,29 @@

namespace OpenFeature\implementation\provider;

use DateTime;
use OpenFeature\interfaces\provider\ResolutionDetails as ResolutionDetailsInterface;
use OpenFeature\interfaces\provider\ResolutionError;

class ResolutionDetails implements ResolutionDetailsInterface
{
/** @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;

/**
* @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;
}
Expand Down
5 changes: 2 additions & 3 deletions src/implementation/provider/ResolutionDetailsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace OpenFeature\implementation\provider;

use DateTime;
use OpenFeature\interfaces\provider\ResolutionDetails as ResolutionDetailsInterface;
use OpenFeature\interfaces\provider\ResolutionError;

Expand All @@ -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);

Expand Down
Loading

0 comments on commit fb44796

Please sign in to comment.