Complex classes like ClassInfo 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 ClassInfo, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class ClassInfo { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Wrapper for classes getter. |
||
| 20 | * |
||
| 21 | * @return array |
||
| 22 | */ |
||
| 23 | public static function allClasses() { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Returns true if a class or interface name exists. |
||
| 29 | * |
||
| 30 | * @param string $class |
||
| 31 | * @return bool |
||
| 32 | */ |
||
| 33 | public static function exists($class) { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Cache for {@link hasTable()} |
||
| 39 | */ |
||
| 40 | private static $_cache_all_tables = array(); |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var Array Cache for {@link ancestry()}. |
||
| 44 | */ |
||
| 45 | private static $_cache_ancestry = array(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @todo Move this to SS_Database or DB |
||
| 49 | */ |
||
| 50 | public static function hasTable($class) { |
||
| 57 | |||
| 58 | public static function reset_db_cache() { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Returns the manifest of all classes which are present in the database. |
||
| 65 | * |
||
| 66 | * @param string $class Class name to check enum values for ClassName field |
||
| 67 | * @param boolean $includeUnbacked Flag indicating whether or not to include |
||
| 68 | * types that don't exist as implemented classes. By default these are excluded. |
||
| 69 | * @return array List of subclasses |
||
| 70 | */ |
||
| 71 | public static function getValidSubClasses($class = 'SilverStripe\\CMS\\Model\\SiteTree', $includeUnbacked = false) { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Returns an array of the current class and all its ancestors and children |
||
| 86 | * which require a DB table. |
||
| 87 | * |
||
| 88 | * @todo Move this into {@see DataObjectSchema} |
||
| 89 | * |
||
| 90 | * @param string|object $nameOrObject Class or object instance |
||
| 91 | * @return array |
||
| 92 | */ |
||
| 93 | public static function dataClassesFor($nameOrObject) { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @deprecated 4.0..5.0 |
||
| 118 | */ |
||
| 119 | public static function baseDataClass($class) { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Returns a list of classes that inherit from the given class. |
||
| 126 | * The resulting array includes the base class passed |
||
| 127 | * through the $class parameter as the first array value. |
||
| 128 | * |
||
| 129 | * Example usage: |
||
| 130 | * <code> |
||
| 131 | * ClassInfo::subclassesFor('BaseClass'); |
||
| 132 | * array( |
||
| 133 | * 'BaseClass' => 'BaseClass', |
||
| 134 | * 'ChildClass' => 'ChildClass', |
||
| 135 | * 'GrandChildClass' => 'GrandChildClass' |
||
| 136 | * ) |
||
| 137 | * </code> |
||
| 138 | * |
||
| 139 | * @param string|object $nameOrObject The classname or object |
||
| 140 | * @return array Names of all subclasses as an associative array. |
||
| 141 | */ |
||
| 142 | public static function subclassesFor($nameOrObject) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Convert a class name in any case and return it as it was defined in PHP |
||
| 161 | * |
||
| 162 | * eg: self::class_name('dataobJEct'); //returns 'DataObject' |
||
| 163 | * |
||
| 164 | * @param string|object $nameOrObject The classname or object you want to normalise |
||
| 165 | * @return string The normalised class name |
||
| 166 | */ |
||
| 167 | public static function class_name($nameOrObject) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Returns the passed class name along with all its parent class names in an |
||
| 177 | * array, sorted with the root class first. |
||
| 178 | * |
||
| 179 | * @param string|object $nameOrObject Class or object instance |
||
| 180 | * @param bool $tablesOnly Only return classes that have a table in the db. |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | public static function ancestry($nameOrObject, $tablesOnly = false) { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @return array A self-keyed array of class names. Note that this is only available with Silverstripe |
||
| 209 | * classes and not built-in PHP classes. |
||
| 210 | */ |
||
| 211 | public static function implementorsOf($interfaceName) { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Returns true if the given class implements the given interface |
||
| 217 | */ |
||
| 218 | public static function classImplements($className, $interfaceName) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get all classes contained in a file. |
||
| 224 | * @uses ManifestBuilder |
||
| 225 | * |
||
| 226 | * @todo Doesn't return additional classes that only begin |
||
| 227 | * with the filename, and have additional naming separated through underscores. |
||
| 228 | * |
||
| 229 | * @param string $filePath Path to a PHP file (absolute or relative to webroot) |
||
| 230 | * @return array |
||
| 231 | */ |
||
| 232 | public static function classes_for_file($filePath) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Returns all classes contained in a certain folder. |
||
| 246 | * |
||
| 247 | * @todo Doesn't return additional classes that only begin |
||
| 248 | * with the filename, and have additional naming separated through underscores. |
||
| 249 | * |
||
| 250 | * @param string $folderPath Relative or absolute folder path |
||
| 251 | * @return array Array of class names |
||
| 252 | */ |
||
| 253 | public static function classes_for_folder($folderPath) { |
||
| 264 | |||
| 265 | private static $method_from_cache = array(); |
||
| 266 | |||
| 267 | public static function has_method_from($class, $method, $compclass) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @deprecated 4.0..5.0 |
||
| 289 | */ |
||
| 290 | public static function table_for_object_field($candidateClass, $fieldName) { |
||
| 294 | } |
||
| 295 | |||
| 296 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..