Completed
Push — master ( bb0194...d71e32 )
by Vitaliy
15:31
created

LazyInitHelper::createDependencyKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace iiifx\LazyInit;
4
5
use Closure;
6
use ErrorException;
7
8
/**
9
 * Class LazyInitHelper.
10
 *
11
 * @author Vitaliy IIIFX Khomenko <[email protected]>
12
 *
13
 * @link   https://github.com/iiifx-production/lazy-init
14
 */
15
class LazyInitHelper
0 ignored issues
show
Coding Style introduced by
LazyInitHelper does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
16
{
17
    use LazyInitStaticTrait;
18
19
    /**
20
     *
21
     */
22
    const PART_SEPARATOR = '#';
23
24
    /**
25
     * @param Closure     $closure
26
     * @param string|null $key
27
     * @param mixed[]     $params
28
     *
29
     * @return mixed
30
     *
31
     * @throws ErrorException
32
     */
33 4
    public static function lazyInit ( Closure $closure, $key = null, array $params = [ ] )
34
    {
35 4
        if ( $key === null ) {
36 4
            $key = static::createBacktraceKey();
37 4
        }
38
39 4
        return static::lazyInitStatic( $closure, $key, $params );
40
    }
41
42
    /**
43
     * @param int $backtraceDepth
44
     *
45
     * @return string
46
     *
47
     * @throws ErrorException
48
     */
49 15
    public static function createBacktraceKey ( $backtraceDepth = 3 )
50
    {
51 15
        return implode(
52 15
            static::PART_SEPARATOR,
53 15
            static::createBacktraceData( $backtraceDepth )
54 14
        );
55
    }
56
57
    /**
58
     * @param int $backtraceDepth
59
     *
60
     * @return array
61
     *
62
     * @throws ErrorException
63
     */
64 16
    public static function createBacktraceData ( $backtraceDepth = 0 )
65
    {
66 16
        $backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, $backtraceDepth );
67 16
        $backtraceKey = $backtraceDepth - 1;
68 16
        if ( isset( $backtrace[ $backtraceKey ][ 'file' ], $backtrace[ $backtraceKey ][ 'line' ] ) ) {
69 14
            $parts = [ ];
70 14
            $parts[] = $backtrace[ $backtraceKey ][ 'file' ];
71 14
            $parts[] = $backtrace[ $backtraceKey ][ 'line' ];
72
73 14
            return $parts;
74
        }
75 2
        throw new ErrorException( 'Unable to create BacktraceData.' );
76
    }
77
78
    /**
79
     * @param array $dependency
80
     *
81
     * @return string
82
     */
83 8
    public static function createDependencyKey ( array $dependency )
84
    {
85 8
        return md5( serialize( $dependency ) );
86
    }
87
}
88