Passed
Push — master ( 834610...5de4ee )
by Joao
05:56 queued 04:18
created

Singleton::__sleep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
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
    protected function __construct()
8
    {
9
    }
10
11
    /**
12
     * @throws SingletonException
13
     */
14 1
    final public function __clone()
15
    {
16 1
        throw new SingletonException('You can not clone a singleton.');
17
    }
18
19
    /**
20
     * @throws SingletonException
21
     */
22 1
    final public function __sleep()
23
    {
24 1
        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 3
    public static function getInstance()
39
    {
40
        static $instances;
41
42 3
        $calledClass = get_called_class();
43
44 3
        if (!isset($instances[$calledClass])) {
45 1
            $instances[$calledClass] = new $calledClass();
46
        }
47 3
        return $instances[$calledClass];
48
    }
49
50
}
51