VariantResults   B
last analyzed

Complexity

Total Complexity 43

Size/Duplication

Total Lines 252
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 88.3%

Importance

Changes 0
Metric Value
dl 0
loc 252
rs 8.96
c 0
b 0
f 0
ccs 83
cts 94
cp 0.883
wmc 43
lcom 1
cbo 2

26 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 21 14
A toArray() 0 26 5
A getExperimentId() 0 4 1
A setExperimentId() 0 4 1
A getIsBaseline() 0 4 1
A setIsBaseline() 0 4 1
A getLevel() 0 4 1
A setLevel() 0 4 1
A getLift() 0 4 1
A setLift() 0 4 1
A getName() 0 4 1
A setName() 0 4 1
A getRate() 0 4 1
A setRate() 0 4 1
A getScope() 0 4 1
A setScope() 0 4 1
A getTotalIncrease() 0 4 1
A setTotalIncrease() 0 4 1
A getValue() 0 4 1
A setValue() 0 4 1
A getVariationId() 0 4 1
A setVariationId() 0 4 1
A getSamples() 0 4 1
A setSamples() 0 4 1
A getVariance() 0 4 1
A setVariance() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like VariantResults often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use VariantResults, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @author Oleg Krivtsov <[email protected]>
4
 * @date 12 October 2016
5
 * @copyright (c) 2016, Web Marketing ROI
6
 */
7
namespace WebMarketingROI\OptimizelyPHP\Resource\v2;
8
9
use WebMarketingROI\OptimizelyPHP\Exception;
10
use WebMarketingROI\OptimizelyPHP\Resource\v2\Datapoint;
11
12
/**
13
 * Optimizely campaign variant results.
14
 */
