Completed
Push — master ( e91368...26d5da )
by Anton
9s
created

Singleton   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 9
cts 11
cp 0.8182
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setInstance() 0 4 1
A getInstance() 0 4 1
A initInstance() 0 4 1
A __construct() 0 3 1
A __clone() 0 3 1
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link https://github.com/bluzphp/framework
7
 */
8
9
/**
10
 * @namespace
11
 */
12
namespace Bluz\Common;
13
14
/**
15
 * Singleton
16
 *
17
 * @package  Bluz\Common
18
 * @author   Anton Shevchuk
19
 * @link     https://github.com/bluzphp/framework/wiki/Trait-Singleton
20
 */
21
trait Singleton
22
{
23
    /**
24
     * @var static singleton instance
25
     */
26
    protected static $instance;
27
28
    /**
29
     * Set or replace instance
30
     *
31
     * @param  mixed $instance
32
     * @return void
33
     */
34 630
    public static function setInstance($instance)
35
    {
36 630
        static::$instance = $instance;
37 630
    }
38
39
    /**
40
     * Get instance
41
     *
42
     * @return static
43
     */
44 653
    public static function getInstance()
45
    {
46 653
        return static::$instance ?? (static::$instance = static::initInstance());
47
    }
48
49
    /**
50
     * Initialization of class instance
51
     *
52
     * @return static
53
     */
54 1
    protected static function initInstance()
55
    {
56 1
        return new static;
57
    }
58
59
    /**
60
     * Disabled by access level
61
     */
62 1
    protected function __construct()
63
    {
64 1
    }
65
66
    /**
67
     * Disabled by access level
68
     */
69
    protected function __clone()
70
    {
71
    }
72
}
73