Singleton   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 6
eloc 9
c 4
b 0
f 0
dl 0
loc 44
ccs 12
cts 14
cp 0.8571
rs 10

5 Methods

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