LazyInitStaticTrait   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 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
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 lazyInitStatic() 0 16 4
1
<?php
2
3
namespace iiifx\LazyInit;
4
5
use Closure;
6
use ErrorException;
7
8
/**
9
 * Class LazyInitStaticTrait.
10
 *
11
 * @author  Vitaliy IIIFX Khomenko <[email protected]>
12
 *
13
 * @link    https://github.com/iiifx-production/lazy-init
14
 */
15
trait LazyInitStaticTrait
16
{
17
    /**
18
     * @var mixed[]
19
     */
20
    protected static $lazyInitStaticData = [ ];
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 11
    protected static function lazyInitStatic ( Closure $container, $dependency = null, array $params = [ ] )
32
    {
33
        /** @var string $key */
34 11
        if ( $dependency === null ) {
35 5
            $key = LazyInitHelper::createBacktraceKey();
36 11
        } elseif ( is_array( $dependency ) ) {
37 4
            $key = LazyInitHelper::createDependencyKey( $dependency );
38 4
        } else {
39 10
            $key = $dependency;
40
        }
41 11
        if ( !array_key_exists( $key, static::$lazyInitStaticData ) ) {
42 11
            static::$lazyInitStaticData[ $key ] = call_user_func_array( $container, $params );
43 11
        }
44
45 11
        return static::$lazyInitStaticData[ $key ];
46
    }
47
}
48