Skip to content

Config v2 branch #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open

Config v2 branch #1

wants to merge 16 commits into from

Conversation

shujat333
Copy link
Owner

Summary

  • The "what"; a concise description of each logical change
  • Another change

The "why", or other context.

Test plan

Issues

Copy link

@oakbani oakbani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revise all changes -

  • use camel casing everywhere.
  • update headers.
  • do not assign a value to local variable if it's only being used once.

*
* @var [Attribute]
*/
private $_attributes;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to prefix variable name with underscore for private variables. It was old practice. Also revise the same for all new variables you have added.

/**
* @var string environmentKey of the config.
*/
private $environment_key;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHP uses camel casing to name variables. Also revise others too

$this->_attributes = $config['attributes'] ?: [];
$this->_audiences = $config['audiences'] ?: [];
$this->_events = $config['events'] ?: [];
$this->_typed_audiences = isset($config['typedAudiences']) ? $config['typedAudiences']: [];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

local variables for same are declared below as well. Get rid of local variables and use these new variables in below code.

@@ -93,6 +93,18 @@ class DatafileProjectConfig implements ProjectConfigInterface
*/
private $datafile;

/**
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update header of this file and all others. 2021

/**
* @var string sdkKey of the config.
*/
private $sdk_key;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment. use camel case

/**
* @var string Contents of datafile.
*/
private $datafile;


public function __construct($revision, array $experimentsMap, array $featuresMap, $datafile = null)
public function __construct($revision, array $experimentsMap, array $featuresMap, $datafile = null, $environment_key='', $sdk_key='', array $attributes=[], array $audiences=[], array $events=[])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

camel casing for method args

