GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AnnotationTypeReader::getType()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 2
Metric Value
c 4
b 1
f 2
dl 0
loc 22
rs 8.6737
cc 6
eloc 14
nc 6
nop 1
1
<?php
2
3
namespace Papper\Internal;
4
5
use TokenReflection\Broker;
6
use TokenReflection\Broker\Backend\Memory;
7
use TokenReflection\ReflectionClass as TokenReflectionClass;
8
9
/**
10
 * Class AnnotationTypeReader
11
 *
12
 * @author Vladimir Komissarov <[email protected]>
13
 * @todo add detection of multiple classes definition
14
 * @todo refactor returned type from string to reflection class
15
 */
16
class AnnotationTypeReader
17
{
18
	const TYPE_REGEXP = '/^[\\\\a-zA-Z_\x7f-\xff][\\\\a-zA-Z0-9_\x7f-\xff]*/i';
19
	const PROPERTY_ANNOTATION_NAME = 'var';
20
	const METHOD_ANNOTATION_NAME = 'return';
21
22
	/**
23
	 * @var string[]
24
	 */
25
	private static $ignoredTypes = array('boolean', 'bool', 'integer', 'int', 'float', 'double', 'string', 'array', 'object', 'null');
26
27
	/**
28
	 * @var \TokenReflection\Broker
29
	 */
30
	private $broker;
31
32
	public function __construct()
33
	{
34
		$this->broker = new Broker(new Memory());
35
	}
36
37
	public function getType(\Reflector $reflector)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
38
	{
39
		if (!$reflector instanceof \ReflectionProperty && !$reflector instanceof \ReflectionMethod) {
40
			throw new \InvalidArgumentException('Argument "reflector" should be instance of \ReflectionProperty or \ReflectionMethod');
41
		}
42
43
		if (null === $tokenizedClass = $this->getTokenizedReflectionClass($reflector->getDeclaringClass())) {
44
			return null;
45
		}
46
47
		if ($reflector instanceof \ReflectionProperty) {
48
			$name = self::PROPERTY_ANNOTATION_NAME;
49
			$annotations = $tokenizedClass->getProperty($reflector->name)->getAnnotations();
50
		} else {
51
			$name = self::METHOD_ANNOTATION_NAME;
52
			$annotations = $tokenizedClass->getMethod($reflector->name)->getAnnotations();
53
		}
54
55
		return isset($annotations[$name])
56
			? $this->parseType($annotations[$name], $tokenizedClass)
57
			: null;
58
	}
59
60
	private function getTokenizedReflectionClass(\ReflectionClass $class)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
61
	{
62
		$this->broker->processFile($class->getFileName());
63
		return $this->broker->getClass($class->name);
64
	}
65
66
	private function parseType($annotations, TokenReflectionClass $tokenizedClass)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
67
	{
68
		$currentNamespace = $tokenizedClass->getNamespaceName();
69
		$currentNamespaceAliases = $tokenizedClass->getNamespaceAliases();
70
71
		foreach ($annotations as $annotation) {
72
			preg_match(self::TYPE_REGEXP, $annotation, $matches);
73
			if (!empty($matches[0])) {
74
				list($parsedNamespace, $parsedClassName) = $this->parseClass($matches[0]);
75
76
				if ($this->isIgnoredType($parsedClassName)) {
77
					continue;
78
				}
79
80
				if (empty($parsedNamespace) && isset($currentNamespaceAliases[$parsedClassName])) {
81
					return $currentNamespaceAliases[$parsedClassName];
82
				} else if (isset($currentNamespaceAliases[$parsedNamespace])) {
83
					return $currentNamespaceAliases[$parsedNamespace] . '\\' . $parsedClassName;
84
				} else if (class_exists($currentNamespace . '\\' . $parsedClassName)) {
85
					return $currentNamespace . '\\' . $parsedClassName;
86
				} else if (class_exists($parsedClassName)) {
87
					return $parsedClassName;
88
				}
89
			}
90
		}
91
		return null;
92
	}
93
94
	private function parseClass($classDefinition)
95
	{
96
		return (false !== $pos = strpos($classDefinition, "\\"))
97
			? array(substr($classDefinition, 0, $pos), substr($classDefinition, $pos + 1))
98
			: array('', $classDefinition);
99
	}
100
101
	private function isIgnoredType($type)
102
	{
103
		return in_array($type, self::$ignoredTypes);
104
	}
105
}
106