Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |