Passed
Push — master ( b428ad...75fde3 )
by Бабичев
01:49
created

Lumper::_unique()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 2
crap 3
1
<?php
2
3
namespace Bavix\Lumper;
4
5
use Bavix\Slice\Slice;
6
7
class Lumper
8
{
9
10
    /**
11
     * @var []
12
     */
13
    protected $instances = [];
14
15
    /**
16
     * @param callable $callback
17
     *
18
     * @return string
19
     */
20 2
    protected function _string(callable $callback)
21
    {
22 2
        return new Reflection\Func($callback);
23
    }
24
25
    /**
26
     * @param callable $callback
27
     * @param string   $hash
28
     *
29
     * @return string
30
     */
31 3
    protected function _unique(callable $callback, $hash)
32
    {
33 3
        if (null === $hash || $hash instanceof Slice)
34
        {
35 2
            $hash .= $this->_string($callback);
36
        }
37
38 3
        return $hash;
39
    }
40
41
    /**
42
     * @param callable $callback
43
     * @param string   $unique
44
     *
45
     * @return mixed
46
     */
47 3
    public function once(callable $callback, $unique = null)
48
    {
49 3
        $_ = (string)$this->_unique($callback, $unique);
50
51 3
        if (empty($this->instances[$_]))
52
        {
53 3
            $this->instances[$_] =
54 3
                $unique instanceof Slice ?
55 1
                    $callback($unique) :
56 2
                    $callback();
57
        }
58
59 3
        return $this->instances[$_];
60
    }
61
62
}
63