Completed
Push — master ( faa8b2...2212f7 )
by Joao
01:49
created

Singleton   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
c 3
b 0
f 0
lcom 0
cbo 1
dl 0
loc 46
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __clone() 0 4 1
A __sleep() 0 4 1
A __wakeup() 0 4 1
A getInstance() 0 11 2
1
<?php
2
3
namespace ByJG\DesignPattern;
4
5
trait Singleton
6
{
7
    protected function __construct()
8
    {
9
    }
10
11
    /**
12
     * @throws SingletonException
13
     */
14
    final public function __clone()
15
    {
16
        throw new SingletonException('You can not clone a singleton.');
17
    }
18
19
    /**
20
     * @throws SingletonException
21
     */
22
    final public function __sleep()
23
    {
24
        throw new SingletonException('You can not serialize a singleton.');
25
    }
26
27
    /**
28
     * @throws SingletonException
29
     */
30
    final public function __wakeup()
31
    {
32
        throw new SingletonException('You can not deserialize a singleton.');
33
    }
34
35
    /**
36
     * @return static
37
     */
38
    public static function getInstance()
39
    {
40
        static $instances;
41
42
        $calledClass = get_called_class();
43
44
        if (!isset($instances[$calledClass])) {
45
            $instances[$calledClass] = new $calledClass();
46
        }
47
        return $instances[$calledClass];
48
    }
49
50
}
51