Singleton   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 53
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getInstance() 0 12 3
A clearInstance() 0 5 1
1
<?php
2
namespace Redaxscript;
3
4
use function array_key_exists;
5
use function is_array;
6
7
/**
8
 * abstract class to create a singleton class
9
 *
10
 * @since 2.2.0
11
 *
12
 * @package Redaxscript
13
 * @category Singleton
14
 * @author Henry Ruhs
15
 *
16
 *
17
 * @codeCoverageIgnore
18
 */
19
20
abstract class Singleton
21
{
22
	/**
23
	 * instance of the class
24
	 *
25
	 * @var Singleton
26
	 */
27
28
	protected static $_instance;
29
30
	/**
31
	 * constructor of the class
32
	 *
33
	 * @since 2.2.0
34
	 */
35
36 1
	private function __construct()
37
	{
38 1
	}
39
40
	/**
41
	 * get the instance
42
	 *
43
	 * @since 2.2.0
44
	 *
45
	 * @return static
46
	 */
47
48 2
	public static function getInstance()
49
	{
50 2
		$className = static::class;
51
52
		/* create instance */
53
54 2
		if (!is_array(static::$_instance) || !array_key_exists($className, static::$_instance))
55
		{
56 1
			static::$_instance[$className] = new static();
57
		}
58 2
		return static::$_instance[$className];
59
	}
60
61
	/**
62
	 * clear the instance
63
	 *
64
	 * @since 3.0.0
65
	 */
66
67 1
	public static function clearInstance() : void
68
	{
69 1
		$className = static::class;
70 1
		self::$_instance[$className] = null;
71 1
	}
72
}
73