Complex classes like ReflectionFileNamespace 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 ReflectionFileNamespace, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class ReflectionFileNamespace |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * List of classes in the namespace |
||
| 27 | * |
||
| 28 | * @var array|ReflectionClass[] |
||
| 29 | */ |
||
| 30 | protected $fileClasses; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * List of functions in the namespace |
||
| 34 | * |
||
| 35 | * @var array|ReflectionFunction[] |
||
| 36 | */ |
||
| 37 | protected $fileFunctions; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * List of constants in the namespace |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $fileConstants; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * List of imported namespaces (aliases) |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $fileNamespaceAliases; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Namespace node |
||
| 55 | * |
||
| 56 | * @var Namespace_ |
||
| 57 | */ |
||
| 58 | private $namespaceNode; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Name of the file |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | private $fileName; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * File namespace constructor |
||
| 69 | * |
||
| 70 | * @param string $fileName Name of the file |
||
| 71 | * @param string $namespaceName Name of the namespace |
||
| 72 | * @param Namespace_|null $namespaceNode Optional AST-node for this namespace block |
||
| 73 | */ |
||
| 74 | 38 | public function __construct($fileName, $namespaceName, Namespace_ $namespaceNode = null) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Returns the concrete class from the file namespace or false if there is no class |
||
| 85 | * |
||
| 86 | * @param string $className |
||
| 87 | * |
||
| 88 | * @return bool|ReflectionClass |
||
| 89 | */ |
||
| 90 | 16 | public function getClass($className) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Gets list of classes in the namespace |
||
| 101 | * |
||
| 102 | * @return ReflectionClass[]|array |
||
| 103 | */ |
||
| 104 | 20 | public function getClasses() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Returns a value for the constant |
||
| 115 | * |
||
| 116 | * @param string $constantName name of the constant to fetch |
||
| 117 | * |
||
| 118 | * @return bool|mixed |
||
| 119 | */ |
||
| 120 | 9 | public function getConstant($constantName) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Returns a list of defined constants in the namespace |
||
| 131 | * |
||
| 132 | * @return array |
||
| 133 | */ |
||
| 134 | 13 | public function getConstants() |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Gets doc comments from a class. |
||
| 145 | * |
||
| 146 | * @return string|false The doc comment if it exists, otherwise "false" |
||
| 147 | */ |
||
| 148 | 1 | public function getDocComment() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Gets starting line number |
||
| 162 | * |
||
| 163 | * @return integer |
||
| 164 | */ |
||
| 165 | 1 | public function getEndLine() |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Returns the name of file |
||
| 172 | * |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | 3 | public function getFileName() |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Returns the concrete function from the file namespace or false if there is no function |
||
| 182 | * |
||
| 183 | * @param string $functionName |
||
| 184 | * |
||
| 185 | * @return bool|ReflectionFunction |
||
| 186 | */ |
||
| 187 | 6 | public function getFunction($functionName) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Gets list of functions in the namespace |
||
| 198 | * |
||
| 199 | * @return ReflectionFunction[]|array |
||
| 200 | */ |
||
| 201 | 10 | public function getFunctions() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Gets namespace name |
||
| 212 | * |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | 33 | public function getName() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Returns a list of namespace aliases |
||
| 224 | * |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | 1 | public function getNamespaceAliases() |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Gets starting line number |
||
| 238 | * |
||
| 239 | * @return integer |
||
| 240 | */ |
||
| 241 | 1 | public function getStartLine() |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Checks if the given class is present in this filenamespace |
||
| 248 | * |
||
| 249 | * @param string $className |
||
| 250 | * |
||
| 251 | * @return bool |
||
| 252 | */ |
||
| 253 | 17 | public function hasClass($className) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Checks if the given constant is present in this filenamespace |
||
| 262 | * |
||
| 263 | * @param string $constantName |
||
| 264 | * |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | 13 | public function hasConstant($constantName) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Checks if the given function is present in this filenamespace |
||
| 276 | * |
||
| 277 | * @param string $functionName |
||
| 278 | * |
||
| 279 | * @return bool |
||
| 280 | */ |
||
| 281 | 7 | public function hasFunction($functionName) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Searches for classes in the given AST |
||
| 290 | * |
||
| 291 | * @return array|ReflectionClass[] |
||
| 292 | */ |
||
| 293 | 20 | private function findClasses() |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Searches for functions in the given AST |
||
| 313 | * |
||
| 314 | * @return array |
||
| 315 | */ |
||
| 316 | 10 | private function findFunctions() |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Searches for constants in the given AST |
||
| 337 | * |
||
| 338 | * @return array |
||
| 339 | */ |
||
| 340 | 13 | private function findConstants() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Searchse for namespace aliases for the current block |
||
| 363 | * |
||
| 364 | * @return array |
||
| 365 | */ |
||
| 366 | 1 | private function findNamespaceAliases() |
|
| 384 | } |
||
| 385 |