Complex classes like DataObjectSchema 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 DataObjectSchema, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class DataObjectSchema { |
||
| 11 | use Injectable; |
||
| 12 | use Configurable; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Default separate for table namespaces. Can be set to any string for |
||
| 16 | * databases that do not support some characters. |
||
| 17 | * |
||
| 18 | * Defaults to \ to to conform to 3.x convention. |
||
| 19 | * |
||
| 20 | * @config |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | private static $table_namespace_separator = '\\'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Cache of database fields |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $databaseFields = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Cache of composite database field |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $compositeFields = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Cache of table names |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $tableNames = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Clear cached table names |
||
| 48 | */ |
||
| 49 | public function reset() { |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Get all table names |
||
| 57 | * |
||
| 58 | * @return array |
||
| 59 | */ |
||
| 60 | public function getTableNames() { |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Given a DataObject class and a field on that class, determine the appropriate SQL for |
||
| 67 | * selecting / filtering on in a SQL string. Note that $class must be a valid class, not an |
||
| 68 | * arbitrary table. |
||
| 69 | * |
||
| 70 | * The result will be a standard ANSI-sql quoted string in "Table"."Column" format. |
||
| 71 | * |
||
| 72 | * @param string $class Class name (not a table). |
||
| 73 | * @param string $field Name of field that belongs to this class (or a parent class) |
||
| 74 | * @return string The SQL identifier string for the corresponding column for this field |
||
| 75 | */ |
||
| 76 | public function sqlColumnForField($class, $field) { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Get table name for the given class. |
||
| 86 | * |
||
| 87 | * Note that this does not confirm a table actually exists (or should exist), but returns |
||
| 88 | * the name that would be used if this table did exist. |
||
| 89 | * |
||
| 90 | * @param string $class |
||
| 91 | * @return string Returns the table name, or null if there is no table |
||
| 92 | */ |
||
| 93 | public function tableName($class) { |
||
| 101 | /** |
||
| 102 | * Returns the root class (the first to extend from DataObject) for the |
||
| 103 | * passed class. |
||
| 104 | * |
||
| 105 | * @param string|object $class |
||
| 106 | * @return string |
||
| 107 | * @throws InvalidArgumentException |
||
| 108 | */ |
||
| 109 | public function baseDataClass($class) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Get the base table |
||
| 123 | * |
||
| 124 | * @param string|object $class |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | public function baseDataTable($class) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Find the class for the given table |
||
| 133 | * |
||
| 134 | * @param string $table |
||
| 135 | * @return string|null The FQN of the class, or null if not found |
||
| 136 | */ |
||
| 137 | public function tableClass($table) { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Cache all table names if necessary |
||
| 158 | */ |
||
| 159 | protected function cacheTableNames() { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Generate table name for a class. |
||
| 183 | * |
||
| 184 | * Note: some DB schema have a hard limit on table name length. This is not enforced by this method. |
||
| 185 | * See dev/build errors for details in case of table name violation. |
||
| 186 | * |
||
| 187 | * @param string $class |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | protected function buildTableName($class) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Return the complete map of fields to specification on this object, including fixed_fields. |
||
| 204 | * "ID" will be included on every table. |
||
| 205 | * |
||
| 206 | * @param string $class Class name to query from |
||
| 207 | * @return array Map of fieldname to specification, similiar to {@link DataObject::$db}. |
||
| 208 | */ |
||
| 209 | public function databaseFields($class) { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Returns a list of all the composite if the given db field on the class is a composite field. |
||
| 220 | * Will check all applicable ancestor classes and aggregate results. |
||
| 221 | * |
||
| 222 | * Can be called directly on an object. E.g. Member::composite_fields(), or Member::composite_fields(null, true) |
||
| 223 | * to aggregate. |
||
| 224 | * |
||
| 225 | * Includes composite has_one (Polymorphic) fields |
||
| 226 | * |
||
| 227 | * @param string $class Name of class to check |
||
| 228 | * @param bool $aggregated Include fields in entire hierarchy, rather than just on this table |
||
| 229 | * @return array List of composite fields and their class spec |
||
| 230 | */ |
||
| 231 | public function compositeFields($class, $aggregated = true) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Cache all database and composite fields for the given class. |
||
| 251 | * Will do nothing if already cached |
||
| 252 | * |
||
| 253 | * @param string $class Class name to cache |
||
| 254 | */ |
||
| 255 | protected function cacheDatabaseFields($class) { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Returns the table name in the class hierarchy which contains a given |
||
| 315 | * field column for a {@link DataObject}. If the field does not exist, this |
||
| 316 | * will return null. |
||
| 317 | * |
||
| 318 | * @param string $candidateClass |
||
| 319 | * @param string $fieldName |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | public function tableForField($candidateClass, $fieldName) { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Returns the class name in the class hierarchy which contains a given |
||
| 332 | * field column for a {@link DataObject}. If the field does not exist, this |
||
| 333 | * will return null. |
||
| 334 | * |
||
| 335 | * @param string $candidateClass |
||
| 336 | * @param string $fieldName |
||
| 337 | * @return string |
||
| 338 | */ |
||
| 339 | public function classForField($candidateClass, $fieldName) { |
||
| 362 | } |
||
| 363 |
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: