Completed
Push — master ( 41e6df...f2e1c4 )
by Roberto
04:13
created

HasInstances::clearAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 18 and the first side effect is on line 11.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Joomla! common library.
4
 *
5
 * @copyright  Copyright (C) 2017 Roberto Segura López, Inc. All rights reserved.
6
 * @license    GNU/GPL 2, http://www.gnu.org/licenses/gpl-2.0.htm
7
 */
8
9
namespace Phproberto\Joomla\Traits;
10
11 1
defined('JPATH_PLATFORM') || die;
12
13
/**
14
 * Classes using multiple singleton instances.
15
 *
16
 * @since  0.0.1
17
 */
18
trait HasInstances
19
{
20
	/**
21
	 * Cached instances
22
	 *
23
	 * @var  array
24
	 */
25
	protected static $instances = array();
26
27
	/**
28
	 * Remove an instance from cache.
29
	 *
30
	 * @param   integer  $id  Class identifier
31
	 *
32
	 * @return  void
33
	 */
34 2
	public static function clear($id)
35
	{
36 2
		unset(static::$instances[get_called_class()][$id]);
37 2
	}
38
39
	/**
40
	 * Clear all instances from cache
41
	 *
42
	 * @return  void
43
	 */
44
	public static function clearAll()
45
	{
46
		unset(static::$instances[get_called_class()]);
47
	}
48
49
	/**
50
	 * Ensure that we retrieve a non-statically-cached instance.
51
	 *
52
	 * @param   integer  $id   Identifier of the instance
53
	 *
54
	 * @return  $this
55
	 */
56 1
	public static function getFresh($id)
57
	{
58 1
		static::clear($id);
59
60 1
		return static::get($id);
61
	}
62
63
	/**
64
	 * Create and return a cached instance
65
	 *
66
	 * @param   integer  $id  Identifier of the instance
67
	 *
68
	 * @return  $this
69
	 */
70 4
	public static function get($id)
71
	{
72 4
		$class = get_called_class();
73
74 4
		if (empty(static::$instances[$class][$id]))
75
		{
76 3
			static::$instances[$class][$id] = new static($id);
0 ignored issues
show
Unused Code introduced by
The call to HasInstances::__construct() has too many arguments starting with $id.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
77
		}
78
79 4
		return static::$instances[$class][$id];
80
	}
81
}
82