Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
mmelograno committed Mar 6, 2024
1 parent 465ac4d commit 532d0fb
Show file tree
Hide file tree
Showing 18 changed files with 285 additions and 696 deletions.
4 changes: 2 additions & 2 deletions src/SplitIO/Sdk.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ private function __construct()
}

/**
* @param $sdkKey
* @param string $sdkKey
* @param array $options
* @return \SplitIO\Sdk\Factory\SplitFactoryInterface
*/
public static function factory($sdkKey = 'localhost', array $options = array())
public static function factory(string $sdkKey = 'localhost', array $options = array())
{
//Adding SDK Key into args array.
$options['sdkKey'] = $sdkKey;
Expand Down
135 changes: 77 additions & 58 deletions src/SplitIO/Sdk/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
use SplitIO\Split as SplitApp;
use SplitIO\Sdk\Validator\InputValidator;
use SplitIO\Sdk\Validator\FlagSetsValidator;
use SplitIO\Component\Cache\SplitCache;
use SplitIO\Component\Cache\SegmentCache;
use SplitIO\Component\Cache\ImpressionCache;
use SplitIO\Component\Cache\EventsCache;

class Client implements ClientInterface
{
Expand All @@ -22,33 +26,33 @@ class Client implements ClientInterface
* Flag to get Impression's labels feature enabled
* @var bool
*/
private $labelsEnabled;
private bool $labelsEnabled;

/**
* @var \SplitIO\Component\Cache\SplitCache
*/
private $splitCache;
private SplitCache $splitCache;

/**
* @var \SplitIO\Component\Cache\SegmentCache
*/
private $segmentCache;
private SegmentCache $segmentCache;

/**
* @var \SplitIO\Component\Cache\ImpressionCache
*/
private $impressionCache;
private ImpressionCache $impressionCache;

/**
* @var \SplitIO\Component\Cache\EventsCache
*/
private $eventCache;
private EventsCache $eventCache;

/**
* @param array $storages
* @param array $options
*/
public function __construct($storages, $options = array())
public function __construct(array $storages, ?array $options = array())
{
$this->splitCache = $storages['splitCache'];
$this->segmentCache = $storages['segmentCache'];
Expand All @@ -67,17 +71,22 @@ public function __construct($storages, $options = array())
/**
* Builds new Impression object
*
* @param $matchingKey
* @param $featureFlag
* @param $treatment
* @param string $label
* @param $time
* @param string $key
* @param string $featureFlag
* @param string $treatment
* @param int $changeNumber
* @param string $bucketingKey
* @param string|null $label
* @param string|null $bucketingKey
*
* @return \SplitIO\Sdk\Impressions\Impression
*/
private function createImpression($key, $featureFlag, $treatment, $changeNumber, $label = '', $bucketingKey = null)
private function createImpression(
string $key,
string $featureFlag,
string $treatment,
int $changeNumber,
?string $label = '',
?string $bucketingKey = null)
{
if (!$this->labelsEnabled) {
$label = null;
Expand All @@ -89,14 +98,14 @@ private function createImpression($key, $featureFlag, $treatment, $changeNumber,
/**
* Verifies inputs for getTreatment and getTreatmentWithConfig methods
*
* @param $key
* @param $featureFlagName
* @param $attributes
* @param $operation
* @param string|Key $key
* @param string $featureFlagName
* @param string $operation
* @param array|null $attributes
*
* @return null|mixed
*/
private function doInputValidationForTreatment($key, $featureFlagName, array $attributes = null, $operation)
private function doInputValidationForTreatment(string|Key $key, string $featureFlagName, string $operation, ?array $attributes = null)
{
$key = InputValidator::validateKey($key, $operation);
if (is_null($key)) {
Expand All @@ -122,19 +131,18 @@ private function doInputValidationForTreatment($key, $featureFlagName, array $at
/**
* Executes evaluation for getTreatment or getTreatmentWithConfig
*
* @param $operation
* @param $metricName
* @param $key
* @param $featureFlagName
* @param $attributes
* @param string $operation
* @param string|Key $key
* @param string $featureFlagName
* @param array $attributes
*
* @return mixed
*/
private function doEvaluation($operation, $key, $featureFlagName, $attributes)
private function doEvaluation(string $operation, string|Key $key, string $featureFlagName, ?array $attributes)
{
$default = array('treatment' => TreatmentEnum::CONTROL, 'config' => null);

$inputValidation = $this->doInputValidationForTreatment($key, $featureFlagName, $attributes, $operation);
$inputValidation = $this->doInputValidationForTreatment($key, $featureFlagName, $operation, $attributes);
if (is_null($inputValidation)) {
return $default;
}
Expand All @@ -156,7 +164,7 @@ private function doEvaluation($operation, $key, $featureFlagName, $attributes)
$bucketingKey
);

$this->registerData($impression, $attributes);
$this->registerData(array($impression), $attributes);
return array(
'treatment' => $result['treatment'],
'config' => $result['config'],
Expand All @@ -177,7 +185,7 @@ private function doEvaluation($operation, $key, $featureFlagName, $attributes)
ImpressionLabel::EXCEPTION,
$bucketingKey
);
$this->registerData($impression, $attributes);
$this->registerData(array($impression), $attributes);
} catch (\Exception $e) {
SplitApp::logger()->critical(
"An error occurred when attempting to log impression for " .
Expand All @@ -191,7 +199,7 @@ private function doEvaluation($operation, $key, $featureFlagName, $attributes)
/**
* @inheritdoc
*/
public function getTreatment($key, $featureName, array $attributes = null)
public function getTreatment(string|Key $key, string $featureName, ?array $attributes = null): string
{
try {
$result = $this->doEvaluation(
Expand All @@ -210,7 +218,7 @@ public function getTreatment($key, $featureName, array $attributes = null)
/**
* @inheritdoc
*/
public function getTreatmentWithConfig($key, $featureFlagName, array $attributes = null)
public function getTreatmentWithConfig(string|Key $key, string $featureFlagName, ?array $attributes = null): array
{
try {
return $this->doEvaluation(
Expand All @@ -228,14 +236,14 @@ public function getTreatmentWithConfig($key, $featureFlagName, array $attributes
/**
* Verifies inputs for getTreatments and getTreatmentsWithConfig methods
*
* @param $key
* @param $featureNames
* @param $attributes
* @param $operation
* @param string|Key $key
* @param array $featureNames
* @param string $operation
* @param array|null $attributes
*
* @return null|mixed
*/
private function doInputValidationForTreatments($key, $featureFlagNames, array $attributes = null, $operation)
private function doInputValidationForTreatments(string|Key $key, array $featureFlagNames, string $operation, ?array $attributes = null)
{
$featureFlags = InputValidator::validateFeatureFlagNames($featureFlagNames, $operation);
if (is_null($featureFlags)) {
Expand All @@ -259,7 +267,7 @@ private function doInputValidationForTreatments($key, $featureFlagNames, array $
);
}

private function registerData($impressions, $attributes)
private function registerData(array $impressions, ?array $attributes)
{
try {
if (is_null($impressions) || (is_array($impressions) && 0 == count($impressions))) {
Expand All @@ -281,17 +289,16 @@ private function registerData($impressions, $attributes)
/**
* Executes evaluation for getTreatments or getTreatmentsWithConfig
*
* @param $operation
* @param $metricName
* @param $key
* @param $featureFlagNames
* @param $attributes
* @param string $operation
* @param string|Key $key
* @param array $featureFlagNames
* @param array $attributes
*
* @return mixed
*/
private function doEvaluationForTreatments($operation, $key, $featureFlagNames, $attributes)
private function doEvaluationForTreatments(string $operation, string|Key $key, array $featureFlagNames, ?array $attributes)
{
$inputValidation = $this->doInputValidationForTreatments($key, $featureFlagNames, $attributes, $operation);
$inputValidation = $this->doInputValidationForTreatments($key, $featureFlagNames, $operation, $attributes);
if (is_null($inputValidation)) {
return array();
}
Expand Down Expand Up @@ -328,7 +335,7 @@ private function doEvaluationForTreatments($operation, $key, $featureFlagNames,
/**
* @inheritdoc
*/
public function getTreatments($key, $featureFlagNames, array $attributes = null)
public function getTreatments(string|Key $key, array $featureFlagNames, ?array $attributes = null): array
{
try {
return array_map(
Expand All @@ -352,7 +359,7 @@ function ($feature) {
/**
* @inheritdoc
*/
public function getTreatmentsWithConfig($key, $featureFlagNames, array $attributes = null)
public function getTreatmentsWithConfig(string|Key $key, array $featureFlagNames, ?array $attributes = null): array
{
try {
return $this->doEvaluationForTreatments(
Expand All @@ -372,7 +379,7 @@ public function getTreatmentsWithConfig($key, $featureFlagNames, array $attribut
/**
* @inheritdoc
*/
public function isTreatment($key, $featureFlagName, $treatment)
public function isTreatment(string|Key $key, string $featureFlagName, string $treatment): bool
{
try {
$calculatedTreatment = $this->getTreatment($key, $featureFlagName);
Expand All @@ -396,7 +403,7 @@ public function isTreatment($key, $featureFlagName, $treatment)
/**
* @inheritdoc
*/
public function track($key, $trafficType, $eventType, $value = null, $properties = null)
public function track(string $key, string $trafficType, string $eventType, ?float $value = null, ?array $properties = null): bool
{
$key = InputValidator::validateTrackKey($key);
$trafficType = InputValidator::validateTrafficType($this->splitCache, $trafficType);
Expand Down Expand Up @@ -425,7 +432,10 @@ public function track($key, $trafficType, $eventType, $value = null, $properties
return false;
}

public function getTreatmentsByFlagSets($key, $flagSets, array $attributes = null)
/**
* @inheritdoc
*/
public function getTreatmentsByFlagSets(string|Key $key, array $flagSets, ?array $attributes = null): array
{
try {
return array_map(
Expand All @@ -445,7 +455,10 @@ function ($feature) {
}
}

public function getTreatmentsWithConfigByFlagSets($key, $flagSets, array $attributes = null)
/**
* @inheritdoc
*/
public function getTreatmentsWithConfigByFlagSets(string|Key $key, array $flagSets, ?array $attributes = null): array
{
try {
return $this->doEvaluationByFlagSets(
Expand All @@ -460,7 +473,10 @@ public function getTreatmentsWithConfigByFlagSets($key, $flagSets, array $attrib
}
}

public function getTreatmentsByFlagSet($key, $flagSet, array $attributes = null)
/**
* @inheritdoc
*/
public function getTreatmentsByFlagSet(string|Key $key, string $flagSet, ?array $attributes = null): array
{
try {
return array_map(
Expand All @@ -480,7 +496,10 @@ function ($feature) {
}
}

public function getTreatmentsWithConfigByFlagSet($key, $flagSet, array $attributes = null)
/**
* @inheritdoc
*/
public function getTreatmentsWithConfigByFlagSet(string|Key $key, string $flagSet, ?array $attributes = null): array
{
try {
return $this->doEvaluationByFlagSets(
Expand All @@ -495,7 +514,7 @@ public function getTreatmentsWithConfigByFlagSet($key, $flagSet, array $attribut
}
}

private function doInputValidationByFlagSets($key, $flagSets, array $attributes = null, $operation)
private function doInputValidationByFlagSets(string|Key $key, array $flagSets, string $operation, ?array $attributes = null): ?array
{
$key = InputValidator::validateKey($key, $operation);
if (is_null($key) || !InputValidator::validAttributes($attributes, $operation)) {
Expand All @@ -514,9 +533,9 @@ private function doInputValidationByFlagSets($key, $flagSets, array $attributes
);
}

private function doEvaluationByFlagSets($operation, $key, $flagSets, $attributes)
private function doEvaluationByFlagSets(string $operation, string|Key $key, array $flagSets, ?array $attributes): array
{
$inputValidation = $this->doInputValidationByFlagSets($key, $flagSets, $attributes, $operation);
$inputValidation = $this->doInputValidationByFlagSets($key, $flagSets, $operation, $attributes);
if (is_null($inputValidation)) {
return array();
}
Expand Down Expand Up @@ -548,11 +567,11 @@ private function doEvaluationByFlagSets($operation, $key, $flagSets, $attributes
}

private function processEvaluations(
$matchingKey,
$bucketingKey,
$operation,
$attributes,
$evaluations
string $matchingKey,
?string $bucketingKey,
string $operation,
?array $attributes,
array $evaluations
) {
$result = array();
$impressions = array();
Expand Down
Loading

0 comments on commit 532d0fb

Please sign in to comment.