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

Singleton::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 1
b 0
f 0
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