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 | 38 | 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 | 38 | $this->className = $className; |
|
| 56 | 38 | $this->declaringClass = $declaringClass; |
|
| 57 | 38 | $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 | 38 | unset($this->name, $this->class); |
|
| 61 | 38 | } |
|
| 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 | 49 | public function __toString() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * {@inheritDoc} |
||
| 123 | */ |
||
| 124 | 1 | public function getClosure($object) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritDoc} |
||
| 133 | */ |
||
| 134 | 107 | public function getDeclaringClass() |
|
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritDoc} |
||
| 141 | */ |
||
| 142 | 52 | public function getModifiers() |
|
| 166 | |||
| 167 | /** |
||
| 168 | * {@inheritDoc} |
||
| 169 | */ |
||
| 170 | 50 | public function getPrototype() |
|
| 184 | |||
| 185 | /** |
||
| 186 | * {@inheritDoc} |
||
| 187 | */ |
||
| 188 | 1 | public function invoke($object, $args = null) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * {@inheritDoc} |
||
| 197 | */ |
||
| 198 | 3 | public function invokeArgs($object, array $args) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * {@inheritDoc} |
||
| 207 | */ |
||
| 208 | 101 | public function isAbstract() |
|
| 212 | |||
| 213 | /** |
||
| 214 | * {@inheritDoc} |
||
| 215 | */ |
||
| 216 | 98 | public function isConstructor() |
|
| 220 | |||
| 221 | /** |
||
| 222 | * {@inheritDoc} |
||
| 223 | */ |
||
| 224 | 98 | public function isDestructor() |
|
| 228 | |||
| 229 | /** |
||
| 230 | * {@inheritDoc} |
||
| 231 | */ |
||
| 232 | 101 | public function isFinal() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * {@inheritDoc} |
||
| 239 | */ |
||
| 240 | 101 | public function isPrivate() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * {@inheritDoc} |
||
| 247 | */ |
||
| 248 | 101 | public function isProtected() |
|
| 252 | |||
| 253 | /** |
||
| 254 | * {@inheritDoc} |
||
| 255 | */ |
||
| 256 | 104 | public function isPublic() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * {@inheritDoc} |
||
| 263 | */ |
||
| 264 | 101 | public function isStatic() |
|
| 268 | |||
| 269 | /** |
||
| 270 | * {@inheritDoc} |
||
| 271 | */ |
||
| 272 | 1 | public function setAccessible($accessible) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Parses methods from the concrete class node |
||
| 281 | * |
||
| 282 | * @param ClassLike $classLikeNode Class-like node |
||
| 283 | * @param ReflectionClass $reflectionClass Reflection of the class |
||
| 284 | * |
||
| 285 | * @return array|ReflectionMethod[] |
||
| 286 | */ |
||
| 287 | 52 | public static function collectFromClassNode(ClassLike $classLikeNode, ReflectionClass $reflectionClass) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Implementation of internal reflection initialization |
||
| 310 | * |
||
| 311 | * @return void |
||
| 312 | */ |
||
| 313 | 54 | protected function __initialize() |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Returns ClassMethod node to prevent all possible type checks with instanceof |
||
| 320 | * |
||
| 321 | * @return ClassMethod |
||
| 322 | */ |
||
| 323 | 445 | private function getClassMethodNode() |
|
| 327 | } |
||
| 328 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: