FlyTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A fly() 0 16 4
1
<?php
2
3
/**
4
 * This software package is licensed under `AGPL, Commercial` license[s].
5
 *
6
 * @package maslosoft/embedi
7
 * @license AGPL, Commercial
8
 *
9
 * @copyright Copyright (c) Peter Maselkowski <[email protected]>
10
 *
11
 */
12
13
namespace Maslosoft\EmbeDi\Traits;
14
15
use Exception;
16
17
trait FlyTrait
18
{
19
20
	private static $instances = [];
21
22
	/**
23
	 * Get flyweight instance of component
24
	 * @param string $instanceId
25
	 * @return static
26
	 * @throws Exception
27
	 */
28
	public static function fly($instanceId = null)
29
	{
30
		if (null === $instanceId)
31
		{
32
			if (!defined(sprintf('%s::%s', static::class, 'DefaultInstanceId')))
33
			{
34
				throw new Exception(sprintf('Class `%s` must define constant `%s` (%1$s::%2$s)', static::class, 'DefaultInstanceId'));
35
			}
36
			$instanceId = static::DefaultInstanceId;
37
		}
38
		if (!isset(self::$instances[static::class][$instanceId]))
39
		{
40
			self::$instances[static::class][$instanceId] = new static($instanceId);
0 ignored issues
show
Unused Code introduced by
The call to FlyTrait::__construct() has too many arguments starting with $instanceId.

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...
41
		}
42
		return self::$instances[static::class][$instanceId];
43
	}
44
45
}
46