15
class VariantResults
16
{
17
    /**
18
     * The unique identifier for the Experiment this entity contains results for (if applicable).
19
     * @var integer 
20
     */
21
    private $experimentId;
22
    
23
    /**
24
     * Indicates that this variant is the baseline that all other entities will 
25
     * be compared against. Also referred to as the 'Control' or 'Control Group'.
26
     * @var boolean
27
     */
28
    private $isBaseline;
29
    
30
    /**
31
     *
32
     * @var type 
33
     */
34
    private $level;
35
    
36
    /**
37
     * The relative difference in performance of this variant vs. the baseline 
38
     * variant. Lift is calculated as follows: (Winning Conversion Rate % - Old 
39
     * Conversion Rate %) - Old Conversion Rate % = % Improvement
40
     * @var Datapoint
41
     */
42
    private $lift;
43
    
44
    /**
45
     * The name of the variant
46
     * @var string
47
     */
48
    private $name;
49
    
50
    /**
51
     *
52
     * @var number
53
     */
54
    private $rate;
55
    
56
    /**
57
     * The scope that this variant represents. Can be 'variation', 'experiment' or 'campaign'
58
     * @var string 
59
     */
60
    private $scope;
61
    
62
    /**
63
     *
64
     * @var Datapoint
65
     */
66
    private $totalIncrease;
67
    
68
    /**
69
     *
70
     * @var number
71
     */
72
    private $value;
73
    
74
    /**
75
     * 
76
     * @var string 
77
     */
78
    private $variationId;
79
    
80
    /**
81
     *
82
     * @var type 
83
     */
84
    private $samples;
85
    
86
    /**
87
     *
88
     * @var type 
89
     */
90
    private $variance;
91
    
92
    /**
93
     * Constructor.
94
     */
95 6
    public function __construct($options = array())
96
    {
97 6
        foreach ($options as $name=>$value) {
98
            switch ($name) {                
99 6
                case 'experiment_id': $this->setExperimentId($value); break;
100 6
                case 'is_baseline': $this->setIsBaseline($value); break;
101 6
                case 'level': $this->setLevel($value); break;
102 6
                case 'lift': $this->setLift(new Datapoint($value)); break;
103 6
                case 'name': $this->setName($value); break;
104 6
                case 'rate': $this->setRate($value); break;
105 6
                case 'scope': $this->setScope($value); break;
106 6
                case 'total_increase': $this->setTotalIncrease(new Datapoint($value)); break;
107 6
                case 'value': $this->setValue($value); break;
108 6
                case 'variation_id': $this->setVariationId($value); break;
109
                case 'samples': $this->setSamples($value); break;
110
                case 'variance': $this->setVariance($value); break;
111
                default:
112 6
                    throw new Exception('Unknown option found in the VariantResults entity: ' . $name);
113
            }
114
        }
115 6
    }
116
    
117
    /**
118
     * Returns this object as array.
119
     */
120 2
    public function toArray()
121
    {
122
        $options = array(
123 2
            'experiment_id' => $this->getExperimentId(),
124 2
            'is_baseline' => $this->getIsBaseline(),
125 2
            'level' => $this->getLevel(),
126 2
            'lift' => $this->getLift()?$this->getLift()->toArray():null,
127 2
            'name' => $this->getName(),
128 2
            'rate' => $this->getRate(),
129 2
            'scope' => $this->getScope(),
130 2
            'total_increase' => $this->getTotalIncrease()?$this->getTotalIncrease()->toArray():null,
131 2
            'value' => $this->getValue(),
132 2
            'variation_id' => $this->getVariationId(),
133 2
            'samples' => $this->getSamples(),
134 2
            'variance' => $this->getVariance(),
135
        );
136
        
137
        // Remove options with empty values
138 2
        $cleanedOptions = array();
139 2
        foreach ($options as $name=>$value) {
140 2
            if ($value!==null)
141 2
                $cleanedOptions[$name] = $value;
142
        }
143
        
144 2
        return $cleanedOptions;
145
    }
146
    
147 2
    public function getExperimentId()
148
    {
149 2
        return $this->experimentId;
150
    }
151
    
152 6
    public function setExperimentId($experimentId)
153
    {
154 6
        $this->experimentId = $experimentId;
155 6
    }
156
    
157 2
    public function getIsBaseline()
158
    {
159 2
        return $this->isBaseline;
160
    }
161
    
162 6
    public function setIsBaseline($isBaseline)
163
    {
164 6
        $this->isBaseline = $isBaseline;
165 6
    }
166
    
167 2
    public function getLevel()
168
    {
169 2
        return $this->level;
170
    }
171
    
172
    public function setLevel($level)
173
    {
174
        $this->level = $level;
175
    }
176
    
177 2
    public function getLift()
178
    {
179 2
        return $this->lift;
180
    }
181
    
182 6
    public function setLift($lift)
183
    {
184 6
        $this->lift = $lift;
185 6
    }
186
    
187 2
    public function getName()
188
    {
189 2
        return $this->name;
190
    }
191
    
192 6
    public function setName($name)
193
    {
194 6
        $this->name = $name;
195 6
    }
196
    
197 2
    public function getRate()
198
    {
199 2
        return $this->rate;
200
    }
201
    
202 6
    public function setRate($rate)
203
    {
204 6
        $this->rate = $rate;
205 6
    }
206
    
207 2
    public function getScope()
208
    {
209 2
        return $this->scope;
210
    }
211
    
212 6
    public function setScope($scope)
213
    {
214 6
        $this->scope = $scope;
215 6
    }
216
    
217 2
    public function getTotalIncrease()
218
    {
219 2
        return $this->totalIncrease;
220
    }
221
    
222 6
    public function setTotalIncrease($totalIncrease)
223
    {
224 6
        $this->totalIncrease = $totalIncrease;
225 6
    }
226
    
227 2
    public function getValue()
228
    {
229 2
        return $this->value;
230
    }
231
    
232 6
    public function setValue($value)
233
    {
234 6
        $this->value = $value;
235 6
    }
236
    
237 2
    public function getVariationId()
238
    {
239 2
        return $this->variationId;
240
    }
241
    
242 6
    public function setVariationId($variationId)
243
    {
244 6
        $this->variationId = $variationId;
245 6
    }
246
    
247 2
    public function getSamples()
248
    {
249 2
        return $this->samples;
250
    }
251
    
252
    public function setSamples($samples)
253
    {
254
        $this->samples = $samples;
255
    }
256
    
257 2
    public function getVariance()
258
    {
259 2
        return $this->variance;
260
    }
261
    
262
    public function setVariance($variance)
263
    {
264
        $this->variance = $variance;
265
    }
266
}
267
268
269
270
271
272
273
274
275