From 9e9673d756a7d171e09a9480821763a6fb725404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Fri, 25 May 2018 23:21:47 +0200 Subject: [PATCH] Fix: Declare a single class per file --- src/LaunchDarkly/EvalResult.php | 47 +++++ src/LaunchDarkly/FeatureFlag.php | 194 -------------------- src/LaunchDarkly/InvalidSDKKeyException.php | 9 + src/LaunchDarkly/LDClient.php | 7 - src/LaunchDarkly/Prerequisite.php | 39 ++++ src/LaunchDarkly/Rollout.php | 41 +++++ src/LaunchDarkly/Segment.php | 61 ------ src/LaunchDarkly/SegmentRule.php | 63 +++++++ src/LaunchDarkly/Target.php | 39 ++++ src/LaunchDarkly/WeightedVariation.php | 39 ++++ tests/FeatureFlagTest.php | 51 ----- tests/LDClientTest.php | 42 +---- tests/MockFeatureRequester.php | 28 +++ tests/MockFeatureRequesterForFeature.php | 29 +++ tests/MockFeatureRequesterForSegment.php | 29 +++ 15 files changed, 372 insertions(+), 346 deletions(-) create mode 100644 src/LaunchDarkly/EvalResult.php create mode 100644 src/LaunchDarkly/InvalidSDKKeyException.php create mode 100644 src/LaunchDarkly/Prerequisite.php create mode 100644 src/LaunchDarkly/Rollout.php create mode 100644 src/LaunchDarkly/SegmentRule.php create mode 100644 src/LaunchDarkly/Target.php create mode 100644 src/LaunchDarkly/WeightedVariation.php create mode 100644 tests/MockFeatureRequester.php create mode 100644 tests/MockFeatureRequesterForFeature.php create mode 100644 tests/MockFeatureRequesterForSegment.php diff --git a/src/LaunchDarkly/EvalResult.php b/src/LaunchDarkly/EvalResult.php new file mode 100644 index 000000000..f2fff0a4b --- /dev/null +++ b/src/LaunchDarkly/EvalResult.php @@ -0,0 +1,47 @@ +_variation = $variation; + $this->_value = $value; + $this->_prerequisiteEvents = $prerequisiteEvents; + } + + /** + * @return int + */ + public function getVariation() + { + return $this->_variation; + } + + /** + * @return null + */ + public function getValue() + { + return $this->_value; + } + + /** + * @return array + */ + public function getPrerequisiteEvents() + { + return $this->_prerequisiteEvents; + } +} diff --git a/src/LaunchDarkly/FeatureFlag.php b/src/LaunchDarkly/FeatureFlag.php index 1bc81855d..2c94c2498 100644 --- a/src/LaunchDarkly/FeatureFlag.php +++ b/src/LaunchDarkly/FeatureFlag.php @@ -223,197 +223,3 @@ public function isDeleted() return $this->_deleted; } } - -class EvalResult -{ - private $_variation = null; - private $_value = null; - /** @var array */ - private $_prerequisiteEvents = []; - - /** - * EvalResult constructor. - * @param null $value - * @param array $prerequisiteEvents - */ - public function __construct($variation, $value, array $prerequisiteEvents) - { - $this->_variation = $variation; - $this->_value = $value; - $this->_prerequisiteEvents = $prerequisiteEvents; - } - - /** - * @return int - */ - public function getVariation() - { - return $this->_variation; - } - - /** - * @return null - */ - public function getValue() - { - return $this->_value; - } - - /** - * @return array - */ - public function getPrerequisiteEvents() - { - return $this->_prerequisiteEvents; - } -} - -class WeightedVariation -{ - /** @var int */ - private $_variation = null; - /** @var int */ - private $_weight = null; - - private function __construct($variation, $weight) - { - $this->_variation = $variation; - $this->_weight = $weight; - } - - public static function getDecoder() - { - return function ($v) { - return new WeightedVariation($v['variation'], $v['weight']); - }; - } - - /** - * @return int - */ - public function getVariation() - { - return $this->_variation; - } - - /** - * @return int - */ - public function getWeight() - { - return $this->_weight; - } -} - -class Target -{ - /** @var string[] */ - private $_values = array(); - /** @var int */ - private $_variation = null; - - protected function __construct(array $values, $variation) - { - $this->_values = $values; - $this->_variation = $variation; - } - - public static function getDecoder() - { - return function ($v) { - return new Target($v['values'], $v['variation']); - }; - } - - /** - * @return \string[] - */ - public function getValues() - { - return $this->_values; - } - - /** - * @return int - */ - public function getVariation() - { - return $this->_variation; - } -} - -class Prerequisite -{ - /** @var string */ - private $_key = null; - /** @var int */ - private $_variation = null; - - protected function __construct($key, $variation) - { - $this->_key = $key; - $this->_variation = $variation; - } - - public static function getDecoder() - { - return function ($v) { - return new Prerequisite($v['key'], $v['variation']); - }; - } - - /** - * @return string - */ - public function getKey() - { - return $this->_key; - } - - /** - * @return int - */ - public function getVariation() - { - return $this->_variation; - } -} - -class Rollout -{ - /** @var WeightedVariation[] */ - private $_variations = array(); - /** @var string */ - private $_bucketBy = null; - - protected function __construct(array $variations, $bucketBy) - { - $this->_variations = $variations; - $this->_bucketBy = $bucketBy; - } - - public static function getDecoder() - { - return function ($v) { - return new Rollout( - array_map(WeightedVariation::getDecoder(), $v['variations']), - isset($v['bucketBy']) ? $v['bucketBy'] : null); - }; - } - - /** - * @return WeightedVariation[] - */ - public function getVariations() - { - return $this->_variations; - } - - /** - * @return string - */ - public function getBucketBy() - { - return $this->_bucketBy; - } -} diff --git a/src/LaunchDarkly/InvalidSDKKeyException.php b/src/LaunchDarkly/InvalidSDKKeyException.php new file mode 100644 index 000000000..7eb7795bb --- /dev/null +++ b/src/LaunchDarkly/InvalidSDKKeyException.php @@ -0,0 +1,9 @@ +_key = $key; + $this->_variation = $variation; + } + + public static function getDecoder() + { + return function ($v) { + return new Prerequisite($v['key'], $v['variation']); + }; + } + + /** + * @return string + */ + public function getKey() + { + return $this->_key; + } + + /** + * @return int + */ + public function getVariation() + { + return $this->_variation; + } +} diff --git a/src/LaunchDarkly/Rollout.php b/src/LaunchDarkly/Rollout.php new file mode 100644 index 000000000..63ed2d1d7 --- /dev/null +++ b/src/LaunchDarkly/Rollout.php @@ -0,0 +1,41 @@ +_variations = $variations; + $this->_bucketBy = $bucketBy; + } + + public static function getDecoder() + { + return function ($v) { + return new Rollout( + array_map(WeightedVariation::getDecoder(), $v['variations']), + isset($v['bucketBy']) ? $v['bucketBy'] : null); + }; + } + + /** + * @return WeightedVariation[] + */ + public function getVariations() + { + return $this->_variations; + } + + /** + * @return string + */ + public function getBucketBy() + { + return $this->_bucketBy; + } +} diff --git a/src/LaunchDarkly/Segment.php b/src/LaunchDarkly/Segment.php index 620f2a22a..e29c5e4eb 100644 --- a/src/LaunchDarkly/Segment.php +++ b/src/LaunchDarkly/Segment.php @@ -102,64 +102,3 @@ public function isDeleted() return $this->_deleted; } } - -class SegmentRule -{ - /** @var Clause[] */ - private $_clauses = array(); - /** @var int */ - private $_weight = null; - /** @var string */ - private $_bucketBy = null; - - protected function __construct(array $clauses, $weight, $bucketBy) - { - $this->_clauses = $clauses; - $this->_weight = $weight; - $this->_bucketBy = $bucketBy; - } - - public static function getDecoder() - { - return function ($v) { - return new SegmentRule( - array_map(Clause::getDecoder(), $v['clauses'] ?: []), - isset($v['weight']) ? $v['weight'] : null, - isset($v['bucketBy']) ? $v['bucketBy'] : null); - }; - } - - public function matchesUser($user, $segmentKey, $segmentSalt) - { - foreach ($this->_clauses as $clause) { - if (!$clause->matchesUserNoSegments($user)) { - return false; - } - } - // If the weight is absent, this rule matches - if ($this->_weight === null) { - return true; - } - // All of the clauses are met. See if the user buckets in - $bucketBy = ($this->_bucketBy === null) ? "key" : bucketBy; - $bucket = VariationOrRollout::bucketUser($user, $segmentKey, $bucketBy, $segmentSalt); - $weight = $this->_weight / 100000.0; - return $bucket < $weight; - } - - /** - * @return Clause[] - */ - public function getClauses() - { - return $this->_clauses; - } - - /** - * @return string - */ - public function getBucketBy() - { - return $this->_bucketBy; - } -} diff --git a/src/LaunchDarkly/SegmentRule.php b/src/LaunchDarkly/SegmentRule.php new file mode 100644 index 000000000..91e14cc29 --- /dev/null +++ b/src/LaunchDarkly/SegmentRule.php @@ -0,0 +1,63 @@ +_clauses = $clauses; + $this->_weight = $weight; + $this->_bucketBy = $bucketBy; + } + + public static function getDecoder() + { + return function ($v) { + return new SegmentRule( + array_map(Clause::getDecoder(), $v['clauses'] ?: []), + isset($v['weight']) ? $v['weight'] : null, + isset($v['bucketBy']) ? $v['bucketBy'] : null); + }; + } + + public function matchesUser($user, $segmentKey, $segmentSalt) + { + foreach ($this->_clauses as $clause) { + if (!$clause->matchesUserNoSegments($user)) { + return false; + } + } + // If the weight is absent, this rule matches + if ($this->_weight === null) { + return true; + } + // All of the clauses are met. See if the user buckets in + $bucketBy = ($this->_bucketBy === null) ? "key" : bucketBy; + $bucket = VariationOrRollout::bucketUser($user, $segmentKey, $bucketBy, $segmentSalt); + $weight = $this->_weight / 100000.0; + return $bucket < $weight; + } + + /** + * @return Clause[] + */ + public function getClauses() + { + return $this->_clauses; + } + + /** + * @return string + */ + public function getBucketBy() + { + return $this->_bucketBy; + } +} diff --git a/src/LaunchDarkly/Target.php b/src/LaunchDarkly/Target.php new file mode 100644 index 000000000..1f024d0f6 --- /dev/null +++ b/src/LaunchDarkly/Target.php @@ -0,0 +1,39 @@ +_values = $values; + $this->_variation = $variation; + } + + public static function getDecoder() + { + return function ($v) { + return new Target($v['values'], $v['variation']); + }; + } + + /** + * @return \string[] + */ + public function getValues() + { + return $this->_values; + } + + /** + * @return int + */ + public function getVariation() + { + return $this->_variation; + } +} diff --git a/src/LaunchDarkly/WeightedVariation.php b/src/LaunchDarkly/WeightedVariation.php new file mode 100644 index 000000000..9335e70ce --- /dev/null +++ b/src/LaunchDarkly/WeightedVariation.php @@ -0,0 +1,39 @@ +_variation = $variation; + $this->_weight = $weight; + } + + public static function getDecoder() + { + return function ($v) { + return new WeightedVariation($v['variation'], $v['weight']); + }; + } + + /** + * @return int + */ + public function getVariation() + { + return $this->_variation; + } + + /** + * @return int + */ + public function getWeight() + { + return $this->_weight; + } +} diff --git a/tests/FeatureFlagTest.php b/tests/FeatureFlagTest.php index f64303648..e73531b76 100644 --- a/tests/FeatureFlagTest.php +++ b/tests/FeatureFlagTest.php @@ -2,7 +2,6 @@ namespace LaunchDarkly\Tests; use LaunchDarkly\FeatureFlag; -use LaunchDarkly\FeatureRequester; use LaunchDarkly\LDUserBuilder; use LaunchDarkly\Segment; @@ -566,53 +565,3 @@ private function makeBooleanFeatureWithSegmentMatch($segmentKey) return $this->booleanFlagWithClauses(array($clause)); } } - -class MockFeatureRequesterForFeature implements FeatureRequester -{ - public $key = null; - public $val = null; - - public function __construct($baseurl = null, $key = null, $options = null) - { - } - - public function getFeature($key) - { - return ($key == $this->key) ? $this->val : null; - } - - public function getSegment($key) - { - return null; - } - - public function getAllFeatures() - { - return null; - } -} - -class MockFeatureRequesterForSegment implements FeatureRequester -{ - public $key = null; - public $val = null; - - public function __construct($baseurl = null, $key = null, $options = null) - { - } - - public function getFeature($key) - { - return null; - } - - public function getSegment($key) - { - return ($key == $this->key) ? $this->val : null; - } - - public function getAllFeatures() - { - return null; - } -} diff --git a/tests/LDClientTest.php b/tests/LDClientTest.php index 53f664d2a..b5b36af6c 100644 --- a/tests/LDClientTest.php +++ b/tests/LDClientTest.php @@ -53,8 +53,8 @@ public function testToggleEvent() $builder = new LDUserBuilder(3); $user = $builder->build(); $client->variation('foo', $user, 'argdef'); - $proc = getPrivateField($client, '_eventProcessor'); - $queue = getPrivateField($proc, '_queue'); + $proc = $this->getPrivateField($client, '_eventProcessor'); + $queue = $this->getPrivateField($proc, '_queue'); $this->assertEquals(1, sizeof($queue)); } @@ -86,37 +86,13 @@ public function testLoggerInterfaceWarn() $client->variation('MyFeature', $user); } -} - -function getPrivateField(&$object, $fieldName) -{ - $reflection = new \ReflectionClass(get_class($object)); - $field = $reflection->getProperty($fieldName); - $field->setAccessible(true); - - return $field->getValue($object); -} - -class MockFeatureRequester implements FeatureRequester -{ - public static $val = null; - - public function __construct($baseurl, $key, $options) - { - } - - public function getFeature($key) - { - return self::$val; - } - - public function getSegment($key) - { - return null; - } - - public function getAllFeatures() + + private function getPrivateField(&$object, $fieldName) { - return null; + $reflection = new \ReflectionClass(get_class($object)); + $field = $reflection->getProperty($fieldName); + $field->setAccessible(true); + + return $field->getValue($object); } } diff --git a/tests/MockFeatureRequester.php b/tests/MockFeatureRequester.php new file mode 100644 index 000000000..b2e6cf847 --- /dev/null +++ b/tests/MockFeatureRequester.php @@ -0,0 +1,28 @@ +key) ? $this->val : null; + } + + public function getSegment($key) + { + return null; + } + + public function getAllFeatures() + { + return null; + } +} diff --git a/tests/MockFeatureRequesterForSegment.php b/tests/MockFeatureRequesterForSegment.php new file mode 100644 index 000000000..aa4af48ad --- /dev/null +++ b/tests/MockFeatureRequesterForSegment.php @@ -0,0 +1,29 @@ +key) ? $this->val : null; + } + + public function getAllFeatures() + { + return null; + } +}