$id = $attr["id"];
$key = $attr["key"];
$optly_attr = new OptimizelyAttribute(
$id,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

directly use $attr["id"]; here instead of assigning both id and key to local variables first

*/
protected function getConfigAttributes()
{
$attribut_arr = [];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

camel case

@@ -207,6 +318,85 @@ protected function getVariationsMap(Experiment $experiment)
return $variationsMap;
}

/**
* Generates array of delivery rules for optimizelyFeature.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong definition

*
* @return array of optimizelyExperiments as delivery rules .
*/
protected function getExperimentAudiences(array $audience_cond)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

willl review this later

Copy link

@oakbani oakbani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Some more comments

foreach ($normalAudiences as $naudience) {
$id = $naudience['id'];
if (in_array($id, $uniqueIds) == false) {
array_push($audiencesArray, $naudience);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of false put a ! before in_array

$resAudiences = '';
$audienceConditions = array('and', 'or', 'not');
if ($audienceCond != null){
$cond = '';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return If audience cond is null. This way you can reduce 1 level nesting

@@ -223,11 +402,21 @@ protected function getExperimentsMaps()
foreach ($this->experiments as $exp) {
$expId = $exp->getId();
$expKey = $exp->getKey();
$audiences = '';
if (is_null($exp->getAudienceConditions())) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of is_null use === null to avoid an extra function call

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to put this condition audiences is already empty str. Just put the else logic in an if block

$expId = $exp->getId();
$expKey = $exp->getKey();
$audiences = '';
if (is_null($exp->getAudienceConditions())) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. No need for this condition. You are setting the same value

$experimentRules = [];
$deliveryRules = [];
$rollout_id = $feature->getRolloutId();
if (is_null($rollout_id))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

* @return string Experiment audiences.
*/
public function getExperimentAudiences()
{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to getAudiences

Copy link

@yasirfolio3 yasirfolio3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Please add more tests for conditions.
  2. Update headers in all files.
  3. Follow previous work for proper naming and comments.

Follow C# SDK PR for tests and implementation issues.

/**
* list of Audiences that will be parsed from the datafile
*
* @var [Audiences]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @var [Audiences]
* @var [Audience]

@@ -51,6 +51,37 @@ public function getBotFiltering();
*/
public function getRevision();

/**
* @return string String represnting envkey of the datafile.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return string String represnting envkey of the datafile.
* @return string String representing environment key of the datafile.

@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2020, Optimizely Inc and Contributors
* Copyright 2021, Optimizely Inc and Contributors

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Copyright 2021, Optimizely Inc and Contributors
* Copyright 2020-2021, Optimizely Inc and Contributors

/**
* array of attributes as OptimizelyAttribute.
*
* @var <String, OptimizelyAttribute> associative array

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix this comment, this is not supposed to be a map.



/**
* Map of rollout Experiments Keys to OptimizelyExperiments.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a map, it's an array.

@@ -66,6 +84,22 @@ public function getKey()
return $this->key;
}

/**
* @return array Map of Experiment Keys to OptimizelyExperiments.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a map, it's an array.

}

/**
* @return array Map of Rollout Experiments Keys to OptimizelyExperiments.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a map, it's an array. Please verify the comment from other SDKs.

@@ -28,17 +28,24 @@ class OptimizelyExperiment implements \JsonSerializable
*/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update header file with 2020-2021

@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2020, Optimizely
* Copyright 2021, Optimizely

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Copyright 2021, Optimizely
* Copyright 2020-2021, Optimizely

Copy link

@oakbani oakbani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Minor comments

@@ -51,6 +51,37 @@ public function getBotFiltering();
*/
public function getRevision();

/**
* @return string String represnting representing environment key of the datafile.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo. representing

$attributArray = [];
$attributes = $this->projectConfig->getAttributes();
foreach ($attributes as $attr) {
$optly_attr = new OptimizelyAttribute(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$optlyArr

$attr['id'],
$attr['key']
);
array_push($attributArray, $optly_attr);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attributeArray

$eventsArray = [];
$events = $this->projectConfig->getEvents();
foreach ($events as $event) {
$optly_event = new OptimizelyEvent(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$optlyEvent

$audiencesArray = $typedAudiences;
foreach ($audiencesArray as $typedAudience) {
$id = $typedAudience['id'];
array_push($uniqueIds, $id);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

directly push $typedAudience['id'];. No need to assign it to a variable here

$id = $audience['id'];
if ($id != '$opt_dummy_audience') {
$optly_audience = new OptimizelyAudience(
$audience['id'],
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use $id here. Or remove $id assignment above.

@@ -210,8 +251,8 @@ class DatafileProjectConfig implements ProjectConfigInterface
/**
* DatafileProjectConfig constructor to load and set project configuration data.
*
* @param $datafile string JSON string representing the project.
* @param $logger LoggerInterface
* @param $datafile string JSON string representing the project.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra spaces?

$this->attributes = $config['attributes'] ?: [];
$this->audiences = $config['audiences'] ?: [];
$this->events = $config['events'] ?: [];
$this->typedAudiences = isset($config['typedAudiences']) ? $config['typedAudiences'] : [];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this can't be $config['typedAudiences'] ?: []

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i just followed the existing way to get typedAudiences ealier it was being used as a variable but i made it a class attribute.

$this->events = $config['events'] ?: [];
$this->typedAudiences = isset($config['typedAudiences']) ? $config['typedAudiences'] : [];
$this->_anonymizeIP = isset($config['anonymizeIP']) ? $config['anonymizeIP'] : false;
$this->_botFiltering = isset($config['botFiltering']) ? $config['botFiltering'] : null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the default value, why specifying explicit null.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default value for botFiltering was not mentioned anywhere i did not change this line it is as it was.

$this->_revision = $config['revision'];
$this->_sendFlagDecisions = isset($config['sendFlagDecisions']) ? $config['sendFlagDecisions'] : false;

$groups = $config['groups'] ?: [];
$experiments = $config['experiments'] ?: [];
$events = $config['events'] ?: [];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason to remove it?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they were being used as variables but i need to get them as attributes so delete the variables and using attributes in further functions

* @param bool $skipJsonValidation boolean representing whether JSON schema validation needs to be performed.
* @param LoggerInterface $logger Logger instance
* @param ErrorHandlerInterface $errorHandler ErrorHandler instance.
* @param string $datafile JSON string representing the Optimizely project.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra spaces.

return array_filter(array_values($this->_experimentKeyMap), function ($experiment) use ($rolloutExperimentIds) {
return !in_array($experiment->getId(), $rolloutExperimentIds);
});
return array_filter(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't change just for the sake of change. please.

/**
* @return array List of attributes parsed from the datafile
*/
public function getAttributes();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

following are not a part of ProjectConfigInterface

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attributes, audiences, events, typedAudiences.
check in other sdks, how retrieving arrays.

Copy link

@mnoman09 mnoman09 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some comments

$rollouts = isset($config['rollouts']) ? $config['rollouts'] : [];
$featureFlags = isset($config['featureFlags']) ? $config['featureFlags']: [];
$featureFlags = isset($config['featureFlags']) ? $config['featureFlags'] : [];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid nit changes in parts of code which are not changed. Change it back to what it was.

Copy link
Owner Author

@shujat333 shujat333 Aug 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually i started my work before the pr of duplicate exp key issue was merged with master so i just update my branch with the latest master branch to use that instead of doing that task again

@@ -627,14 +721,15 @@ public function getAttribute($attributeKey)

/**
* @param $experimentKey string Key for experiment.
* @param $variationKey string Key for variation.
* @param $variationKey string Key for variation.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need of this change in this PR.

Copy link
Owner Author

@shujat333 shujat333 Aug 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually i started my work before the pr of duplicate exp key issue was merged with master so i just update my branch with the latest master branch to use that instead of doing that task again

*
* @return Variation Entity corresponding to the provided experiment key and variation key.
* Dummy entity is returned if key or ID is invalid.
*/
public function getVariationFromKey($experimentKey, $variationKey)
{
if (isset($this->_variationKeyMap[$experimentKey])
if (
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change all the extra nit changes you have done back to as it was. make another PR if you want to fix these nits.

private $id;

/**
* @var string name representing audience.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @var string name representing audience.
* @var string representing audience name.

class OptimizelyAudience implements \JsonSerializable
{
/**
* @var string id representing audience.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @var string id representing audience.
* @var string representing audience id.

@@ -207,6 +307,77 @@ protected function getVariationsMap(Experiment $experiment)
return $variationsMap;
}

/**
* Generates string of audience conditions array mapped to audience names.
*
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add some examples in comments as well see csharp link

protected function getSerializedAudiences(array $audienceConditions)
{
$finalAudiences = '';
$ConditionsArray = array('and', 'or', 'not');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of initializing it over here can we move it outside to function to avoid initializing it over and over as you are also using recursion.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have to initialize it in constructor which makes it more annoying.

*
* @return string of experiment audience conditions.
*/
protected function getSerializedAudiences(array $audienceConditions)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comments in the function and explain the logic see Csharp link

if ($audience == null) {
$finalAudiences = $finalAudiences . $cond . ' ' . '"' . $itemStr . '"';
} else {
$name = $audience->getName();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we refactor this logic and take name before checking audience.
something like this
$name = $audience == null ? $itemStr : $audience->getName();
we can then avoid condition of audience == null and use
$finalAudiences = $finalAudiences . $cond . ' ' . '"' . $name. '"';

$this->optConfigService,
array($audienceConditions[$testNo])
);
//fwrite(STDERR, print_r(gettype($response), true));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants