Passed
Push — master ( 9ebbfb...a3c8aa )
by Joao
10:06 queued 13s
created

Singleton::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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