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() |
|
| 67 | { |
||
| 68 | return [ |
||
| 69 | 1 | 'name' => $this->getClassMethodNode()->name, |
|
| 70 | 1 | 'class' => $this->className |
|
| 71 | ]; |
||
| 72 | } |
||
| 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) |
|
| 121 | { |
||
| 122 | 1 | $this->initializeInternalReflection(); |
|
| 123 | |||
| 124 | 1 | return parent::getClosure($object); |
|
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * {@inheritDoc} |
||
| 129 | */ |
||
| 130 | 3 | public function getDeclaringClass() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * {@inheritDoc} |
||
| 137 | */ |
||
| 138 | 8 | public function getModifiers() |
|
| 139 | { |
||
| 140 | 8 | $modifiers = 0; |
|
| 141 | 8 | if ($this->isPublic()) { |
|
| 142 | 6 | $modifiers += self::IS_PUBLIC; |
|
| 143 | } |
||
| 144 | 8 | if ($this->isProtected()) { |
|
| 145 | 3 | $modifiers += self::IS_PROTECTED; |
|
| 146 | } |
||
| 147 | 8 | if ($this->isPrivate()) { |
|
| 148 | 5 | $modifiers += self::IS_PRIVATE; |
|
| 149 | } |
||
| 150 | 8 | if ($this->isAbstract()) { |
|
| 151 | 4 | $modifiers += self::IS_ABSTRACT; |
|
| 152 | } |
||
| 153 | 8 | if ($this->isFinal()) { |
|
| 154 | 3 | $modifiers += self::IS_FINAL; |
|
| 155 | } |
||
| 156 | 8 | if ($this->isStatic()) { |
|
| 157 | 3 | $modifiers += self::IS_STATIC; |
|
| 158 | } |
||
| 159 | |||
| 160 | 8 | return $modifiers; |
|
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * {@inheritDoc} |
||
| 165 | */ |
||
| 166 | 2 | public function getPrototype() |
|
| 167 | { |
||
| 168 | 2 | $parent = $this->getDeclaringClass()->getParentClass(); |
|
| 169 | 2 | if (!$parent) { |
|
| 170 | throw new ReflectionException("No prototype"); |
||
| 171 | } |
||
| 172 | |||
| 173 | 2 | $prototypeMethod = $parent->getMethod($this->getName()); |
|
| 174 | 2 | if (!$prototypeMethod) { |
|
| 175 | throw new ReflectionException("No prototype"); |
||
| 176 | } |
||
| 177 | |||
| 178 | 2 | return $prototypeMethod; |
|
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * {@inheritDoc} |
||
| 183 | */ |
||
| 184 | 1 | public function invoke($object, $args = null) |
|
| 185 | { |
||
| 186 | 1 | $this->initializeInternalReflection(); |
|
| 187 | |||
| 188 | 1 | return call_user_func_array('parent::invoke', func_get_args()); |
|
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * {@inheritDoc} |
||
| 193 | */ |
||
| 194 | 3 | public function invokeArgs($object, array $args) |
|
| 195 | { |
||
| 196 | 3 | $this->initializeInternalReflection(); |
|
| 197 | |||
| 198 | 3 | return parent::invokeArgs($object, $args); |
|
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * {@inheritDoc} |
||
| 203 | */ |
||
| 204 | 8 | public function isAbstract() |
|
| 205 | { |
||
| 206 | 8 | return $this->getClassMethodNode()->isAbstract(); |
|
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritDoc} |
||
| 211 | */ |
||
| 212 | 1 | public function isConstructor() |
|
| 213 | { |
||
| 214 | 1 | return $this->getClassMethodNode()->name == '__construct'; |
|
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritDoc} |
||
| 219 | */ |
||
| 220 | 1 | public function isDestructor() |
|
| 221 | { |
||
| 222 | 1 | return $this->getClassMethodNode()->name == '__destruct'; |
|
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritDoc} |
||
| 227 | */ |
||
| 228 | 8 | public function isFinal() |
|
| 229 | { |
||
| 230 | 8 | return $this->getClassMethodNode()->isFinal(); |
|
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritDoc} |
||
| 235 | */ |
||
| 236 | 8 | public function isPrivate() |
|
| 237 | { |
||
| 238 | 8 | return $this->getClassMethodNode()->isPrivate(); |
|
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * {@inheritDoc} |
||
| 243 | */ |
||
| 244 | 8 | public function isProtected() |
|
| 245 | { |
||
| 246 | 8 | return $this->getClassMethodNode()->isProtected(); |
|
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * {@inheritDoc} |
||
| 251 | */ |
||
| 252 | 11 | public function isPublic() |
|
| 253 | { |
||
| 254 | 11 | return $this->getClassMethodNode()->isPublic(); |
|
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * {@inheritDoc} |
||
| 259 | */ |
||
| 260 | 8 | public function isStatic() |
|
| 261 | { |
||
| 262 | 8 | return $this->getClassMethodNode()->isStatic(); |
|
| 263 | } |
||
| 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() |
|
| 310 | { |
||
| 311 | 6 | parent::__construct($this->className, $this->getName()); |
|
| 312 | 6 | } |
|
| 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 |