LazyInitTrait::lazyInit()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.2
cc 4
eloc 10
nc 6
nop 3
crap 4
1
<?php
2
3
namespace iiifx\LazyInit;
4
5
use Closure;
6
use ErrorException;
7
8
/**
9
 * Class LazyInitTrait.
10
 *
11
 * @author  Vitaliy IIIFX Khomenko <[email protected]>
12
 *
13
 * @link    https://github.com/iiifx-production/lazy-init
14
 */
15
trait LazyInitTrait
16
{
17
    /**
18
     * @var mixed[]
19
     */
20
    protected $lazyInitData = [ ];
21
22
    /**
23
     * @param Closure           $container
24
     * @param string|array|null $dependency
25
     * @param mixed[]           $params
26
     *
27
     * @return mixed
28
     *
29
     * @throws ErrorException
30
     */
31 5
    protected function lazyInit ( Closure $container, $dependency = null, array $params = [ ] )
32
    {
33
        /** @var string $key */
34 5
        if ( $dependency === null ) {
35 5
            $key = LazyInitHelper::createBacktraceKey();
36 5
        } elseif ( is_array( $dependency ) ) {
37 4
            $key = LazyInitHelper::createDependencyKey( $dependency );
38 4
        } else {
39 4
            $key = $dependency;
40
        }
41 5
        if ( !array_key_exists( $key, $this->lazyInitData ) ) {
42 5
            $this->lazyInitData[ $key ] = call_user_func_array( $container, $params );
43 5
        }
44
45 5
        return $this->lazyInitData[ $key ];
46
    }
47
}
48