FlyTrait::fly()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 16
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 5
nop 1
crap 20
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