Complex classes like ClassManifest 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 ClassManifest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class ClassManifest |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * base manifest directory |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $base; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Set if including test classes |
||
| 33 | * |
||
| 34 | * @see TestOnly |
||
| 35 | * @var bool |
||
| 36 | */ |
||
| 37 | protected $tests; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Cache to use, if caching. |
||
| 41 | * Set to null if uncached. |
||
| 42 | * |
||
| 43 | * @var CacheInterface|null |
||
| 44 | */ |
||
| 45 | protected $cache; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Key to use for the top level cache of all items |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $cacheKey; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Map of classes to paths |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $classes = array(); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * List of root classes with no parent class |
||
| 63 | * |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $roots = array(); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * List of direct children for any class |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $children = array(); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * List of descendents for any class (direct + indirect children) |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected $descendants = array(); |
||
| 81 | |||
| 82 | /** |
||
| 83 | * List of interfaces and paths to those files |
||
| 84 | * |
||
| 85 | * @var array |
||
| 86 | */ |
||
| 87 | protected $interfaces = array(); |
||
| 88 | |||
| 89 | /** |
||
| 90 | * List of direct implementors of any interface |
||
| 91 | * |
||
| 92 | * @var array |
||
| 93 | */ |
||
| 94 | protected $implementors = array(); |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Map of traits to paths |
||
| 98 | * |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | protected $traits = array(); |
||
| 102 | |||
| 103 | /** |
||
| 104 | * PHP Parser for parsing found files |
||
| 105 | * |
||
| 106 | * @var Parser |
||
| 107 | */ |
||
| 108 | private $parser; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var NodeTraverser |
||
| 112 | */ |
||
| 113 | private $traverser; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var ClassManifestVisitor |
||
| 117 | */ |
||
| 118 | private $visitor; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Constructs and initialises a new class manifest, either loading the data |
||
| 122 | * from the cache or re-scanning for classes. |
||
| 123 | * |
||
| 124 | * @param string $base The manifest base path. |
||
| 125 | * @param bool $includeTests Include the contents of "tests" directories. |
||
| 126 | * @param bool $forceRegen Force the manifest to be regenerated. |
||
| 127 | * @param CacheFactory $cacheFactory Optional cache to use. Set to null to not cache. |
||
| 128 | */ |
||
| 129 | public function __construct( |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get or create active parser |
||
| 160 | * |
||
| 161 | * @return Parser |
||
| 162 | */ |
||
| 163 | public function getParser() |
||
| 171 | |||
| 172 | public function getTraverser() |
||
| 182 | |||
| 183 | public function getVisitor() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Returns the file path to a class or interface if it exists in the |
||
| 194 | * manifest. |
||
| 195 | * |
||
| 196 | * @param string $name |
||
| 197 | * @return string|null |
||
| 198 | */ |
||
| 199 | public function getItemPath($name) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Returns a map of lowercased class names to file paths. |
||
| 217 | * |
||
| 218 | * @return array |
||
| 219 | */ |
||
| 220 | public function getClasses() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Returns a lowercase array of all the class names in the manifest. |
||
| 227 | * |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | public function getClassNames() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Returns a lowercase array of all trait names in the manifest |
||
| 237 | * |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | public function getTraitNames() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Returns an array of all the descendant data. |
||
| 247 | * |
||
| 248 | * @return array |
||
| 249 | */ |
||
| 250 | public function getDescendants() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Returns an array containing all the descendants (direct and indirect) |
||
| 257 | * of a class. |
||
| 258 | * |
||
| 259 | * @param string|object $class |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | public function getDescendantsOf($class) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Returns a map of lowercased interface names to file locations. |
||
| 279 | * |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | public function getInterfaces() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Returns a map of lowercased interface names to the classes the implement |
||
| 289 | * them. |
||
| 290 | * |
||
| 291 | * @return array |
||
| 292 | */ |
||
| 293 | public function getImplementors() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Returns an array containing the class names that implement a certain |
||
| 300 | * interface. |
||
| 301 | * |
||
| 302 | * @param string $interface |
||
| 303 | * @return array |
||
| 304 | */ |
||
| 305 | public function getImplementorsOf($interface) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Get module that owns this class |
||
| 318 | * |
||
| 319 | * @param string $class Class name |
||
| 320 | * @return Module |
||
| 321 | */ |
||
| 322 | public function getOwnerModule($class) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Completely regenerates the manifest file. |
||
| 349 | */ |
||
| 350 | public function regenerate() |
||
| 386 | |||
| 387 | public function handleFile($basename, $pathname) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Recursively coalesces direct child information into full descendant |
||
| 487 | * information. |
||
| 488 | * |
||
| 489 | * @param string $class |
||
| 490 | * @return array |
||
| 491 | */ |
||
| 492 | protected function coalesceDescendants($class) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Verify that cached data is valid for a single item |
||
| 515 | * |
||
| 516 | * @param array $data |
||
| 517 | * @return bool |
||
| 518 | */ |
||
| 519 | protected function validateItemCache($data) |
||
| 538 | } |
||
| 539 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.