Singleton   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 9
dl 0
loc 49
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A me() 0 3 1
A __construct() 0 2 1
A getInstance() 0 8 2
A __clone() 0 3 1
1
<?php
2
/***************************************************************************
3
 *   Copyright (C) 2004-2007 by Sveta A. Smirnova                          *
4
 *                                                                         *
5
 *   This program is free software; you can redistribute it and/or modify  *
6
 *   it under the terms of the GNU Lesser General Public License as        *
7
 *   published by the Free Software Foundation; either version 3 of the    *
8
 *   License, or (at your option) any later version.                       *
9
 *                                                                         *
10
 **************************************************************************/
11
12
declare(strict_types=1);
13
14
namespace DBD\Common;
15
16
use DBD\Entity\Common\EntityException;
17
18
abstract class Singleton implements Instantiatable
19
{
20
    /**
21
     * Все вызванные ранее инстансы классов
22
     *
23
     * @var array $instances
24
     */
25
    private static $instances = [];
26
27
    /**
28
     * Singleton constructor. You can't create me
29
     */
30
    private function __construct()
31
    {
32
    }
33
34
    /**
35
     * @return Instantiatable|Singleton|static
36
     */
37
    public static function me(): Instantiatable
38
    {
39
        return self::getInstance(get_called_class());
40
    }
41
42
    /**
43
     * Функция получения инстанса класса
44
     *
45
     * @param string $class
46
     *
47
     * @return Singleton
48
     */
49
    final public static function getInstance(string $class): Singleton
50
    {
51
        if (!isset(self::$instances[$class])) {
52
            $object = new $class;
53
54
            return self::$instances[$class] = $object;
55
        } else {
56
            return self::$instances[$class];
57
        }
58
    }
59
60
    /**
61
     * do not clone me
62
     * @throws EntityException
63
     */
64
    protected function __clone()
65
    {
66
        throw new EntityException("Singleton classes should not be cloned");
67
    }
68
}
69