Completed
Push — master ( 22b422...fbd4ac )
by Peter
05:54
created

UseResolver::resolve()   D

Complexity

Conditions 10
Paths 18

Size

Total Lines 44
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 10.0033

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 44
ccs 30
cts 31
cp 0.9677
rs 4.8196
cc 10
eloc 23
nc 18
nop 2
crap 10.0033

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 http://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 13
	public static function resolve(Reflector $reflection, $className)
38
	{
39 13
		if ($reflection instanceof ReflectionProperty || $reflection instanceof ReflectionMethod)
40 13
		{
41 1
			$reflection = $reflection->getDeclaringClass();
42 1
		}
43 13
		$docs = (new DocComment())->forClass($reflection);
44 13
		$use = $docs['use'];
45 13
		$ns = $docs['namespace'];
46 13
		$aliases = $docs['useAliases'];
47
48
		// Resolve to itself with keywords
49 13
		if ($className === 'self' || $className === 'static')
50 13
		{
51 1
			$fqn = $ns . '\\' . $docs['className'];
52 1
			return $fqn;
53
		}
54
55
		// This is for same namespaced class as current class
56 13
		$aliases[$ns . '\\' . $className] = $className;
57
58 13
		if (in_array($className, $use))
59 13
		{
60 4
			return $className;
61
		}
62 11
		foreach ($use as $useClause)
63
		{
64 11
			$patternClass = preg_quote($className);
65 11
			$pattern = "~\\\\$patternClass$~";
66 11
			if (preg_match($pattern, $useClause))
67 11
			{
68 5
				return $useClause;
69
			}
70 11
		}
71 7
		foreach ($aliases as $useClause => $alias)
72
		{
73 7
			if ($className == $alias)
74 7
			{
75 7
				NameNormalizer::normalize($useClause, false);
76 7
				return $useClause;
77
			}
78 1
		}
79
		return $className;
80
	}
81
82
}
83