Singleton   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 7
c 1
b 0
f 0
dl 0
loc 48
ccs 0
cts 17
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __clone() 0 2 1
A __construct() 0 2 1
A getInstance() 0 9 2
A __wakeup() 0 3 1
1
<?php
2
3
namespace Mediadevs\Validator\Helpers;
4
5
use Exception;
6
7
class Singleton
8
{
9
    /**
10
     * Singleton instances will be stored in this register.
11
     *
12
     * @var array
13
     */
14
    private static $instances = array();
15
16
    /**
17
     * Singleton's constructor should not be public. However, it can't be
18
     * private either if we want to allow subclassing.
19
     */
20
    protected function __construct()
21
    {
22
        // Unreachable.
23
    }
24
25
    /**
26
     * Cloning and unserialization are not permitted for singletons.
27
     */
28
    protected function __clone()
29
    {
30
        // Unreachable.
31
    }
32
33
    /**
34
     * @throws Exception
35
     *
36
     * @return void
37
     */
38
    public function __wakeup(): void
39
    {
40
        throw new Exception('Cannot unserialize singleton');
41
    }
42
43
    /**
44
     * The method you use to get the Singleton's instance.
45
     */
46
    public static function getInstance(): self
47
    {
48
        $subclass = static::class;
49
50
        if (!isset(self::$instances[$subclass])) {
51
            self::$instances[$subclass] = new static();
52
        }
53
54
        return self::$instances[$subclass];
55
    }
56
}
57