Completed
Pull Request — master (#8)
by Jeremy
06:38 queued 04:13
created

DecoratorTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 79.41%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 12
c 2
b 1
f 1
lcom 2
cbo 1
dl 0
loc 89
ccs 27
cts 34
cp 0.7941
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A __call() 0 5 1
A __get() 0 5 1
A __set() 0 5 1
A __isset() 0 5 1
A __unset() 0 4 1
A __toString() 0 5 1
A __invoke() 0 6 1
A getExperimentNameForMethod() 0 4 1
A getExperimentNameForAttribute() 0 4 1
A getExperiment() 0 4 1
1
<?php
2
3
4
namespace Scientist\Blind;
5
6
use Scientist\Experiment;
7
use Scientist\Study;
8
9
trait DecoratorTrait
10
{
11
    /** @var  Study */
12
    private $study;
13
    /** @var Experiment[] */
14
    private $experiments = [];
15
16
    /**
17
     * @param string $method
18
     * @return string
19
     */
20 4
    private function getExperimentNameForMethod($method)
21
    {
22 4
        return sprintf("%s::%s", $this->study->getName(), $method);
23
    }
24
25
    /**
26
     * @param string $attribute
27
     * @return string
28
     */
29 6
    private function getExperimentNameForAttribute($attribute)
30
    {
31 6
        return sprintf("%s::\$%s", $this->study->getName(), $attribute);
32
    }
33
34
    /**
35
     * @param string $name
36
     * @return Experiment
37
     */
38 8
    private function getExperiment($name)
39
    {
40 8
        return $this->experiments[$name];
41
    }
42
43
    /**
44
     * DecoratorTrait constructor.
45
     * @param Study $study
46
     * @param Experiment[] $experiments
47
     */
48 14
    function __construct(Study $study, array $experiments = [])
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
49
    {
50 14
        $this->study = $study;
51 14
        foreach ($experiments as $experiment) {
52 14
            $this->experiments[$experiment->getName()] = $experiment;
53 14
        }
54 14
    }
55
56 4
    function __call($name, $arguments)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
57
    {
58 4
        $experiment = $this->getExperiment($this->getExperimentNameForMethod($name));
59 4
        return call_user_func_array([$experiment, 'run'], $arguments);
60
    }
61
62 3
    function __get($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
63
    {
64 3
        $experiment = $this->getExperiment($this->getExperimentNameForAttribute($name));
65 3
        return call_user_func_array([$experiment, 'run'], [$name]);
66
    }
67
68 4
    function __set($name, $value)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
69
    {
70 4
        $experiment = $this->getExperiment($this->getExperimentNameForAttribute($name));
71 4
        return call_user_func_array([$experiment, 'run'], [$name, $value]);
72
    }
73
74 1
    function __isset($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
75
    {
76 1
        $value = $this->__get($name);
77 1
        return $value !== null;
78
    }
79
80 1
    function __unset($name)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
81
    {
82 1
        $this->__set($name, null);
83 1
    }
84
85
    function __toString()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
86
    {
87
        $experiment = $this->getExperiment($this->getExperimentNameForMethod('__toString'));
88
        return call_user_func([$experiment, 'run']);
89
    }
90
91
    function __invoke()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
92
    {
93
        $arguments = func_get_args();
94
        $experiment = $this->getExperiment($this->getExperimentNameForMethod('__invoke'));
95
        return call_user_func_array([$experiment, 'run'], $arguments);
96
    }
97
}
98