Completed
Push — master ( 0c1923...90d6e2 )
by Anton
10s
created

Singleton   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 84.62%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 54
ccs 11
cts 13
cp 0.8462
rs 10
c 2
b 0
f 0
wmc 6
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setInstance() 0 4 1
A getInstance() 0 6 2
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 isset(static::$instance)
47 653
            ? static::$instance
48 653
            : static::$instance = static::initInstance();
49
    }
50
51
    /**
52
     * Initialization of class instance
53
     *
54
     * @return static
55
     */
56 1
    protected static function initInstance()
57
    {
58 1
        return new static;
59
    }
60
61
    /**
62
     * Disabled by access level
63
     */
64 1
    protected function __construct()
65
    {
66 1
    }
67
68
    /**
69
     * Disabled by access level
70
     */
71
    protected function __clone()
72
    {
73
    }
74
}
75