Completed
Push — master ( 576c5e...7a3dbb )
by Roberto
03:51
created

HasInstances   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 56
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A clearInstance() 0 6 1
A getFreshInstance() 0 6 1
A getInstance() 0 11 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 clearInstance($id)
35
	{
36 2
		$class = get_called_class();
37
38 2
		unset(static::$instances[$class][$id]);
39 2
	}
40
41
	/**
42
	 * Ensure that we retrieve a non-statically-cached instance.
43
	 *
44
	 * @param   integer  $id   Identifier of the instance
45
	 *
46
	 * @return  $this
47
	 */
48 1
	public static function getFreshInstance($id)
49
	{
50 1
		static::clearInstance($id);
51
52 1
		return static::getInstance($id);
53
	}
54
55
	/**
56
	 * Create and return a cached instance
57
	 *
58
	 * @param   integer  $id  Identifier of the instance
59
	 *
60
	 * @return  $this
61
	 */
62 4
	public static function getInstance($id)
63
	{
64 4
		$class = get_called_class();
65
66 4
		if (empty(static::$instances[$class][$id]))
67
		{
68 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...
69
		}
70
71 4
		return static::$instances[$class][$id];
72
	}
73
}
74