ChainWrapper::__call()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 12
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace __\Traits\Sequence;
4
5
use __;
6
use Exception;
7
8
class ChainWrapper
9
{
10
    /**
11
     * @var mixed $value
12
     */
13
    private $value;
14
15
    /**
16
     * Php-lodash constructor.
17
     *
18
     * @param mixed $value the value that is going to be chained
19
     */
20 1
    public function __construct($value)
21
    {
22 1
        $this->value = $value;
23 1
    }
24
25
    /**
26
     * Dynamically calls php-lodash functions, prepend the list of parameters with the current collection list
27
     *
28
     * @param string $functionName must be a valid php-lodash function
29
     * @param array  $params
30
     *
31
     * @return $this
32
     * @throws \Exception
33
     */
34 1
    public function __call(string $functionName, array $params): self
35
    {
36 1
        if (is_callable('\__::' . $functionName, true)) {
37 1
            $params = $params == null ? [] : $params;
38 1
            $params = __::prepend($params, $this->value);
39
            /** @var callable $fnCallable */
40 1
            $fnCallable = ['\__', $functionName];
41 1
            $this->value = call_user_func_array($fnCallable, $params);
42
43 1
            return $this;
44
        } else {
45
            throw new Exception("Invalid function {$functionName}");
46
        }
47
    }
48
49
    /**
50
     * @return mixed
51
     */
52 1
    public function value()
53
    {
54 1
        return $this->value;
55
    }
56
}
57