Completed
Push — master ( 8b1dd2...f2d606 )
by Oleg
02:17
created

ExperimentResults   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 173
Duplicated Lines 4.62 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 93.85%

Importance

Changes 0
Metric Value
dl 8
loc 173
ccs 61
cts 65
cp 0.9385
rs 10
c 0
b 0
f 0
wmc 30
lcom 1
cbo 4

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setConfidenceThreshold() 0 4 1
A setEndTime() 0 4 1
A setExperimentId() 0 4 1
A setStatsConfig() 0 4 1
C __construct() 8 23 10
B toArray() 0 25 6
A getConfidenceThreshold() 0 4 1
A getEndTime() 0 4 1
A getExperimentId() 0 4 1
A getMetrics() 0 4 1
A setMetrics() 0 4 1
A getReach() 0 4 1
A setReach() 0 4 1
A getStartTime() 0 4 1
A setStartTime() 0 4 1
A getStatsConfig() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\ExperimentMetricResults;
11
use WebMarketingROI\OptimizelyPHP\Resource\v2\ExperimentVariationReach;
12
use WebMarketingROI\OptimizelyPHP\Resource\v2\StatsConfig;
13
14
/**
15
 * Optimizely experiment results.
16
 */
17
class ExperimentResults
18
{
19
    /**
20
     * The significance level at which you would like to declare winning and 
21
     * losing variations. A lower number minimizes the time needed to declare 
22
     * a winning or losing variation, but increases the risk that your results 
23
     * aren't true winners and losers.
24
     * @var number
25
     */
26
    private $confidenceThreshold;
27
    
28
    /**
29
     * The latest time to count events in results
30
     * @var string
31
     */
32
    private $endTime;
33
    
34
    /**
35
     * The unique identifier for the Experiment.
36
     * @var type 
37
     */
38
    private $experimentId;
39
    
40
    /**
41
     * The breakdown of experiment results by metric
42
     * @var array[ExperimentMetricResult]
43
     */
44
    private $metrics;
45
    
46
    /**
47
     * The total number of users exposed to a different experience
48
     * @var ExperimentVariationReach
49
     */
50
    private $reach;
51
    
52
    /**
53
     * The earliest time to count events in results
54
     * @var string 
55
     */
56
    private $startTime;
57
    
58
    /**
59
     *
60
     * @var StatsConfig 
61
     */
62
    private $statsConfig;
63
    
64
    /**
65
     * Constructor.
66
     */
67 3
    public function __construct($options = array())
68
    {
69 3
        foreach ($options as $name=>$value) {
70
            switch ($name) {                
71 3
                case 'confidence_threshold': $this->setConfidenceThreshold($value); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
72 3
                case 'end_time': $this->setEndTime($value); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
73 3
                case 'experiment_id': $this->setExperimentId($value); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
74 3 View Code Duplication
                case 'metrics': {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
75 3
                    $metrics = array();
76 3
                    foreach ($value as $metricInfo) {
77 3
                        $metrics[] = new ExperimentMetricResults($metricInfo);                        
78
                    }
79 3
                    $this->setMetrics($metrics); 
80 3
                    break;
81
                }
82 3
                case 'reach': $this->setReach(new ExperimentVariationReach($value)); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
83 3
                case 'start_time': $this->setStartTime($value); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
84
                case 'stats_config': $this->setStatsConfig(new StatsConfig($value)); break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
85
                default:
86 3
                    throw new Exception('Unknown option found in the ExperimentResults entity: ' . $name);
87
            }
88
        }
89 3
    }
90
    
91
    /**
92
     * Returns this object as array.
93
     */
94 1
    public function toArray()
95
    {
96
        $options = array(
97 1
            'confidence_threshold' => $this->getConfidenceThreshold(),
98 1
            'end_time' => $this->getEndTime(),
99 1
            'experiment_id' => $this->getExperimentId(),
100
            'metrics' => array(),
101 1
            'reach' => $this->getReach()?$this->getReach()->toArray():null,
102 1
            'start_time' => $this->getStartTime(),
103 1
            'stats_config' => $this->getStatsConfig()?$this->getStatsConfig()->toArray():null,
104
        );
105
        
106 1
        foreach ($this->getMetrics() as $metric) {
107 1
            $options['metrics'][] = $metric->toArray();
108
        }
109
        
110
        // Remove options with empty values
111 1
        $cleanedOptions = array();
112 1
        foreach ($options as $name=>$value) {
113 1
            if ($value!==null)
114 1
                $cleanedOptions[$name] = $value;
115
        }
116
        
117 1
        return $cleanedOptions;
118
    }
119
    
120 3
    public function getConfidenceThreshold()
121
    {
122 3
        return $this->confidenceThreshold;
123
    }
124
    
125 3
    public function setConfidenceThreshold($confidenceThreshold)
126
    {
127 3
        $this->confidenceThreshold = $confidenceThreshold;
128 3
    }
129
    
130 1
    public function getEndTime()
131
    {
132 1
        return $this->endTime;
133
    }
134
    
135 3
    public function setEndTime($endTime)
136
    {
137 3
        $this->endTime = $endTime;
138 3
    }
139
    
140 1
    public function getExperimentId()
141
    {
142 1
        return $this->experimentId;
143
    }
144
    
145 3
    public function setExperimentId($experimentId)
146
    {
147 3
        $this->experimentId = $experimentId;
148 3
    }
149
    
150 1
    public function getMetrics()
151
    {
152 1
        return $this->metrics;
153
    }
154
    
155 3
    public function setMetrics($metrics)
156
    {
157 3
        $this->metrics = $metrics;
158 3
    }
159
    
160 1
    public function getReach()
161
    {
162 1
        return $this->reach;
163
    }
164
    
165 3
    public function setReach($reach)
166
    {
167 3
        $this->reach = $reach;
168 3
    }
169
    
170 2
    public function getStartTime()
171
    {
172 2
        return $this->startTime;
173
    }
174
    
175 3
    public function setStartTime($startTime)
176
    {
177 3
        $this->startTime = $startTime;
178 3
    }
179
    
180 1
    public function getStatsConfig()
181
    {
182 1
        return $this->statsConfig;
183
    }
184
    
185
    public function setStatsConfig($statsConfig)
186
    {
187
        $this->statsConfig = $statsConfig;
188
    }
189
}
190
191
192
193
194
195
196
197
198
199
200