Complex classes like ReflectionClass 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 ReflectionClass, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class ReflectionClass extends \ReflectionClass |
||
| 11 | { |
||
| 12 | |||
| 13 | use ReflectionDocCommentTrait; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Get the body of the class. |
||
| 17 | * |
||
| 18 | * @return string |
||
| 19 | * @throws \Wingu\OctopusCore\Reflection\Exceptions\RuntimeException If the class is internal. |
||
| 20 | */ |
||
| 21 | 51 | public function getBody() |
|
| 22 | { |
||
| 23 | 51 | $fileName = $this->getFileName(); |
|
| 24 | 51 | if ($fileName === false) { |
|
| 25 | 3 | throw new RuntimeException('Can not get body of a class that is internal.'); |
|
| 26 | } |
||
| 27 | |||
| 28 | 48 | $lines = file($fileName, FILE_IGNORE_NEW_LINES); |
|
| 29 | 48 | $lines = array_slice( |
|
| 30 | 16 | $lines, |
|
| 31 | 48 | $this->getStartLine() - 1, |
|
| 32 | 48 | ($this->getEndLine() - $this->getStartLine() + 1), |
|
| 33 | 32 | true |
|
| 34 | 16 | ); |
|
| 35 | 48 | $lines = implode("\n", $lines); |
|
| 36 | |||
| 37 | 48 | $firstBracketPos = strpos($lines, '{'); |
|
| 38 | 48 | $lastBracketPost = strrpos($lines, '}'); |
|
| 39 | 48 | $body = substr($lines, $firstBracketPos + 1, $lastBracketPost - $firstBracketPos - 1); |
|
| 40 | |||
| 41 | 48 | return trim(rtrim($body), "\n\r"); |
|
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Gets the interfaces. |
||
| 46 | * |
||
| 47 | * @return \Wingu\OctopusCore\Reflection\ReflectionClass[] |
||
| 48 | */ |
||
| 49 | 12 | public function getInterfaces() |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Get interfaces that are implemented directly by the reflected class. |
||
| 62 | * |
||
| 63 | * @return \Wingu\OctopusCore\Reflection\ReflectionClass[] |
||
| 64 | */ |
||
| 65 | 6 | public function getOwnInterfaces() |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Gets a reflection on a method. |
||
| 77 | * |
||
| 78 | * @param string $name The method name. |
||
| 79 | * @return \Wingu\OctopusCore\Reflection\ReflectionMethod |
||
| 80 | */ |
||
| 81 | 12 | public function getMethod($name) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Gets a list of methods. |
||
| 88 | * |
||
| 89 | * @param integer $filter Filter for method types. This is an OR filter only. |
||
| 90 | * @return \Wingu\OctopusCore\Reflection\ReflectionMethod[] |
||
| 91 | */ |
||
| 92 | 24 | public function getMethods($filter = -1) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Gets a list of own methods (not inherited). |
||
| 104 | * |
||
| 105 | * @param integer $filter Filter for method types. |
||
| 106 | * @return \Wingu\OctopusCore\Reflection\ReflectionMethod[] |
||
| 107 | */ |
||
| 108 | 15 | public function getOwnMethods($filter = -1) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Gets the constructor of the class. |
||
| 135 | * |
||
| 136 | * @return \Wingu\OctopusCore\Reflection\ReflectionMethod |
||
| 137 | */ |
||
| 138 | 6 | public function getConstructor() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Checks if this method belongs to the current class (not inherited). |
||
| 149 | * |
||
| 150 | * @param string $name Name of the method being checked for. |
||
| 151 | * @return boolean |
||
| 152 | */ |
||
| 153 | 9 | public function hasOwnMethod($name) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Get the parent class. |
||
| 164 | * |
||
| 165 | * @return \Wingu\OctopusCore\Reflection\ReflectionClass |
||
| 166 | */ |
||
| 167 | 18 | public function getParentClass() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Get a property reflection. |
||
| 176 | * |
||
| 177 | * @param string $name Name of the property. |
||
| 178 | * @return \Wingu\OctopusCore\Reflection\ReflectionProperty |
||
| 179 | */ |
||
| 180 | 3 | public function getProperty($name) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Gets properties. |
||
| 187 | * |
||
| 188 | * @param integer $filter Filter for the properties. |
||
| 189 | * @return \Wingu\OctopusCore\Reflection\ReflectionProperty[] |
||
| 190 | */ |
||
| 191 | 21 | public function getProperties($filter = -1) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Get properties that are defined in the reflected class. |
||
| 203 | * |
||
| 204 | * @param integer $filter Filter for the properties. |
||
| 205 | * @return \Wingu\OctopusCore\Reflection\ReflectionProperty[] |
||
| 206 | */ |
||
| 207 | 12 | public function getOwnProperties($filter = -1) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Get the constants that are defined in the class |
||
| 231 | * |
||
| 232 | * @return \Wingu\OctopusCore\Reflection\ReflectionConstant[] the array of constants |
||
| 233 | */ |
||
| 234 | 18 | public function getConstants() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Get constants that are defined directly by the reflected class. |
||
| 247 | * |
||
| 248 | * @return \Wingu\OctopusCore\Reflection\ReflectionConstant[] |
||
| 249 | */ |
||
| 250 | 9 | public function getOwnConstants() |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Returns an array of traits used by this class. |
||
| 261 | * |
||
| 262 | * @return \Wingu\OctopusCore\Reflection\ReflectionClass[] |
||
| 263 | */ |
||
| 264 | 30 | public function getTraits() |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Get the use statements. |
||
| 278 | * |
||
| 279 | * @return \Wingu\OctopusCore\Reflection\ReflectionClassUse[] |
||
| 280 | */ |
||
| 281 | 3 | public function getUses() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Gets a ReflectionExtension object for the extension which defined the class. |
||
| 293 | * |
||
| 294 | * @return \Wingu\OctopusCore\Reflection\ReflectionExtension |
||
| 295 | */ |
||
| 296 | 3 | public function getExtension() |
|
| 305 | } |
||
| 306 |