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 |
||
| 22 | class ClassManifest |
||
| 23 | { |
||
| 24 | |||
| 25 | const CONF_FILE = '_config.php'; |
||
| 26 | const CONF_DIR = '_config'; |
||
| 27 | |||
| 28 | protected $base; |
||
| 29 | protected $tests; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var ManifestCache |
||
| 33 | */ |
||
| 34 | protected $cache; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $cacheKey; |
||
| 40 | |||
| 41 | protected $classes = array(); |
||
| 42 | protected $roots = array(); |
||
| 43 | protected $children = array(); |
||
| 44 | protected $descendants = array(); |
||
| 45 | protected $interfaces = array(); |
||
| 46 | protected $implementors = array(); |
||
| 47 | protected $configs = array(); |
||
| 48 | protected $configDirs = array(); |
||
| 49 | protected $traits = array(); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var \PhpParser\Parser |
||
| 53 | */ |
||
| 54 | private $parser; |
||
| 55 | /** |
||
| 56 | * @var NodeTraverser |
||
| 57 | */ |
||
| 58 | private $traverser; |
||
| 59 | /** |
||
| 60 | * @var ClassManifestVisitor |
||
| 61 | */ |
||
| 62 | private $visitor; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Constructs and initialises a new class manifest, either loading the data |
||
| 66 | * from the cache or re-scanning for classes. |
||
| 67 | * |
||
| 68 | * @param string $base The manifest base path. |
||
| 69 | * @param bool $includeTests Include the contents of "tests" directories. |
||
| 70 | * @param bool $forceRegen Force the manifest to be regenerated. |
||
| 71 | * @param bool $cache If the manifest is regenerated, cache it. |
||
| 72 | */ |
||
| 73 | public function __construct($base, $includeTests = false, $forceRegen = false, $cache = true) |
||
| 95 | |||
| 96 | public function getParser() |
||
| 104 | |||
| 105 | public function getTraverser() |
||
| 115 | |||
| 116 | public function getVisitor() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Returns the file path to a class or interface if it exists in the |
||
| 127 | * manifest. |
||
| 128 | * |
||
| 129 | * @param string $name |
||
| 130 | * @return string|null |
||
| 131 | */ |
||
| 132 | public function getItemPath($name) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Returns a map of lowercased class names to file paths. |
||
| 150 | * |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | public function getClasses() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Returns a lowercase array of all the class names in the manifest. |
||
| 160 | * |
||
| 161 | * @return array |
||
| 162 | */ |
||
| 163 | public function getClassNames() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Returns a lowercase array of all trait names in the manifest |
||
| 170 | * |
||
| 171 | * @return array |
||
| 172 | */ |
||
| 173 | public function getTraitNames() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Returns an array of all the descendant data. |
||
| 180 | * |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | public function getDescendants() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Returns an array containing all the descendants (direct and indirect) |
||
| 190 | * of a class. |
||
| 191 | * |
||
| 192 | * @param string|object $class |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | public function getDescendantsOf($class) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Returns a map of lowercased interface names to file locations. |
||
| 212 | * |
||
| 213 | * @return array |
||
| 214 | */ |
||
| 215 | public function getInterfaces() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Returns a map of lowercased interface names to the classes the implement |
||
| 222 | * them. |
||
| 223 | * |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | public function getImplementors() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Returns an array containing the class names that implement a certain |
||
| 233 | * interface. |
||
| 234 | * |
||
| 235 | * @param string $interface |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | public function getImplementorsOf($interface) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Returns an array of paths to module config files. |
||
| 251 | * |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | public function getConfigs() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Returns an array of module names mapped to their paths. |
||
| 261 | * |
||
| 262 | * "Modules" in SilverStripe are simply directories with a _config.php |
||
| 263 | * file. |
||
| 264 | * |
||
| 265 | * @return array |
||
| 266 | */ |
||
| 267 | public function getModules() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Get module that owns this class |
||
| 289 | * |
||
| 290 | * @param string $class Class name |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | public function getOwnerModule($class) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Completely regenerates the manifest file. |
||
| 315 | * |
||
| 316 | * @param bool $cache Cache the result. |
||
| 317 | */ |
||
| 318 | public function regenerate($cache = true) |
||
| 357 | |||
| 358 | public function handleDir($basename, $pathname, $depth) |
||
| 364 | |||
| 365 | public function handleFile($basename, $pathname, $depth) |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Recursively coalesces direct child information into full descendant |
||
| 474 | * information. |
||
| 475 | * |
||
| 476 | * @param string $class |
||
| 477 | * @return array |
||
| 478 | */ |
||
| 479 | protected function coalesceDescendants($class) |
||
| 499 | } |
||
| 500 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.