Completed
Pull Request — master (#8)
by Jeremy
04:54
created

DecoratorTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 79.41%

Importance

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

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getExperimentNameForMethod() 0 4 1
A getExperimentNameForAttribute() 0 4 1
A getExperiment() 0 3 1
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
1
<?php
2
3
4
namespace Scientist\Blind;
5
6
7
use Scientist\Experiment;
8
use Scientist\Study;
9
10
trait DecoratorTrait
11
{
12
    /** @var  Study */
13
    private $study;
14
    /** @var Experiment[] */
15
    private $experiments = [];
16
17
    /**
18
     * @param string $method
19
     * @return string
20
     */
21 4
    private function getExperimentNameForMethod($method)
22
    {
23 4
        return sprintf("%s::%s", $this->study->getName(), $method);
24
    }
25
26
    /**
27
     * @param string $attribute
28
     * @return string
29
     */
30 6
    private function getExperimentNameForAttribute($attribute)
31
    {
32 6
        return sprintf("%s::\$%s", $this->study->getName(), $attribute);
33
    }
34
35
    /**
36
     * @param string $name
37
     * @return Experiment
38
     */
39 8
    private function getExperiment($name) {
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
}