Complex classes like Reflection 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 Reflection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | abstract class Reflection implements \Reflector |
||
| 6 | { |
||
| 7 | const IS_STATIC = 1; |
||
| 8 | const IS_ABSTRACT = 2; |
||
| 9 | const IS_FINAL = 4; |
||
| 10 | const IS_IMPLICIT_ABSTRACT = 16; |
||
| 11 | const IS_EXPLICIT_ABSTRACT = 32; |
||
| 12 | const IS_PUBLIC = 256; |
||
| 13 | const IS_PROTECTED = 512; |
||
| 14 | const IS_PRIVATE = 1024; |
||
| 15 | const IS_DEPRECATED = 262144; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @type string |
||
| 19 | */ |
||
| 20 | protected $name; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @type string |
||
| 24 | */ |
||
| 25 | protected $fileName; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @type int |
||
| 29 | */ |
||
| 30 | protected $startLine; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @type int |
||
| 34 | */ |
||
| 35 | protected $endLine; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Constructs a new Reflection object. |
||
| 39 | * |
||
| 40 | * @param string $name The fully qualified name |
||
| 41 | */ |
||
| 42 | 1101 | public function __construct($name) |
|
| 46 | |||
| 47 | /** |
||
| 48 | * Gets the fully qualified entity name (with the namespace). |
||
| 49 | * |
||
| 50 | * @return string |
||
| 51 | */ |
||
| 52 | 963 | public function getName() |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Gets the short name of the entity (without the namespace). |
||
| 59 | * |
||
| 60 | * @return string |
||
| 61 | */ |
||
| 62 | 1101 | public function getShortName() |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Gets the namespace name. |
||
| 71 | * |
||
| 72 | * @return string |
||
| 73 | */ |
||
| 74 | 12 | public function getNamespaceName() |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Checks if this entity is defined in a namespace. |
||
| 84 | * |
||
| 85 | * @return bool |
||
| 86 | */ |
||
| 87 | 6 | public function inNamespace() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Gets the filename of the file in which the class has been defined. |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | 1065 | public function getFileName() |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Get the starting line number. |
||
| 104 | * |
||
| 105 | * @return int |
||
| 106 | */ |
||
| 107 | 6 | public function getStartLine() |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Get the ending line number. |
||
| 114 | * |
||
| 115 | * @return int |
||
| 116 | */ |
||
| 117 | public function getEndLine() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Gets a ReflectionExtension object for the extension which defined the entity. |
||
| 124 | * |
||
| 125 | * @return \ReflectionExtension|null Null for user defined |
||
| 126 | */ |
||
| 127 | public function getExtension() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Gets the name of the extension which defined the entity. |
||
| 134 | * |
||
| 135 | * @return string|bool Extension name or false for user defined |
||
| 136 | */ |
||
| 137 | public function getExtensionName() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Returns a bitfield of the access modifiers for this class. |
||
| 144 | * |
||
| 145 | * @return int Bitmask of modifier constants |
||
| 146 | */ |
||
| 147 | 12 | public function getModifiers() |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Returns the string representation of the Reflection object. |
||
| 174 | * |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | public function __toString() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Exports a reflected class. |
||
| 184 | * |
||
| 185 | * @param \Reflector $argument The reflection to export. |
||
| 186 | * @param bool $return Setting to true will return the export, otherwise will emitt it. |
||
| 187 | * |
||
| 188 | * @return string|void |
||
| 189 | */ |
||
| 190 | public static function export(\Reflector $argument, $return = false) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Gets modifiers names. |
||
| 199 | * |
||
| 200 | * @param int $modifiers |
||
| 201 | * |
||
| 202 | * @return array An array of modifier names |
||
| 203 | */ |
||
| 204 | 6 | public static function getModifierNames($modifiers) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Sets the filename of the file in which the class has been defined. |
||
| 242 | * |
||
| 243 | * @internal |
||
| 244 | * |
||
| 245 | * @param string $fileName |
||
| 246 | * |
||
| 247 | * @return self |
||
| 248 | */ |
||
| 249 | 1101 | public function setFilename($fileName) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Set the starting line number. |
||
| 258 | * |
||
| 259 | * @internal |
||
| 260 | * |
||
| 261 | * @param int $startLine |
||
| 262 | * |
||
| 263 | * @return self |
||
| 264 | */ |
||
| 265 | 1101 | public function setStartLine($startLine) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Set the starting line number. |
||
| 274 | * |
||
| 275 | * @internal |
||
| 276 | * |
||
| 277 | * @param int $endLine |
||
| 278 | * |
||
| 279 | * @return self |
||
| 280 | */ |
||
| 281 | 1101 | public function setEndLine($endLine) |
|
| 287 | } |
||
| 288 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: