Completed
Push — master ( c5cc72...7c6f56 )
by Jean-Christophe
02:02
created

DocComment::parse()   C

Complexity

Conditions 17
Paths 17

Size

Total Lines 60
Code Lines 46

Duplication

Lines 15
Ratio 25 %

Importance

Changes 0
Metric Value
dl 15
loc 60
rs 6.2661
c 0
b 0
f 0
cc 17
eloc 46
nc 17
nop 1

How to fix   Long Method    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
	class DocComment {
3
		private static $classes = array();
4
		private static $methods = array();
5
		private static $fields = array();
6
		private static $parsedFiles = array();
7
8
		public static function clearCache() {
9
			self::$classes = array();
10
			self::$methods = array();
11
			self::$fields = array();
12
			self::$parsedFiles = array();
13
		}
14
15
		public function get($reflection) {
16
			if($reflection instanceof ReflectionClass) {
17
				return $this->forClass($reflection);
18
			} elseif($reflection instanceof ReflectionMethod) {
19
				return $this->forMethod($reflection);
20
			} elseif($reflection instanceof ReflectionProperty) {
21
				return $this->forProperty($reflection);
22
			}
23
		}
24
25
		public function forClass($reflection) {
26
			$this->process($reflection->getFileName());
27
			$name = $reflection->getName();
28
			return isset(self::$classes[$name]) ? self::$classes[$name] : false;
29
		}
30
31
		public function forMethod($reflection) {
32
			$this->process($reflection->getDeclaringClass()->getFileName());
33
			$class = $reflection->getDeclaringClass()->getName();
34
			$method = $reflection->getName();
35
			return isset(self::$methods[$class][$method]) ? self::$methods[$class][$method] : false;
36
		}
37
38
		public function forProperty($reflection) {
39
			$this->process($reflection->getDeclaringClass()->getFileName());
40
			$class = $reflection->getDeclaringClass()->getName();
41
			$field = $reflection->getName();
42
			return isset(self::$fields[$class][$field]) ? self::$fields[$class][$field] : false;
43
		}
44
45
		private function process($file) {
46
			if(!isset(self::$parsedFiles[$file])) {
47
				$this->parse($file);
48
				self::$parsedFiles[$file] = true;
49
			}
50
		}
51
52
		protected function parse($file) {
53
			$tokens = $this->getTokens($file);
54
			$currentClass = false;
55
			$currentBlock = false;
56
			$max = count($tokens);
57
			$i = 0;
58
			while($i < $max) {
59
				$token = $tokens[$i];
60
				if(is_array($token)) {
61
					list($code, $value) = $token;
62
					switch($code) {
63
						case T_DOC_COMMENT:
64
							$comment = $value;
65
							break;
66
67
						case T_CLASS:
68
							$class = $this->getString($tokens, $i, $max);
69
							if($comment !== false) {
70
								self::$classes[$class] = $comment;
71
								$comment = false;
72
							}
73
							break;
74
75
						case T_VARIABLE:
76
							if($comment !== false) {
77
								$field = substr($token[1], 1);
78
								self::$fields[$class][$field] = $comment;
79
								$comment = false;
80
							}
81
							break;
82
83
						case T_FUNCTION:
84
							if($comment !== false) {
85
								$function = $this->getString($tokens, $i, $max);
86
								self::$methods[$class][$function] = $comment;
87
								$comment = false;
88
							}
89
90
							break;
91
92
						// ignore
93
						case T_WHITESPACE:
94
						case T_PUBLIC:
95
						case T_PROTECTED:
96
						case T_PRIVATE:
97
						case T_ABSTRACT:
98
						case T_FINAL:
99
						case T_VAR:
100
							break;
101
102
						default:
103
							$comment = false;
104
							break;
105
					}
106
				} else {
107
					$comment = false;
108
				}
109
				$i++;
110
			}
111
		}
112
113
		private function getString($tokens, &$i, $max) {
114
			do {
115
				$token = $tokens[$i];
116
				$i++;
117
				if(is_array($token)) {
118
					if($token[0] == T_STRING) {
119
						return $token[1];
120
					}
121
				}
122
			} while($i <= $max);
123
			return false;
124
		}
125
126
		private function getTokens($file) {
127
			return token_get_all(file_get_contents($file));
128
		}
129
	}
130