Complex classes like ReflectionMethod 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 ReflectionMethod, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class ReflectionMethod extends BaseReflectionMethod |
||
| 23 | { |
||
| 24 | use ReflectionFunctionLikeTrait, InternalPropertiesEmulationTrait; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Name of the class |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | private $className; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Optional declaring class reference |
||
| 35 | * |
||
| 36 | * @var ReflectionClass |
||
| 37 | */ |
||
| 38 | private $declaringClass; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Initializes reflection instance for the method node |
||
| 42 | * |
||
| 43 | * @param string $className Name of the class |
||
| 44 | * @param string $methodName Name of the method |
||
| 45 | * @param ClassMethod $classMethodNode AST-node for method |
||
| 46 | * @param ReflectionClass $declaringClass Optional declaring class |
||
| 47 | */ |
||
| 48 | 43 | public function __construct( |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Returns an AST-node for method |
||
| 65 | * |
||
| 66 | * @return ClassMethod |
||
| 67 | */ |
||
| 68 | public function getNode() |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Emulating original behaviour of reflection |
||
| 75 | */ |
||
| 76 | 1 | public function ___debugInfo() |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Returns the string representation of the Reflection method object. |
||
| 86 | * |
||
| 87 | * @link http://php.net/manual/en/reflectionmethod.tostring.php |
||
| 88 | * |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | 64 | public function __toString() |
|
| 92 | { |
||
| 93 | // Internally $this->getReturnType() !== null is the same as $this->hasReturnType() |
||
| 94 | 64 | $returnType = $this->getReturnType(); |
|
| 95 | 64 | $hasReturnType = $returnType !== null; |
|
| 96 | 64 | $paramsNeeded = $hasReturnType || $this->getNumberOfParameters() > 0; |
|
| 97 | 64 | $paramFormat = $paramsNeeded ? "\n\n - Parameters [%d] {%s\n }" : ''; |
|
| 98 | 64 | $returnFormat = $hasReturnType ? "\n - Return [ %s ]" : ''; |
|
| 99 | 64 | $methodParameters = $this->getParameters(); |
|
| 100 | try { |
||
| 101 | 64 | $prototype = $this->getPrototype(); |
|
| 102 | 63 | } catch (\ReflectionException $e) { |
|
| 103 | 63 | $prototype = null; |
|
| 104 | } |
||
| 105 | 64 | $prototypeClass = $prototype ? $prototype->getDeclaringClass()->name : ''; |
|
| 106 | |||
| 107 | 64 | $paramString = ''; |
|
| 108 | 64 | $identation = str_repeat(' ', 4); |
|
| 109 | 64 | foreach ($methodParameters as $methodParameter) { |
|
| 110 | 17 | $paramString .= "\n{$identation}" . $methodParameter; |
|
| 111 | } |
||
| 112 | |||
| 113 | 64 | return sprintf( |
|
| 114 | 64 | "%sMethod [ <user%s%s%s>%s%s%s %s method %s ] {\n @@ %s %d - %d{$paramFormat}{$returnFormat}\n}\n", |
|
| 115 | 64 | $this->getDocComment() ? $this->getDocComment() . "\n" : '', |
|
| 116 | 64 | $prototype ? ", overwrites {$prototypeClass}, prototype {$prototypeClass}" : '', |
|
| 117 | 64 | $this->isConstructor() ? ', ctor' : '', |
|
| 118 | 64 | $this->isDestructor() ? ', dtor' : '', |
|
| 119 | 64 | $this->isFinal() ? ' final' : '', |
|
| 120 | 64 | $this->isStatic() ? ' static' : '', |
|
| 121 | 64 | $this->isAbstract() ? ' abstract' : '', |
|
| 122 | 64 | join( |
|
| 123 | 64 | ' ', |
|
| 124 | 64 | \Reflection::getModifierNames( |
|
| 125 | 64 | $this->getModifiers() & (self::IS_PUBLIC | self::IS_PROTECTED | self::IS_PRIVATE) |
|
| 126 | ) |
||
| 127 | ), |
||
| 128 | 64 | $this->getName(), |
|
| 129 | 64 | $this->getFileName(), |
|
| 130 | 64 | $this->getStartLine(), |
|
| 131 | 64 | $this->getEndLine(), |
|
| 132 | 64 | count($methodParameters), |
|
| 133 | $paramString, |
||
| 134 | 64 | $returnType ? ReflectionType::convertToDisplayType($returnType) : '' |
|
| 135 | ); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * {@inheritDoc} |
||
| 140 | */ |
||
| 141 | 1 | public function getClosure($object = null) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * {@inheritDoc} |
||
| 150 | */ |
||
| 151 | 135 | public function getDeclaringClass() |
|
| 155 | |||
| 156 | /** |
||
| 157 | * {@inheritDoc} |
||
| 158 | */ |
||
| 159 | 65 | public function getModifiers() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * {@inheritDoc} |
||
| 186 | */ |
||
| 187 | 65 | public function getPrototype() |
|
| 201 | |||
| 202 | /** |
||
| 203 | * {@inheritDoc} |
||
| 204 | */ |
||
| 205 | 1 | public function invoke($object, $args = null) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * {@inheritDoc} |
||
| 214 | */ |
||
| 215 | 3 | public function invokeArgs($object, array $args) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * {@inheritDoc} |
||
| 224 | */ |
||
| 225 | 129 | public function isAbstract() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritDoc} |
||
| 232 | */ |
||
| 233 | 128 | public function isConstructor() |
|
| 237 | |||
| 238 | /** |
||
| 239 | * {@inheritDoc} |
||
| 240 | */ |
||
| 241 | 128 | public function isDestructor() |
|
| 245 | |||
| 246 | /** |
||
| 247 | * {@inheritDoc} |
||
| 248 | */ |
||
| 249 | 129 | public function isFinal() |
|
| 253 | |||
| 254 | /** |
||
| 255 | * {@inheritDoc} |
||
| 256 | */ |
||
| 257 | 129 | public function isPrivate() |
|
| 261 | |||
| 262 | /** |
||
| 263 | * {@inheritDoc} |
||
| 264 | */ |
||
| 265 | 129 | public function isProtected() |
|
| 269 | |||
| 270 | /** |
||
| 271 | * {@inheritDoc} |
||
| 272 | */ |
||
| 273 | 132 | public function isPublic() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * {@inheritDoc} |
||
| 280 | */ |
||
| 281 | 129 | public function isStatic() |
|
| 285 | |||
| 286 | /** |
||
| 287 | * {@inheritDoc} |
||
| 288 | */ |
||
| 289 | 1 | public function setAccessible($accessible) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Parses methods from the concrete class node |
||
| 298 | * |
||
| 299 | * @param ClassLike $classLikeNode Class-like node |
||
| 300 | * @param ReflectionClass $reflectionClass Reflection of the class |
||
| 301 | * |
||
| 302 | * @return array|ReflectionMethod[] |
||
| 303 | */ |
||
| 304 | 56 | public static function collectFromClassNode(ClassLike $classLikeNode, ReflectionClass $reflectionClass) |
|
| 305 | { |
||
| 306 | 56 | $methods = []; |
|
| 307 | |||
| 308 | 56 | foreach ($classLikeNode->stmts as $classLevelNode) { |
|
| 309 | 53 | if ($classLevelNode instanceof ClassMethod) { |
|
| 310 | 43 | $classLevelNode->setAttribute('fileName', $classLikeNode->getAttribute('fileName')); |
|
| 311 | |||
| 312 | 43 | $methodName = $classLevelNode->name->toString(); |
|
| 313 | 43 | $methods[$methodName] = new ReflectionMethod( |
|
| 314 | 43 | $reflectionClass->name, |
|
| 315 | $methodName, |
||
| 316 | $classLevelNode, |
||
| 317 | $reflectionClass |
||
| 318 | ); |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | 56 | return $methods; |
|
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Implementation of internal reflection initialization |
||
| 327 | * |
||
| 328 | * @return void |
||
| 329 | */ |
||
| 330 | 69 | protected function __initialize() |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Returns ClassMethod node to prevent all possible type checks with instanceof |
||
| 337 | * |
||
| 338 | * @return ClassMethod |
||
| 339 | */ |
||
| 340 | 578 | private function getClassMethodNode() |
|
| 344 | } |
||
| 345 |