UseResolver::resolve()   B
last analyzed

Complexity

Conditions 11
Paths 34

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 11

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 24
cts 24
cp 1
rs 7.3166
c 0
b 0
f 0
cc 11
nc 34
nop 2
crap 11

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This software package is licensed under AGPL, Commercial license.
5
 *
6
 * @package maslosoft/addendum
7
 * @licence AGPL, Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]> (Meta container, further improvements, bugfixes)
9
 * @copyright Copyright (c) Maslosoft (Meta container, further improvements, bugfixes)
10
 * @copyright Copyright (c) Jan Suchal (Original version, builder, parser)
11
 * @link https://maslosoft.com/addendum/ - maslosoft addendum
12
 * @link https://code.google.com/p/addendum/ - original addendum project
13
 */
14
15
namespace Maslosoft\Addendum\Utilities;
16
17
use Maslosoft\Addendum\Builder\DocComment;
18
use Maslosoft\Addendum\Utilities\NameNormalizer;
19
use ReflectionMethod;
20
use ReflectionProperty;
21
use Reflector;
22
23
/**
24
 * UseResolver
25
 *
26
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
27
 */
28
class UseResolver
29
{
30
31
	/**
32
	 * Resolve class alias
33
	 * @param Reflector $reflection
34
	 * @param string $className
35
	 * @return string
36
	 */
37 21
	public static function resolve(Reflector $reflection, $className)
38
	{
39 21
		if ($reflection instanceof ReflectionProperty || $reflection instanceof ReflectionMethod)
40
		{
41 1
			$reflection = $reflection->getDeclaringClass();
42
		}
43 21
		$docs = (new DocComment())->forClass($reflection);
44 21
		$use = $docs['use'];
45 21
		$ns = $docs['namespace'];
46 21
		$aliases = $docs['useAliases'];
47
48
		// Resolve to itself with keywords
49 21
		if ($className === 'self' || $className === 'static')
50
		{
51 4
			$fqn = $ns . '\\' . $docs['className'];
52 4
			return $fqn;
53
		}
54
55
		// This is for same namespaced class as current class
56 21
		if (strpos($className, '\\') === false)
57
		{
58 10
			$aliases[$ns . '\\' . $className] = $className;
59
		}
60
61 21
		if (in_array($className, $use))
62
		{
63 4
			return $className;
64
		}
65 19
		foreach ($use as $useClause)
66
		{
67 19
			$patternClass = preg_quote($className);
68 19
			$pattern = "~\\\\$patternClass$~";
69 19
			if (preg_match($pattern, $useClause))
70
			{
71 19
				return $useClause;
72
			}
73
		}
74 16
		foreach ($aliases as $useClause => $alias)
75
		{
76 7
			if ($className == $alias)
77
			{
78 7
				NameNormalizer::normalize($useClause, false);
79 7
				return $useClause;
80
			}
81
		}
82 12
		return $className;
83
	}
84
85
}
86