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

DecoratorTrait::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2
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
}