Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 21 | class ClassInfo |
||
| 22 | { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Wrapper for classes getter. |
||
| 26 | * |
||
| 27 | * @return array |
||
| 28 | */ |
||
| 29 | public static function allClasses() |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Returns true if a class or interface name exists. |
||
| 36 | * |
||
| 37 | * @param string $class |
||
| 38 | * @return bool |
||
| 39 | */ |
||
| 40 | public static function exists($class) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Cache for {@link hasTable()} |
||
| 47 | * |
||
| 48 | * @internal |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | private static $_cache_all_tables = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @internal |
||
| 55 | * @var array Cache for {@link ancestry()}. |
||
| 56 | */ |
||
| 57 | private static $_cache_ancestry = array(); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Cache for parse_class_spec |
||
| 61 | * |
||
| 62 | * @internal |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | private static $_cache_parse = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @todo Move this to SS_Database or DB |
||
| 69 | * |
||
| 70 | * @param string $tableName |
||
| 71 | * @return bool |
||
| 72 | */ |
||
| 73 | public static function hasTable($tableName) |
||
| 81 | |||
| 82 | public static function reset_db_cache() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Returns the manifest of all classes which are present in the database. |
||
| 90 | * |
||
| 91 | * @param string $class Class name to check enum values for ClassName field |
||
| 92 | * @param boolean $includeUnbacked Flag indicating whether or not to include |
||
| 93 | * types that don't exist as implemented classes. By default these are excluded. |
||
| 94 | * @return array List of subclasses |
||
| 95 | */ |
||
| 96 | public static function getValidSubClasses($class = 'SilverStripe\\CMS\\Model\\SiteTree', $includeUnbacked = false) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Returns an array of the current class and all its ancestors and children |
||
| 114 | * which require a DB table. |
||
| 115 | * |
||
| 116 | * @todo Move this into {@see DataObjectSchema} |
||
| 117 | * |
||
| 118 | * @param string|object $nameOrObject Class or object instance |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | public static function dataClassesFor($nameOrObject) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @deprecated 4.0..5.0 |
||
| 147 | */ |
||
| 148 | public static function baseDataClass($class) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Returns a list of classes that inherit from the given class. |
||
| 156 | * The resulting array includes the base class passed |
||
| 157 | * through the $class parameter as the first array value. |
||
| 158 | * |
||
| 159 | * Example usage: |
||
| 160 | * <code> |
||
| 161 | * ClassInfo::subclassesFor('BaseClass'); |
||
| 162 | * array( |
||
| 163 | * 'BaseClass' => 'BaseClass', |
||
| 164 | * 'ChildClass' => 'ChildClass', |
||
| 165 | * 'GrandChildClass' => 'GrandChildClass' |
||
| 166 | * ) |
||
| 167 | * </code> |
||
| 168 | * |
||
| 169 | * @param string|object $nameOrObject The classname or object |
||
| 170 | * @return array Names of all subclasses as an associative array. |
||
| 171 | */ |
||
| 172 | public static function subclassesFor($nameOrObject) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Convert a class name in any case and return it as it was defined in PHP |
||
| 192 | * |
||
| 193 | * eg: self::class_name('dataobJEct'); //returns 'DataObject' |
||
| 194 | * |
||
| 195 | * @param string|object $nameOrObject The classname or object you want to normalise |
||
| 196 | * @return string The normalised class name |
||
| 197 | */ |
||
| 198 | public static function class_name($nameOrObject) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Returns the passed class name along with all its parent class names in an |
||
| 209 | * array, sorted with the root class first. |
||
| 210 | * |
||
| 211 | * @param string|object $nameOrObject Class or object instance |
||
| 212 | * @param bool $tablesOnly Only return classes that have a table in the db. |
||
| 213 | * @return array |
||
| 214 | */ |
||
| 215 | public static function ancestry($nameOrObject, $tablesOnly = false) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param string $interfaceName |
||
| 242 | * @return array A self-keyed array of class names. Note that this is only available with Silverstripe |
||
| 243 | * classes and not built-in PHP classes. |
||
| 244 | */ |
||
| 245 | public static function implementorsOf($interfaceName) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Returns true if the given class implements the given interface |
||
| 252 | * |
||
| 253 | * @param string $className |
||
| 254 | * @param string $interfaceName |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | public static function classImplements($className, $interfaceName) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Get all classes contained in a file. |
||
| 264 | * @uses ManifestBuilder |
||
| 265 | * |
||
| 266 | * @todo Doesn't return additional classes that only begin |
||
| 267 | * with the filename, and have additional naming separated through underscores. |
||
| 268 | * |
||
| 269 | * @param string $filePath Path to a PHP file (absolute or relative to webroot) |
||
| 270 | * @return array |
||
| 271 | */ |
||
| 272 | View Code Duplication | public static function classes_for_file($filePath) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Returns all classes contained in a certain folder. |
||
| 289 | * |
||
| 290 | * @todo Doesn't return additional classes that only begin |
||
| 291 | * with the filename, and have additional naming separated through underscores. |
||
| 292 | * |
||
| 293 | * @param string $folderPath Relative or absolute folder path |
||
| 294 | * @return array Array of class names |
||
| 295 | */ |
||
| 296 | View Code Duplication | public static function classes_for_folder($folderPath) |
|
| 310 | |||
| 311 | private static $method_from_cache = array(); |
||
| 312 | |||
| 313 | public static function has_method_from($class, $method, $compclass) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @deprecated 4.0..5.0 |
||
| 338 | */ |
||
| 339 | public static function table_for_object_field($candidateClass, $fieldName) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Strip namespace from class |
||
| 347 | * |
||
| 348 | * @param string|object $nameOrObject Name of class, or instance |
||
| 349 | * @return string Name of class without namespace |
||
| 350 | */ |
||
| 351 | public static function shortName($nameOrObject) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Helper to determine if the given object has a method |
||
| 359 | * |
||
| 360 | * @param object $object |
||
| 361 | * @param string $method |
||
| 362 | * @return bool |
||
| 363 | */ |
||
| 364 | public static function hasMethod($object, $method) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Parses a class-spec, such as "Versioned('Stage','Live')", as passed to create_from_string(). |
||
| 377 | * Returns a 2-element array, with classname and arguments |
||
| 378 | * |
||
| 379 | * @param string $classSpec |
||
| 380 | * @return array |
||
| 381 | * @throws Exception |
||
| 382 | */ |
||
| 383 | public static function parse_class_spec($classSpec) |
||
| 518 | } |
||
| 519 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: