Complex classes like DocComment often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DocComment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class DocComment |
||
25 | { |
||
26 | |||
27 | private static $use = []; |
||
28 | private static $useAliases = []; |
||
29 | private static $namespaces = []; |
||
30 | private static $classNames = []; |
||
31 | private static $classes = []; |
||
32 | private static $methods = []; |
||
33 | private static $fields = []; |
||
34 | private static $parsedFiles = []; |
||
35 | |||
36 | 1 | public static function clearCache() |
|
45 | |||
46 | 48 | public function get($reflection) |
|
47 | { |
||
48 | 48 | if ($reflection instanceof ReflectionClass) |
|
49 | { |
||
50 | 41 | return $this->forClass($reflection); |
|
51 | } |
||
52 | 27 | elseif ($reflection instanceof ReflectionMethod) |
|
53 | { |
||
54 | 9 | return $this->forMethod($reflection); |
|
55 | } |
||
56 | 23 | elseif ($reflection instanceof ReflectionProperty) |
|
57 | { |
||
58 | 23 | return $this->forProperty($reflection); |
|
59 | } |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Get doc comment for file |
||
64 | * If file contains several classes, $className will be returned |
||
65 | * If file name matches class name, this class will be returned |
||
66 | * @param string $name |
||
67 | * @param string $className |
||
68 | */ |
||
69 | 2 | public function forFile($name, $className = null) |
|
91 | |||
92 | 50 | public function forClass(Reflector $reflection) |
|
93 | { |
||
94 | 50 | if (ClassChecker::isAnonymous($reflection->name)) |
|
95 | { |
||
96 | 1 | echo ''; |
|
97 | } |
||
98 | 50 | $fqn = $reflection->getName(); |
|
99 | 50 | $this->process($reflection->getFileName(), $fqn); |
|
100 | 50 | if (ClassChecker::isAnonymous($reflection->name)) |
|
101 | { |
||
102 | 1 | $info = new \ReflectionClass($reflection->getName()); |
|
103 | 1 | $anonFqn = $reflection->getName(); |
|
104 | 1 | NameNormalizer::normalize($anonFqn); |
|
105 | 1 | $this->processAnonymous($info, $anonFqn); |
|
106 | } |
||
107 | $result = [ |
||
108 | 50 | 'namespace' => isset(self::$namespaces[$fqn]) ? self::$namespaces[$fqn] : [], |
|
109 | 50 | 'use' => isset(self::$use[$fqn]) ? self::$use[$fqn] : [], |
|
110 | 50 | 'useAliases' => isset(self::$useAliases[$fqn]) ? self::$useAliases[$fqn] : [], |
|
111 | 50 | 'className' => isset(self::$classNames[$fqn]) ? self::$classNames[$fqn] : [], |
|
112 | 50 | 'class' => isset(self::$classes[$fqn]) ? self::$classes[$fqn] : '', |
|
113 | 50 | 'methods' => isset(self::$methods[$fqn]) ? self::$methods[$fqn] : [], |
|
114 | 50 | 'fields' => isset(self::$fields[$fqn]) ? self::$fields[$fqn] : [] |
|
115 | ]; |
||
116 | 50 | if (ClassChecker::isAnonymous($reflection->name)) |
|
117 | { |
||
118 | 1 | $result['className'] = self::$classNames[$anonFqn]; |
|
119 | 1 | $result['class'] = self::$classes[$anonFqn]; |
|
120 | } |
||
121 | 50 | return $result; |
|
122 | } |
||
123 | |||
124 | 9 | public function forMethod(Reflector $reflection) |
|
125 | { |
||
126 | 9 | $this->process($reflection->getDeclaringClass()->getFileName()); |
|
127 | |||
128 | |||
129 | |||
130 | 9 | $class = $reflection->getDeclaringClass()->getName(); |
|
131 | 9 | $method = $reflection->getName(); |
|
132 | 9 | return isset(self::$methods[$class][$method]) ? self::$methods[$class][$method] : false; |
|
133 | } |
||
134 | |||
135 | 23 | public function forProperty(Reflector $reflection) |
|
136 | { |
||
137 | 23 | $this->process($reflection->getDeclaringClass()->getFileName()); |
|
138 | |||
139 | |||
140 | 23 | $class = $reflection->getDeclaringClass()->getName(); |
|
141 | 23 | $field = $reflection->getName(); |
|
142 | 23 | return isset(self::$fields[$class][$field]) ? self::$fields[$class][$field] : false; |
|
143 | } |
||
144 | |||
145 | 1 | private function processAnonymous(Reflector $reflection, $fqn) |
|
146 | { |
||
147 | 1 | if (!isset(self::$parsedFiles[$fqn])) |
|
148 | { |
||
149 | /* @var $reflection \Maslosoft\Addendum\Reflection\ReflectionAnnotatedClass */ |
||
150 | 1 | self::$classNames[$fqn] = $fqn; |
|
151 | 1 | self::$classes[$fqn] = $reflection->getDocComment(); |
|
152 | 1 | self::$methods[$fqn] = []; |
|
153 | 1 | self::$fields[$fqn] = []; |
|
154 | 1 | foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) |
|
155 | { |
||
156 | 1 | self::$methods[$fqn][$method->name] = $method->getDocComment(); |
|
157 | } |
||
158 | 1 | foreach ($reflection->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) |
|
159 | { |
||
160 | 1 | self::$fields[$fqn][$property->name] = $property->getDocComment(); |
|
161 | } |
||
162 | 1 | self::$parsedFiles[$fqn] = $fqn; |
|
163 | } |
||
164 | 1 | return self::$parsedFiles[$fqn]; |
|
165 | } |
||
166 | |||
167 | 57 | private function process($file, $fqn = false) |
|
176 | |||
177 | 48 | protected function parse($file, $fqn = false) |
|
178 | { |
||
324 | |||
325 | 48 | private function getString($tokens, &$i, $max) |
|
350 | |||
351 | 48 | private function getTokens($file) |
|
355 | |||
356 | } |
||
357 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: