LazyInitTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
c 3
b 0
f 0
lcom 1
cbo 1
dl 0
loc 33
ccs 11
cts 11
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A lazyInit() 0 16 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