Completed
Pull Request — master (#12)
by Mauro
02:28
created

Intern   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 125
Duplicated Lines 16.8 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 15
c 5
b 0
f 0
lcom 1
cbo 5
dl 21
loc 125
ccs 60
cts 60
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 25 3
A runControl() 12 21 3
B runTrials() 9 26 4
B determineMatches() 0 18 5

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
namespace Scientist;
4
5
use Scientist\Matchers\Matcher;
6
7
/**
8
 * Class Intern
9
 *
10
 * Interns do all the hard work, naturally. They'll execute an experiment within
11
 * the Laboratory, and record the results.
12
 *
13
 * @package \Scientist
14
 */
15
class Intern
16
{
17
    /**
18
     * Run an experiment, and retrieve the result.
19
     *
20
     * @param \Scientist\Experiment $experiment
21
     *
22
     * @return \Scientist\Report|\Scientist\Report[]
23
     */
24 14
    public function run(Experiment $experiment)
25
    {
26 14
        $control = $this->runControl($experiment);
27 13
        $trials  = $this->runTrials($experiment);
28
29 13
        if (is_array($control)) {
30 2
            $reports = [];
31 2
            foreach ($control as $key => $control_item) {
32 2
                $this->determineMatches(
33 2
                    $experiment->getMatcher(),
34 2
                    $control_item,
35 2
                    $trials,
36
                    $key
37 2
                );
38
39 2
                $reports[] = new Report($experiment->getName(), $control_item, $trials);
40 2
            }
41
42 2
            return $reports;
43
        } else {
44 11
            $this->determineMatches($experiment->getMatcher(), $control, $trials);
45
46 11
            return new Report($experiment->getName(), $control, $trials);
47
        }
48
    }
49
50
    /**
51
     * Run the control callback, and record its execution state.
52
     *
53
     * @param \Scientist\Experiment $experiment
54
     *
55
     * @return \Scientist\Result|\Scientist\Result[]
56
     */
57 14
    protected function runControl(Experiment $experiment)
58
    {
59 14
        if (count($experiment->getParams())
60 14
            === count($experiment->getParams(), COUNT_RECURSIVE)) {
61 12
            return (new Machine(
62 12
                $experiment->getControl(),
63 12
                $experiment->getParams()
64 12
            ))->execute();
65 View Code Duplication
        } else {
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...
66 2
            $executions = [];
67
68 2
            foreach ($experiment->getParams()[0] as $individual_params) {
69 2
                $executions[] = (new Machine(
70 2
                    $experiment->getControl(),
71
                    $individual_params
72 2
                ))->execute();
73 2
            }
74
75 2
            return $executions;
76
        }
77
    }
78
79
    /**
80
     * Run trial callbacks and record their execution state.
81
     *
82
     * @param \Scientist\Experiment $experiment
83
     *
84
     * @return \Scientist\Result[]
85
     */
86 13
    protected function runTrials(Experiment $experiment)
87
    {
88 13
        $executions = [];
89
90 13
        foreach ($experiment->getTrials() as $name => $trial) {
91 11
            if (count($experiment->getParams())
92 11
                === count($experiment->getParams(), COUNT_RECURSIVE)) {
93 9
                $executions[$name] = (new Machine(
94 9
                    $trial,
95 9
                    $experiment->getParams(),
96
                    true
97 9
                ))->execute();
98 9 View Code Duplication
            } else {
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...
99 2
                foreach ($experiment->getParams()[0] as $individual_params) {
100 2
                    $executions[$name][] = (new Machine(
101 2
                        $trial,
102 2
                        $individual_params,
103
                        true
104 2
                    ))->execute();
105 2
                }
106
            }
107
108 13
        }
109
110 13
        return $executions;
111
    }
112
113
    /**
114
     * Determine whether trial results match the control.
115
     *
116
     * @param \Scientist\Matchers\Matcher $matcher
117
     * @param \Scientist\Result           $control
118
     * @param \Scientist\Result[]         $trials
119
     * @param int|null                    $key
120
     */
121 13
    protected function determineMatches(
122
        Matcher $matcher,
123
        Result $control,
124
        array $trials = [],
125
        $key = null
126
    ) {
127 13
        foreach ($trials as $trial) {
128 11
            if ($key !== null) {
129 2
                if ($matcher->match($control->getValue(), $trial[$key]->getValue())) {
130 2
                    $trial[$key]->setMatch(true);
131 2
                }
132 2
            } else {
133 9
                if ($matcher->match($control->getValue(), $trial->getValue())) {
134 3
                    $trial->setMatch(true);
135 3
                }
136
            }
137 13
        }
138 13
    }
139
}
140