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 |
||
| 13 | class ClassInfo { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Wrapper for classes getter. |
||
| 17 | * |
||
| 18 | * @return array |
||
| 19 | */ |
||
| 20 | public static function allClasses() { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Returns true if a class or interface name exists. |
||
| 26 | * |
||
| 27 | * @param string $class |
||
| 28 | * @return bool |
||
| 29 | */ |
||
| 30 | public static function exists($class) { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Cache for {@link hasTable()} |
||
| 36 | */ |
||
| 37 | private static $_cache_all_tables = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var Array Cache for {@link ancestry()}. |
||
| 41 | */ |
||
| 42 | private static $_cache_ancestry = array(); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @todo Move this to SS_Database or DB |
||
| 46 | */ |
||
| 47 | public static function hasTable($class) { |
||
| 54 | |||
| 55 | public static function reset_db_cache() { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Returns the manifest of all classes which are present in the database. |
||
| 62 | * |
||
| 63 | * @param string $class Class name to check enum values for ClassName field |
||
| 64 | * @param boolean $includeUnbacked Flag indicating whether or not to include |
||
| 65 | * types that don't exist as implemented classes. By default these are excluded. |
||
| 66 | * @return array List of subclasses |
||
| 67 | */ |
||
| 68 | public static function getValidSubClasses($class = 'SiteTree', $includeUnbacked = false) { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Returns an array of the current class and all its ancestors and children |
||
| 79 | * which require a DB table. |
||
| 80 | * |
||
| 81 | * @todo Move this into {@see DataObjectSchema} |
||
| 82 | * |
||
| 83 | * @param string|object $class |
||
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | public static function dataClassesFor($class) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @deprecated 4.0..5.0 |
||
| 109 | */ |
||
| 110 | public static function baseDataClass($class) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Returns a list of classes that inherit from the given class. |
||
| 117 | * The resulting array includes the base class passed |
||
| 118 | * through the $class parameter as the first array value. |
||
| 119 | * |
||
| 120 | * Example usage: |
||
| 121 | * <code> |
||
| 122 | * ClassInfo::subclassesFor('BaseClass'); |
||
| 123 | * array( |
||
| 124 | * 'BaseClass' => 'BaseClass', |
||
| 125 | * 'ChildClass' => 'ChildClass', |
||
| 126 | * 'GrandChildClass' => 'GrandChildClass' |
||
| 127 | * ) |
||
| 128 | * </code> |
||
| 129 | * |
||
| 130 | * @param mixed $class string of the classname or instance of the class |
||
| 131 | * @return array Names of all subclasses as an associative array. |
||
| 132 | */ |
||
| 133 | public static function subclassesFor($class) { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Convert a class name in any case and return it as it was defined in PHP |
||
| 152 | * |
||
| 153 | * eg: self::class_name('dataobJEct'); //returns 'DataObject' |
||
| 154 | * |
||
| 155 | * @param string|object $nameOrObject The classname or object you want to normalise |
||
| 156 | * @return string The normalised class name |
||
| 157 | */ |
||
| 158 | public static function class_name($nameOrObject) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Returns the passed class name along with all its parent class names in an |
||
| 168 | * array, sorted with the root class first. |
||
| 169 | * |
||
| 170 | * @param string $class |
||
| 171 | * @param bool $tablesOnly Only return classes that have a table in the db. |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | public static function ancestry($class, $tablesOnly = false) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return array A self-keyed array of class names. Note that this is only available with Silverstripe |
||
| 198 | * classes and not built-in PHP classes. |
||
| 199 | */ |
||
| 200 | public static function implementorsOf($interfaceName) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Returns true if the given class implements the given interface |
||
| 206 | */ |
||
| 207 | public static function classImplements($className, $interfaceName) { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get all classes contained in a file. |
||
| 213 | * @uses ManifestBuilder |
||
| 214 | * |
||
| 215 | * @todo Doesn't return additional classes that only begin |
||
| 216 | * with the filename, and have additional naming separated through underscores. |
||
| 217 | * |
||
| 218 | * @param string $filePath Path to a PHP file (absolute or relative to webroot) |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | public static function classes_for_file($filePath) { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Returns all classes contained in a certain folder. |
||
| 235 | * |
||
| 236 | * @todo Doesn't return additional classes that only begin |
||
| 237 | * with the filename, and have additional naming separated through underscores. |
||
| 238 | * |
||
| 239 | * @param string $folderPath Relative or absolute folder path |
||
| 240 | * @return array Array of class names |
||
| 241 | */ |
||
| 242 | public static function classes_for_folder($folderPath) { |
||
| 253 | |||
| 254 | private static $method_from_cache = array(); |
||
| 255 | |||
| 256 | public static function has_method_from($class, $method, $compclass) { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @deprecated 4.0..5.0 |
||
| 278 | */ |
||
| 279 | public static function table_for_object_field($candidateClass, $fieldName) { |
||
| 283 | } |
||
| 284 | |||
| 285 |
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..