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

DecoratorTrait::__get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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