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 | 31 | public function __construct( |
|
| 49 | $className, |
||
| 50 | $methodName, |
||
| 51 | ClassMethod $classMethodNode = null, |
||
| 52 | ReflectionClass $declaringClass = null |
||
| 53 | ) { |
||
| 54 | //for some reason, ReflectionMethod->getNamespaceName in php always returns '', so we shouldn't use it too |
||
| 55 | 31 | $this->className = $className; |
|
| 56 | 31 | $this->declaringClass = $declaringClass; |
|
| 57 | 31 | $this->functionLikeNode = $classMethodNode ?: ReflectionEngine::parseClassMethod($className, $methodName); |
|
| 58 | |||
| 59 | // Let's unset original read-only properties to have a control over them via __get |
||
| 60 | 31 | unset($this->name, $this->class); |
|
| 61 | 31 | } |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Emulating original behaviour of reflection |
||
| 65 | */ |
||
| 66 | 1 | public function ___debugInfo() |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Returns the string representation of the Reflection method object. |
||
| 76 | * |
||
| 77 | * @link http://php.net/manual/en/reflectionmethod.tostring.php |
||
| 78 | * |
||
| 79 | * @return string |
||
| 80 | */ |
||
| 81 | 1 | public function __toString() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * {@inheritDoc} |
||
| 119 | */ |
||
| 120 | 1 | public function getClosure($object) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * {@inheritDoc} |
||
| 129 | */ |
||
| 130 | 3 | public function getDeclaringClass() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * {@inheritDoc} |
||
| 137 | */ |
||
| 138 | 8 | public function getModifiers() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * {@inheritDoc} |
||
| 165 | */ |
||
| 166 | 2 | public function getPrototype() |
|
| 180 | |||
| 181 | /** |
||
| 182 | * {@inheritDoc} |
||
| 183 | */ |
||
| 184 | 1 | public function invoke($object, $args = null) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * {@inheritDoc} |
||
| 193 | */ |
||
| 194 | 3 | public function invokeArgs($object, array $args) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * {@inheritDoc} |
||
| 203 | */ |
||
| 204 | 8 | public function isAbstract() |
|
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritDoc} |
||
| 211 | */ |
||
| 212 | 1 | public function isConstructor() |
|
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritDoc} |
||
| 219 | */ |
||
| 220 | 1 | public function isDestructor() |
|
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritDoc} |
||
| 227 | */ |
||
| 228 | 8 | public function isFinal() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritDoc} |
||
| 235 | */ |
||
| 236 | 8 | public function isPrivate() |
|
| 240 | |||
| 241 | /** |
||
| 242 | * {@inheritDoc} |
||
| 243 | */ |
||
| 244 | 8 | public function isProtected() |
|
| 248 | |||
| 249 | /** |
||
| 250 | * {@inheritDoc} |
||
| 251 | */ |
||
| 252 | 11 | public function isPublic() |
|
| 256 | |||
| 257 | /** |
||
| 258 | * {@inheritDoc} |
||
| 259 | */ |
||
| 260 | 8 | public function isStatic() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * {@inheritDoc} |
||
| 267 | */ |
||
| 268 | 1 | public function setAccessible($accessible) |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Parses methods from the concrete class node |
||
| 277 | * |
||
| 278 | * @param ClassLike $classLikeNode Class-like node |
||
| 279 | * @param ReflectionClass $reflectionClass Reflection of the class |
||
| 280 | * |
||
| 281 | * @return array|ReflectionMethod[] |
||
| 282 | */ |
||
| 283 | 66 | public static function collectFromClassNode(ClassLike $classLikeNode, ReflectionClass $reflectionClass) |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Implementation of internal reflection initialization |
||
| 306 | * |
||
| 307 | * @return void |
||
| 308 | */ |
||
| 309 | 6 | protected function __initialize() |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Returns ClassMethod node to prevent all possible type checks with instanceof |
||
| 316 | * |
||
| 317 | * @return ClassMethod |
||
| 318 | */ |
||
| 319 | 12 | private function getClassMethodNode() |
|
| 323 | } |
||
| 324 |