Completed
Push — master ( 0743b5...099278 )
by Roberto
04:46 queued 01:41
created

HasInstances   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 11 2
A clearInstance() 0 6 1
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') or die;
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
12
13
/**
14
 * Classes using multiple singleton instances.
15
 *
16
 * @since  __DEPLOY_VERSION__
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 1
	public static function clearInstance($id)
35
	{
36 1
		$class = get_called_class();
37
38 1
		unset(static::$instances[$class][$id]);
39 1
	}
40
41
	/**
42
	 * Create and return a cached instance
43
	 *
44
	 * @param   integer  $id  Identifier of the active instance
45
	 *
46
	 * @return  $this
47
	 */
48 3
	public static function getInstance($id)
49
	{
50 3
		$class = get_called_class();
51
52 3
		if (empty(static::$instances[$class][$id]))
53 3
		{
54 2
			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...
55 2
		}
56
57 3
		return static::$instances[$class][$id];
58
	}
59
}
60