Singleton::__clone()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the light/traits.
5
 *
6
 * (c) lichunqiang <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Light\Traits\DesignPattern;
13
14
/**
15
 * This implements the singleton design pattern.
16
 */
17
trait Singleton
18
{
19
    /**
20
     * @var static
21
     */
22
    protected static $_instance;
23
24
    /**
25
     * Get the instance.
26
     *
27
     * @return static
28
     */
29 3
    final public static function getInstance()
30
    {
31 3
        if (null === static::$_instance) {
32 1
            static::$_instance = new static();
33 1
        }
34
35 3
        return static::$_instance;
36
    }
37
38
    /**
39
     * Swap the instance to another.
40
     *
41
     * @param mixed $instance
42
     */
43 1
    final public static function swap($instance)
44
    {
45 1
        static::$_instance = $instance;
46 1
    }
47
48
    /**
49
     * Make __construct private to avoid new the object.
50
     */
51 1
    private function __construct()
52
    {
53 1
    }
54
55
    /**
56
     * Disable clone.
57
     */
58
    private function __clone()
59
    {
60
    }
61
}
62